rishai
rishai

Reputation: 515

DecisionTreeClassifier: associating names to labels

I am using sklearn's DecisionTreeClassifier; it is said that the class labels can be of type str.

When I plot the decision tree using sklearn.tree.plot_tree, I can specify the class_names, which the docs says should be "Names of each of the target classes in ascending numerical order." But what about for str classes/labels? Is it the lexicographical order then?

Upvotes: 0

Views: 434

Answers (1)

Tarequzzaman Khan
Tarequzzaman Khan

Reputation: 494

For converting string labels str to Numeric you can use sklearn LabelEncoder

LabelEncoder Encode target labels with value between 0 and n_classes-1.

Example:

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
y = ["paris", "paris", "tokyo", "amsterdam"]
y_encode = le.fit_transform(y)
y_encode

output:

array([1, 1, 2, 0])

For more details visit the LabelEncoder docs.

Upvotes: 1

Related Questions