天天看点

python 数据科学 - 【分类模型】 ☞ 决策树

graphviz

下载地址:

​​Graphviz - Graph Visualization Software​​

链接:​​http://pan.baidu.com/s/1slwQCwd​​ 密码:dt7r

使用方式:

python 数据科学 - 【分类模型】 ☞ 决策树

决策树

思路

1. 一维,计算entropy => 特征选择标准
2. 添加维度,选择information Gain最大
3. if,else      

DecisionTreeClassifier 重要参数调参注意点

python 数据科学 - 【分类模型】 ☞ 决策树

鸢尾花 决策树(分类器)

from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
# iris.data
# iris.target
clf = tree.DecisionTreeClassifier()
clf.fit(iris.data, iris.target)
clf.predict(iris.data)      
python 数据科学 - 【分类模型】 ☞ 决策树
tree.export_graphviz(clf, out_file='tree.dot')      
python 数据科学 - 【分类模型】 ☞ 决策树
python 数据科学 - 【分类模型】 ☞ 决策树

决策边界(等高线图)

import numpy as np
import matplotlib.pyplot as plt

iris = load_iris()
X = iris.data[:, [2,3]]
Y = iris.target
clf = tree.DecisionTreeClassifier(max_depth=2)
clf.fit(X, Y)      
python 数据科学 - 【分类模型】 ☞ 决策树
python 数据科学 - 【分类模型】 ☞ 决策树
x0_min, x0_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x1_min, x1_max = X[:, 1].min() - 1, X[:, 1].max() + 1
# 产生一个以向量xx为行,向量yy为列的矩阵
xx, yy = np.meshgrid(np.arange(x0_min, x0_max, 0.1), 
                     np.arange(x1_min, x1_max, 0.1)) # 等高线的网格数据      
python 数据科学 - 【分类模型】 ☞ 决策树
python 数据科学 - 【分类模型】 ☞ 决策树
plt.plot()
plt.contourf(xx, yy , Z, alpha=0.5, cmap=plt.cm.rainbow) #alpha 透明度,cmap 颜色
plt.scatter(X[:, 0], X[:, 1], c=Y, alpha=1, cmap=plt.cm.RdYlBu) #c 颜色序列
plt.title('Decision Tree')
plt.xlabel('Petal.Length')
plt.ylabel('Petal.Width')
plt.show()      
python 数据科学 - 【分类模型】 ☞ 决策树

Set The Color Of A Matplotlib Plot

python 数据科学 - 【分类模型】 ☞ 决策树
python 数据科学 - 【分类模型】 ☞ 决策树

继续阅读