(十一)决策树算法(分类)

2020-04-07  本文已影响0人  羽天驿

一、原理及概念

计算机中的是二叉树,越往下越大。

决策树

sklearn中决策树分为DecisionTreeClassifier和DecisionTreeRegressor,所以用的算法是CART算法,也就是分类与回归树算法(classification and regression tree,CART),划分标准默认使用的也是Gini,ID3和C4.5用的是信息熵

二、代码的案例

(一、信息熵的计算)

import numpy as np
image.png

H = -\sum\limits_{i =1}^np_ilog_2p_i

# 三个属性:日志密度、好友密度、是否使用真实头像
# 目标值:账号是否真实

# 构建决策树,使用属性进行裂分
# 不进行划分 3no 0.3 7yes 0.7
H1 = -(0.3*np.log2(0.3) + 0.7*np.log2(0.7))
H1
0.8812908992306927

H = \sum\limits_{i =1}^np_ilog_2\frac{1}{p_i}

H1 = (0.3*np.log2(1/0.3) + 0.7*np.log2(1/0.7))
H1
0.8812908992306926

根据某一属性,对类别进行划分,计算信息熵

根据信息增益,判别,属性重要性

# 根据日志密度进行划分,数据,变得有序一点,所以,熵变小!
# 根据日志密度进行划分
# s 3 0.3 -----> 2no 1yes
# m 4 0.4 -----> 1no 3yes
# l 3 0.3 -----> 3yes
H2 = 0.3*(2/3*np.log2(3/2) + 1/3*np.log2(3)) + 0.4*(0.25*np.log2(4) + 0.75*np.log2(4/3)) + 0.3*(1*np.log2(1))
print('根据日志密度进行划分,信息熵是:',H2)
print('信息增益是:',(H1 - H2))
根据日志密度进行划分,信息熵是: 0.5999999999999999
信息增益是: 0.2812908992306927
# 根据好友密度进行划分
# 好友比较少,容易是假账号,僵尸账号。
# s 4 0.4 -----> 3no 1yes
# m 4 0.4 -----> 4yes
# l 2 0.2 -----> 2yes
H3 = 0.4*(0.75*np.log2(4/3) + 0.25*np.log2(4)) + 0.4*(1*np.log2(1)) + 0.2*(1*np.log2(1))
print('根据好友密度进行划分,信息熵是:',H3)
print('信息增益是:',(H1 - H3))
根据好友密度进行划分,信息熵是: 0.32451124978365314
信息增益是: 0.5567796494470394

对比好友密度和日志密度

好友密度将数据划分的更加有序,更能找对规律!

构建决策树时,优先考虑好友密度!

(二、决策树的分类)

import numpy as np

from sklearn.tree import DecisionTreeClassifier

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn import tree

import matplotlib.pyplot as plt
X,y = datasets.load_iris(True)

# 随机拆分
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state = 256)

# max_depth调整树深度:剪枝操作
# max_depth默认,深度最大,延伸到将数据完全划分开为止。
model = DecisionTreeClassifier(max_depth=None,criterion='entropy')
model.fit(X_train,y_train)
y_ = model.predict(X_test)
print('真实类别是:',y_test)
print('算法预测是:',y_)
print('准确率是:',model.score(X_test,y_test))
真实类别是: [0 2 1 0 2 1 0 1 1 1 2 2 2 0 0 1 2 1 0 2 1 0 1 1 2 0 0 1 0 0 2 0 2 2 1 2 0
 0]
算法预测是: [0 2 1 0 2 1 0 1 1 1 2 2 2 0 0 1 2 1 0 2 1 0 1 1 2 0 0 1 0 0 2 0 1 2 1 2 0
 0]
准确率是: 0.9736842105263158
# 决策树提供了predict_proba这个方法,发现这个方法,返回值要么是0,要么是1
model.predict_proba(X_test)
array([[1., 0., 0.],
       [0., 0., 1.],
       [0., 1., 0.],
       [1., 0., 0.],
       [0., 0., 1.],
       [0., 1., 0.],
       [1., 0., 0.],
       [0., 1., 0.],
       [0., 1., 0.],
       [0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 1.],
       [0., 0., 1.],
       [1., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.],
       [0., 1., 0.],
       [1., 0., 0.],
       [0., 0., 1.],
       [0., 1., 0.],
       [1., 0., 0.],
       [0., 1., 0.],
       [0., 1., 0.],
       [0., 0., 1.],
       [1., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.],
       [1., 0., 0.],
       [1., 0., 0.],
       [0., 0., 1.],
       [1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.],
       [0., 1., 0.],
       [0., 0., 1.],
       [1., 0., 0.],
       [1., 0., 0.]])

一图顶千言,绘制决策树

# 设置图片的尺寸
# 鸢尾花4个属性
iris = datasets.load_iris()

X = iris['data']
y = iris['target']
fn = iris['feature_names']
# 随机拆分
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state = 256)

# max_depth调整树深度:剪枝操作
# max_depth默认,深度最大,延伸到将数据完全划分开为止。
model = DecisionTreeClassifier(max_depth=None,criterion='entropy')
model.fit(X_train,y_train)
y_ = model.predict(X_test)
print('真实类别是:',y_test)
print('算法预测是:',y_)
print('准确率是:',model.score(X_test,y_test))
plt.figure(figsize=(12,18))
_ = tree.plot_tree(model,filled = True,feature_names=fn)
真实类别是: [0 2 1 0 2 1 0 1 1 1 2 2 2 0 0 1 2 1 0 2 1 0 1 1 2 0 0 1 0 0 2 0 2 2 1 2 0
 0]
算法预测是: [0 2 1 0 2 1 0 1 1 1 2 2 2 0 0 1 2 1 0 2 1 0 1 1 2 0 0 1 0 0 2 0 1 2 1 2 0
 0]
准确率是: 0.9736842105263158
output_4_1.png

对上面的决策树,进行剪枝

# 设置图片的尺寸
# 鸢尾花4个属性
iris = datasets.load_iris()

X = iris['data']
y = iris['target']
fn = iris['feature_names']
# 随机拆分
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state = 256)

# max_depth调整树深度:剪枝操作
# max_depth默认,深度最大,延伸到将数据完全划分开为止。
model = DecisionTreeClassifier(max_depth=2,criterion='entropy')
model.fit(X_train,y_train)
y_ = model.predict(X_test)
print('真实类别是:',y_test)
print('算法预测是:',y_)
print('准确率是:',model.score(X_test,y_test))
plt.figure(figsize=(6,9))
_ = tree.plot_tree(model,filled = True,feature_names=fn)
真实类别是: [0 2 1 0 2 1 0 1 1 1 2 2 2 0 0 1 2 1 0 2 1 0 1 1 2 0 0 1 0 0 2 0 2 2 1 2 0
 0]
算法预测是: [0 2 1 0 2 1 0 1 1 1 2 2 2 0 0 1 2 1 0 2 1 0 1 1 2 0 0 1 0 0 2 0 1 2 1 2 0
 0]
准确率是: 0.9736842105263158
output_6_1.png

上一篇 下一篇

猜你喜欢

热点阅读