Issue
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:
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!
Solution
You need to use a OrdinalEncoder
and then use DecisionTree
from sklearn.preprocessing import OrdinalEncoder
ord_enc = OrdinalEncoder()
ord_enc.fit_transform(df[features])
Answered By - s510
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.