Nominal values in sklearn DecisionTreeClassifier inPython

Viewed 31

I am trying to use nominal attributes in sklearn DecisionTreeClassifier inPython because I read that the algorithm can handle them but I keep getting the error:

could not convert string to float: 'sunny' My data is here: Data

My code is:

features = ['outlook', 'temperature', 'humidity', 'windy']
X= df[features]
y = df.play

# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 70% training and 30% test

# Create Decision Tree classifer object
clf = DecisionTreeClassifier()

# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)

#Predict the response for test dataset
y_pred = clf.predict(X_test)

Any help with this would be appreciated!

2 Answers

While libraries that can process categories in a semi-automatic mode do exist (e.g. CatBoost, or the recently added sklearn.ensemble.HistGradientBoostingClassifier()), sklearn decision tree implementation still requires feature encoding (though the improvements are being considered, see https://github.com/scikit-learn/scikit-learn/pull/12866).

Since your categories seem to be ordinal, you should probably try label encoding with a defined order, like this:

df['temperature'] = sklearn.preprocessing.label_binarize(
    df['temperature'],
    classes=['cool', 'mild', 'hot']).argmax(axis=1)

You need to use a OrdinalEncoder and then use DecisionTree

from sklearn.preprocessing import OrdinalEncoder

ord_enc = OrdinalEncoder()
ord_enc.fit_transform(df[features])
Related