Issue
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")
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 ?
Solution
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. A way of achieving this, is using .reshape()
. Since we are passing a single column (feature) then we should use -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_encoded = ordinal_encoder.fit_transform(housing_cat.values.reshape(-1,1))
Answered By - Celius Stingher
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.