02-26 決策樹(鳶尾花分類)

[TOC] 更新、更全的《機器學習》的更新網站,更有python、go、數據結構與算法、爬蟲、人工智能教學等着你:http://www.javashuo.com/article/p-vozphyqp-cm.htmlhtml

決策樹(鳶尾花分類)

1、導入模塊

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.font_manager import FontProperties
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')

2、獲取數據

iris_data = datasets.load_iris()
X = iris_data.data[:, [2, 3]]
y = iris_data.target
label_list = ['山鳶尾', '雜色鳶尾', '維吉尼亞鳶尾']

3、構建決策邊界

def plot_decision_regions(X, y, classifier=None):
    marker_list = ['o', 'x', 's']
    color_list = ['r', 'b', 'g']
    cmap = ListedColormap(color_list[:len(np.unique(y))])

    x1_min, x1_max = X[:, 0].min()-1, X[:, 0].max()+1
    x2_min, x2_max = X[:, 1].min()-1, X[:, 1].max()+1
    t1 = np.linspace(x1_min, x1_max, 666)
    t2 = np.linspace(x2_min, x2_max, 666)

    x1, x2 = np.meshgrid(t1, t2)
    y_hat = classifier.predict(np.array([x1.ravel(), x2.ravel()]).T)
    y_hat = y_hat.reshape(x1.shape)
    plt.contourf(x1, x2, y_hat, alpha=0.2, cmap=cmap)
    plt.xlim(x1_min, x1_max)
    plt.ylim(x2_min, x2_max)

    for ind, clas in enumerate(np.unique(y)):
        plt.scatter(X[y == clas, 0], X[y == clas, 1], alpha=0.8, s=50,
                    c=color_list[ind], marker=marker_list[ind], label=label_list[clas])

4、訓練模型

tree = DecisionTreeClassifier(criterion='gini', max_depth=5, random_state=1)
tree.fit(X, y)
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=5,
            max_features=None, max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, presort=False, random_state=1,
            splitter='best')

5、可視化

plot_decision_regions(X, y, classifier=tree)
plt.xlabel('花瓣長度(cm)', fontproperties=font)
plt.ylabel('花瓣寬度(cm)', fontproperties=font)
plt.legend(prop=font)
plt.show()

![png](http://www.chenyoude.com/ml/02-26 決策樹(鳶尾花分類)_10_0.png?x-oss-process=style/watermark)node

6、可視化決策樹

import os
import imageio
import matplotlib.pyplot as plt
from PIL import Image
from pydotplus import graph_from_dot_data
from sklearn.tree import export_graphviz

# 可視化整顆決策樹
# filled=Ture添加顏色,rounded增長邊框圓角
# out_file=None直接把數據賦給dot_data,不產生中間文件.dot
dot_data = export_graphviz(tree, filled=True, rounded=True,
                           class_names=['山鳶尾', '雜色鳶尾', '維吉尼亞鳶尾'],
                           feature_names=['花瓣長度', '花瓣寬度'], out_file=None)
graph = graph_from_dot_data(dot_data)
if not os.path.exists('代碼-決策樹.png'):
    graph.write_png('代碼-決策樹.png')


def cut_img(img_path, new_width, new_height=None):
    '''只是爲了等比例改變圖片大小,沒有其餘做用'''
    img = Image.open(img_path)
    width, height = img.size
    if new_height is None:
        new_height = int(height * (new_width / width))
    new_img = img.resize((new_width, new_height), Image.ANTIALIAS)
    os.remove(img_path)
    new_img.save(img_path)
    new_img.close()


cut_img('代碼-決策樹.png', 500)

# 只是爲了展現圖片,沒有其餘做用
img = imageio.imread('代碼-決策樹.png')
plt.imshow(img)
plt.show()

![png](http://www.chenyoude.com/ml/02-26 決策樹(鳶尾花分類)_12_0.png?x-oss-process=style/watermark)python

相關文章
相關標籤/搜索