Hi, my name is Jinsu. I really enjoy your book!!
I am writing to get some help
The problem is that I cannot import "CategoricalEncoder"
from sklearn.preprocessing import CategoricalEncoder
cat_encoder = CategoricalEncoder()
housing_cat_reshaped = housing_cat.values.reshape(-1, 1)
housing_cat_1hot = cat_encoder.fit_trasform(housing_cat_reshaped)>>> housing_cat_1hot
ImportError Traceback (most recent call last)
1 #Transformation text to integer and integer to one-hot vector
----> 2 from sklearn.preprocessing import CategoricalEncoder
3 cat_encoder = CategoricalEncoder()
4 housing_cat_reshaped = housing_cat.values.reshape(-1, 1)
5 housing_cat_1hot = cat_encoder.fit_trasform(housing_cat_reshaped)
What do I need to do????
Hi @magicmutal ,
Thanks for your kind words, I'm really glad you enjoy the book! :)
The CategoricalEncoder class is only available since Scikit-Learn 0.20 (currently the dev version). So you should either update Scikit-Learn to the dev version, or simply copy the definition of the class in a categorical_encoder.py file in your code base, and just import that one until you can update to Scikit-Learn 0.20. The source code is here, it's pretty standalone, so you can mostly copy it and add the relevant imports, or just copy the code in cell #63 in the Jupyter notebook for chapter 2.
I hope this helps,
Aur茅lien
Hello,
I am interested in using the current version of Categorical Encoder for memory concerns. I have install the dev version (0.20.dev0), should I just use Categorical Encoder or is the functionality already rolled up into OneHotEncoder. I get the following message:
File "
File "/scikit-learn/sklearn/preprocessing/data.py", line 2839, in __init__
"CategoricalEncoder briefly existed in 0.20dev. Its functionality "
RuntimeError: CategoricalEncoder briefly existed in 0.20dev. Its functionality has been rolled into the OneHotEncoder and OrdinalEncoder. This stub will be removed in version 0.21.
Cheers,
Sarah
Hi @swzCuroverse ,
Thanks for your question. You should use the new sklearn.preprocessing.OneHotEncoder class from sklearn 0.20 (not the one from 0.19). I updated the notebook for chapter 2 to explain this.
Hope this helps,
Aur茅lien
Hi Ageron,
I have just started using your book and I am learning a lot thank you!
For the California House Prices Project, must you use OrdinalEncoder followed by the OneHotEncoder.
I am unable to import OrdinalEncorder from SkLearn Pre Processing or Future Encorders. The error I receive afterwards when only using One Hot Encoding is ValueError: could not convert string to float: 'NEAR BAY'.
Thanks and sorry If the question is great,
Viraj
Hi @VirajVaitha123 ,
Did you resolve this issue? If not, can you please specify which versions you are using for your OS, Python, and Scikit-Learn?
@ageron I have the same issue as @VirajVaitha123
_HANDLING TEXT AND CATEGORICAL ATTRIBUTES_
everything works fine till cell 60. What problem we are trying to solve ? The categorical attribute _ocean_proximity_ is a text attribute --> So we cannot compute its median. In general ML algorithms like to work with numbers. So we try to convert the text labels _ocean_proximity_ to numbers. We use the OneHotEncoder to convert integer categorical values into one-hot encoding ( because the ML algorithm that two nearby values are more similar than two distant values).
INPUT:
try:
from sklearn.preprocessing import OrdinalEncoder
except ImportError:
from future_encoders import OrdinalEncoder # Scikit-Learn < 0.20
OUTPUT:
`ImportError Traceback (most recent call last)
<ipython-input-66-db6cd26b8e60> in <module>()
1 try:
----> 2 from sklearn.preprocessing import OrdinalEncoder
3 except ImportError:
ImportError: cannot import name 'OrdinalEncoder' from 'sklearn.preprocessing' (/Users/morphley/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/__init__.py)
During handling of the above exception, another exception occurred:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-66-db6cd26b8e60> in <module>()
2 from sklearn.preprocessing import OrdinalEncoder
3 except ImportError:
----> 4 from future_encoders import OrdinalEncoder # Scikit-Learn < 0.20
ModuleNotFoundError: No module named 'future_encoders'
````
I checked the source code of [scikit-learn/sklearn/preprocessing/data.py](https://github.com/scikit-learn/scikit-learn/blob/34155a2/sklearn/preprocessing/data.py#L2869) and there is no definition of an OrdinalEncoder. Even when I tried to only insert following:
`
from sklearn.preprocessing import OneHotEncoder
cat_encoder = OneHotEncoder()
housing_cat_1hot = cat_encoder.fit_transform(housing_cat)
housing_cat_1hot`
I received this error message:
`
`
`---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
2
3 cat_encoder = OneHotEncoder()
----> 4 housing_cat_1hot = cat_encoder.fit_transform(housing_cat)
5 housing_cat_1hot
~/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/data.py in fit_transform(self, X, y)
2017 """
2018 return _transform_selected(X, self._fit_transform,
-> 2019 self.categorical_features, copy=True)
2020
2021 def _transform(self, X):
~/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/data.py in _transform_selected(X, transform, selected, copy)
1807 X : array or sparse matrix, shape=(n_samples, n_features_new)
1808 """
-> 1809 X = check_array(X, accept_sparse='csc', copy=copy, dtype=FLOAT_DTYPES)
1810
1811 if isinstance(selected, six.string_types) and selected == "all":
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.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)
431 force_all_finite)
432 else:
--> 433 array = np.array(array, dtype=dtype, order=order, copy=copy)
434
435 if ensure_2d:
ValueError: could not convert string to float: 'NEAR BAY'`
```
@ageron Solved the issue..
@ageron First of all great book. I have a couple of questions regarding chapter 2 End-to-End Machine Learning Projects The documentation about Pipelines on scikilearn.org is very complicated ...
We created a train_set (strat_train_set) and a test_set (strat_test_set) using stratified sampling. In the next step before feeding our ML algorithms we need to cleanup and prepare the data. We can do this automatically by using Pipelines. We use a Pipeline to execute transformation steps on our data in the right order.
First Pipeline (only for numerical values):
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
num_pipeline = Pipeline([
('imputer', SimpleImputer(strategy="median")),
('attribs_adder', CombinedAttributesAdder()),
('std_scaler', StandardScaler()),
])
Question: What kind of Transformer is the StandardScaler() ? In general can you tell us a little bit more about the different methods.
When you call the pipeline _fit()_ method it calls _fit_transform()_ sequentially on all transformers passing the output of each call as the parameter to the next call until it reaches the final estimator for which it just calls the _fit()_ method
housing_num_tr = num_pipeline.fit_transform(housing_num)
But we need a full pipeline which can handle both numerical and categorical attributes:
try:
from sklearn.compose import ColumnTransformer
except ImportError:
from future_encoders import ColumnTransformer # Scikit-Learn < 0.20
num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]
full_pipeline = ColumnTransformer([
("num", num_pipeline, num_attribs),
("cat", OneHotEncoder(), cat_attribs),
])
housing_prepared = full_pipeline.fit_transform(housing)
Question: Can you please explain how the ColumnTransformer works and how it knows how to put the table together. What happens if we have more hundreds of tables
Question: You mentioned cleaning and preparing the data. What kind of data transformation possibilities exists such as replace missing values, combine features etc.
At the end of the chapter 2 End-to-End Machine Learning Projects you are talking about Launch, Monitor and Maintain your system. I would love to get more insights about this. Can you please use this or another example to show us how to launch our model, measure the performance and read new data. Because this is crucial . We need some insights otherwise our model is not used...
Hi @morphley ,
Great questions. I'm traveling this week, but I'll try to answer next week.
@ageron Solved the issue..
@ageron Solved the issue..
@morphley I am getting the same error ("ModuleNotFoundError: No module named 'future_encoders'. and "ValueError: could not convert string to float: '<1H OCEAN'"). Do you mind sharing how you solved this issue? TIA!
Hi @subhap15 , make sure you have the future_encoders.py file (located at the root of this project) in the same directory as the notebook.
Happy new year!
Hi @ageron , That worked. Thank you so much for the reply! Your book is truly awesome! Happy New Year to you too!
@subhap15 make sure that you install the latest release of sklearn
pip
pip install -U scikit-learn
or conda:
conda install scikit-learn
@ageron happy new year !!
@morphley I will do that as well. Thank you
@suhap15 You welcome . You can contact me anytime . Cheers
num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]
full_pipeline = ColumnTransformer([
("num", num_pipeline, num_attribs),
("cat", OneHotEncoder(), cat_attribs),
])
housing_prepared = full_pipeline.fit_transform(housing)
At the code line above , we are trying to fit categorical data without labelencoding first and it gave me error when I try the pipeline. I think somewhere before the pipeline we need to convert the categorical column with Labeleencoder. I did try but my kernel died. Here is my trial
num_attribs = list(housing_num)
full_pipeline = ColumnTransformer([
("num", num_pipeline, num_attribs),
("cat", OneHotEncoder(), housing_cat_encoded),
])
housing_prepared = full_pipeline.fit_transform(housing)
I just started reading and working through the book and really enjoyed it. Thanks! I upgraded sklearn to 0.20.2. and it appears that CategoricalEncoder is now implemented through OneHotEncoder. I used the following and it seems to work.
num_pipeline = Pipeline([
('selector',DataFrameSelector(num_attribs)),
('imputer', SimpleImputer(strategy='median')),
('attrib_adder', HousingCombinedAttributesAdder()),
('std_scaler', StandardScaler())
])
cat_pipeline = Pipeline([
('selector', DataFrameSelector(cat_attribs)),
('encoder', OneHotEncoder(categories='auto',sparse=False))
])
full_pipeline = FeatureUnion(transformer_list=[
('num_pipeline',num_pipeline),
('cat_pipeline',cat_pipeline)
])
Most helpful comment
Hi @magicmutal ,
Thanks for your kind words, I'm really glad you enjoy the book! :)
The
CategoricalEncoderclass is only available since Scikit-Learn 0.20 (currently the dev version). So you should either update Scikit-Learn to the dev version, or simply copy the definition of the class in acategorical_encoder.pyfile in your code base, and just import that one until you can update to Scikit-Learn 0.20. The source code is here, it's pretty standalone, so you can mostly copy it and add the relevant imports, or just copy the code in cell #63 in the Jupyter notebook for chapter 2.I hope this helps,
Aur茅lien