如何使用字符串标签和功能训练树分类器?

时间:2018-05-28 05:22:12

标签: python machine-learning artificial-intelligence classification decision-tree

以下是我的代码:

import sklearn
#features = [[140,"smooth"],[130,"smooth"],[150,"bumpy"],[170,"bumpy"]]
#labels = ["apple","apple","orange","orange"]

# Now replace 1 for smooth & 0 for bumpy and 0 for apple & 1 for orange
features = [[140,1],[130,1],[150,0],[170,0]]
labels = [0,0,1,1]

# Now I train a classifier
from sklearn import tree

my_classifier = tree.DecisionTreeClassifier()
my_classifier.fit(features,labels)
predict = my_classifier.predict([[150,0]])
print(predict)

如何训练分类器而不将其转换为数字?

e.g。我想在下面的代码行中对我的分类器进行分类。请提前建议,谢谢。)

features = [[140,"smooth"],[130,"smooth"],[150,"bumpy"],[170,"bumpy"]]
labels = ["apple","apple","orange","orange"]

1 个答案:

答案 0 :(得分:0)

由于算法在引擎盖下工作的方式,你的标签和特征总是会在某个时刻被转换为单热矢量。

您可以维护一个字典,其中向量的哪个索引代表哪个标签,并在推断后将其转回。