是的。
决策树将点投影到每个特征上并找到最佳分割。这种划分可以通过不同的指标来确定,例如:gini 指数或熵(信息增益) Sci-kit learn 在sklearn.tree中有这个@
假设您有 5 个数据点:
color shape fruit
orange oblong orange
red round apple
orange round orange
red oblong apple
red round apple
所以要训练你会做这样的事情:
feature class | feature class
orange orange | oblong orange
red apple | round apple
orange orange | round orange
red apple | oblong apple
red apple | round apple
如您所见,最好的分割是颜色,因为对于这个数据集,如果颜色=红色,那么水果=苹果,如果颜色=橙色,那么水果=橙色。
对这些数据点进行培训,您将拥有决策树:
color
___________________
| |
| |
red orange
apple orange
在现实生活中,这些拆分将基于数值,即num > .52。
至于为此使用什么算法,这取决于。您必须对自己的数据进行研究,因为它更多的是针对每个数据集/偏好类型的事情。
您可以像这样在上面的示例中使用 sci-kit learn:
from sklearn.trees import DecisionTreeClassifier
#make your sample matrix
samples = [[1,1], [0,0], [1,0], [0,1], [0,0]]
#make your target vector ( in this case fruit)
fruitname = [1, 0, 1, 0, 0]
#create and fit the model
dtree = DecisionTreeClassifier()
dtree = dtree.fit(samples, fruitname)
#test an unknown red fruit that is oblong
dtree.predict([0,1])
注意,color=1 表示果实为橙色,shape=1 表示果实为长方形。
查看 sci-kit user guide 以获得更深入的概述。