验证决策树图

数据科学

我以以下方式创建了决策树模型。

# first create the model
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from IPython.display import Image  
from sklearn import tree
import pydotplus
import pandas as pd
iris = datasets.load_iris()

X = iris.data
y = iris.target
clf = DecisionTreeClassifier(random_state=0)
clf.fit(X, y);

X = pd.DataFrame(X, columns=["sepal_length","sepal_width","petal_length","petal_width"])
# converted to data frame for easy analysis

然后按以下方式绘制图形

import pydotplus
# Create DOT data
dot_data = tree.export_graphviz(clf, out_file=None, feature_names=X.columns, 
class_names = iris.target_names)
# Draw graph
graph = pydotplus.graph_from_dot_data(dot_data)  
# Show graph
Image(graph.create_png())

我发现以下结果

在此处输入图片说明

我在阶段3a中获取了数据子集。

X3a = X.query("petal_width >.8 and petal_width <=1.75")

并创建了一个查找每列基尼索引的函数。

 def gini2(x):
        # (Warning: This is a concise implementation, but it is O(n**2)
        # in time and memory, where n = len(x).  *Don't* pass in huge
        # samples!)
        # Mean absolute difference
        mad = np.abs(np.subtract.outer(x, x)).mean()
        # Relative mean absolute difference
        rmad = mad/np.mean(x)
        # Gini coefficient
        g = 0.5 * rmad
        return g

最后在阶段3a验证了每列数据的基尼系数

gini2( X3a["sepal_length"] ) # returns 0.051
gini2( X3a["sepal_width"] ) # returns 0.063
gini2( X3a["petal_length"] ) # returns 0.0686
gini2( X3a["petal_width"] ) # returns 0.08, highest among all the columns

我发现最高的基尼系数是花瓣宽度(0.08)。因此,我希望在这一阶段进行的拆分将位于petal_width。但图片显示,拆分位于花瓣长度上。有人可以解释为什么采用花瓣长度(用于拆分)而不是花瓣宽度吗?

数据科学

终于我找到了问题的答案。

“基于最大信息增益进行拆分。在阶段3a(如上所示)上,petal_length的拆分会产生最大信息增益(即使petal_length在列中不具有最高的基尼值)”

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章