10
$\begingroup$

I was watching Machine Learning A- Z from SuperDataScience but when I was doing below code sample:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


dataset = pd.read_csv('Data.csv')
X = dataset.iloc[:, :-1].values

from sklearn.impute import  SimpleImputer
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3]= imputer.transform(X[:,1:3])


from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features =[0])
X = onehotencoder.fit_transform(X).toarray()

I got this warning message:

/usr/local/lib/python3.5/dist-packages/sklearn/preprocessing/_encoders.py:363: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values. If you want the future behaviour and silence this warning, you can specify "categories='auto'". In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly. warnings.warn(msg, FutureWarning) /usr/local/lib/python3.5/dist-packages/sklearn/preprocessing/_encoders.py:385: DeprecationWarning: The 'categorical_features' keyword is deprecated in version 0.20 and will be removed in 0.22. You can use the ColumnTransformer instead. "use the ColumnTransformer instead.", DeprecationWarning) And this below message also

/usr/local/lib/python3.5/dist-packages/sklearn/preprocessing/_encoders.py:363: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values. If you want the future behaviour and silence this warning, you can specify "categories='auto'". In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly. warnings.warn(msg, FutureWarning)

/usr/local/lib/python3.5/dist-packages/sklearn/preprocessing/_encoders.py:385: DeprecationWarning: The 'categorical_features' keyword is deprecated in version 0.20 and will be removed in 0.22. You can use the ColumnTransformer instead. "use the ColumnTransformer instead.", DeprecationWarning) I was reading ColumnTransfer in sklearn website library I didn't understand how to fix these error messages

SampleFile:Data.csv

$\endgroup$

5 Answers 5

11
$\begingroup$

You can do this to get rid of the deprecation messages

from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

ct = ColumnTransformer(
    [('one_hot_encoder', OneHotEncoder(), [0])],    # The column numbers to be transformed (here is [0] but can be [0, 1, 3])
    remainder='passthrough'                         # Leave the rest of the columns untouched
)

x = np.array(ct.fit_transform(x), dtype=np.float)
$\endgroup$
1
$\begingroup$
# Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#import dataset
dataset = pd.read_csv("Data.csv")
X = dataset.iloc[:,:-1].values
Y = dataset.iloc[:,3].values

#Taking care of Missing data
from sklearn.impute import SimpleImputer  
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])

#Encoding Categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
transformer = ColumnTransformer([('one_hot_encoder', OneHotEncoder(), [0])],remainder='passthrough')
X = np.array(transformer.fit_transform(X), dtype=np.float)
labelencoder_Y = LabelEncoder()
Y = labelencoder_Y.fit_transform(Y)
$\endgroup$
2
  • 1
    $\begingroup$ This answer would be MUCH better if you can explanation of how and why this works. $\endgroup$
    – Stephen Rauch
    Jun 8, 2019 at 21:07
  • $\begingroup$ You realise that LabelEncoder is supposed to be used only on the target. $\endgroup$
    – smh
    May 20, 2021 at 10:03
0
$\begingroup$

For now, there is nothing you should need to do. The code should work even with these warnings. Technically, they are not errors.

If you want to build some model based on this example, you should probably resolve them. Most of the information you need is in the warning. For example:

In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.

So when you move to sklearn version 0.22, you don't need to use both the LabelEncoder() and the OneHotEncoder(), you can do it all in the OneHotEncoder(), but you will probably need to review the version specific documentation to figure out how to do this and meet you specific needs when the version is released.

For now, don't do anything.

$\endgroup$
0
$\begingroup$

Document for ColumnTransformer Example to check it. # TODO: create a LabelEncoder object and fit it to each feature in X

# import preprocessing from sklearn
from sklearn import preprocessing
# 1. INSTANTIATE
# encode labels with value between 0 and n_classes-1.
le = preprocessing.LabelEncoder()
# 2/3. FIT AND TRANSFORM
# use df.apply() to apply le.fit_transform to all columns
X_2 = X.apply(le.fit_transform)
X_2.head()

If you wish to see an end to end example please check.

$\endgroup$
-2
$\begingroup$

In OneHotEncoder, use the parameter handle_unknown, it should look something like this, and now onehotencoder is auto on the dataset X, so you can remove the categorical_features or instead keep auto, removing it will solve the error:

onehotencoder = OneHotEncoder(categorical_features=[i], handle_unknown='ignore')

onehotencoder = OneHotEncoder(handle_unknown='ignore')

Thanks.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.