[英]Not able to fix the error I get while writing code for analyzing "California Housing" data set from O'Reilly book
while executing code:(from book page 69 of "Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems Book by Aurelien Geron")执行代码时:(摘自《使用 Scikit-Learn、Keras 和 TensorFlow:构建智能系统的概念、工具和技术》一书的第 69 页)
housing_cat_encoded = ordinal_encoder.fit_transform(housing_cat)
housing_cat_encoded[:10]
I get error:-我收到错误:-
ValueError: Expected 2D array, got 1D array instead:
array=['<1H OCEAN' '<1H OCEAN' 'NEAR OCEAN' ... 'INLAND' '<1H OCEAN' 'NEAR BAY'].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
How do I fix it?我如何解决它?
In this case the error describes the issue and a way to solve it.在这种情况下,错误描述了问题和解决方法。 The function .fit_transform()
is expecting a 2D array, not a 1D one. function .fit_transform()
需要一个二维数组,而不是一维数组。 A way of achieving this, is using .reshape()
.实现这一点的一种方法是使用.reshape()
。 Since we are passing a single column (feature) then we should use -1,1
.由于我们传递的是单列(特征),所以我们应该使用-1,1
。
housing_cat_encoded = ordinal_encoder.fit_transform(housing_cat.reshape(-1,1))
If housing_cat
is a pandas series, then you might have to use:如果housing_cat
是 pandas 系列,那么您可能必须使用:
housing_cat_encoded = ordinal_encoder.fit_transform(housing_cat.values.reshape(-1,1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.