I'm on page 64 in the book right now and trying to encode ocean proximity feature to One Hot.
I'm writing every word of code same but I'm having error like:
`---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
1 from future_encoders import OneHotEncoder
2 cat_encoder = OneHotEncoder()
----> 3 housing_cat_1hot = cat_encoder.fit_transform(housing_cat)
4 housing_cat_1hot
~Documentsml_envfuture_encoders.py in fit_transform(self, X, y)
583 copy=True)
584 else:
--> 585 return self.fit(X).transform(X)
586
587 def _legacy_transform(self, X):
~Documentsml_envfuture_encoders.py in fit(self, X, y)
500 return self
501 else:
--> 502 self._fit(X, handle_unknown=self.handle_unknown)
503 return self
504
~Documentsml_envfuture_encoders.py in _fit(self, X, handle_unknown)
114 def _fit(self, X, handle_unknown='error'):
115
--> 116 X_temp = check_array(X, dtype=None)
117 if not hasattr(X, 'dtype') and np.issubdtype(X_temp.dtype, np.str_):
118 X = check_array(X, dtype=np.object)
C:ProgramDataAnaconda3libsite-packagessklearnutilsvalidation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
439 "Reshape your data either using array.reshape(-1, 1) if "
440 "your data has a single feature or array.reshape(1, -1) "
--> 441 "if it contains a single sample.".format(array))
442 array = np.atleast_2d(array)
443 # To ensure that array flags are maintained
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.`
It says this method expects 2D array. But in Geron's notebook (https://github.com/ageron/handson-ml/blob/master/02_end_to_end_machine_learning_project.ipynb in line 61) he gives just the 1D array which is objects.
What should be the problem?
Hi @ygtmsc ,
Thanks for your feedback. The error message says that the OneHotEncoder expects a matrix (2D), not a vector (1D). This is weird because the definition of housing_cat (in cell In 59) is:
housing_cat = housing[['ocean_proximity']]
Notice that there are two pairs of square brackets, not just one. This means that housing_cat will be a Pandas DataFrame with a single column. That's a 2D data structure, even though there is just a single column. Perhaps you used a single pair of square brackets instead? If so, then housing_cat would be a Pandas Series object, which is a 1D structure.
In short, please double-check that the housing_cat is a DataFrame, not a Series object. If it is a Series, then make sure you have two pairs of square brackets, this should solve the problem.
Hope this helps,
Aur茅lien
Thank you @ageron for help. It worked!
Most helpful comment
Hi @ygtmsc ,
Thanks for your feedback. The error message says that the
OneHotEncoderexpects a matrix (2D), not a vector (1D). This is weird because the definition ofhousing_cat(in cellIn 59) is:Notice that there are two pairs of square brackets, not just one. This means that
housing_catwill be a PandasDataFramewith a single column. That's a 2D data structure, even though there is just a single column. Perhaps you used a single pair of square brackets instead? If so, thenhousing_catwould be a PandasSeriesobject, which is a 1D structure.In short, please double-check that the
housing_catis a DataFrame, not aSeriesobject. If it is aSeries, then make sure you have two pairs of square brackets, this should solve the problem.Hope this helps,
Aur茅lien