当前位置:   article > 正文

第六章.决策树(Decision Tree)—CART算法_cart决策树

cart决策树

第六章.决策树(Decision Tree)

6.2 CART算法

CART决策树的生成就是递归地构建二叉决策树的过程。CART用基尼(Gini)系数最小化准则来进行特征选择,生成二叉树

1.Gini系数计算:

在这里插入图片描述

2.CART示例

1).题干:

  • 分别计算它们的Gini系数增益,取Gini系数增益值最大的属性作为决策树的根节点属性。
    在这里插入图片描述

2).计算

①.根节点的Gini系数:
在这里插入图片描述
②.根据是否有房来进行划分:
在这里插入图片描述
③.根据婚姻状况来进行划分:
在这里插入图片描述

④.根据年收入进行划分:

  • 例如年收入为60和70这两个值时,我们算得其中间值为65。 倘若以中间值65作为分割点,则得Gini系数增益为 :
    请添加图片描述

  • 所有年收入的Gini系数增益:
    在这里插入图片描述

⑤.根据计算可知,三个属性划分根节点的增益最大的有两个:婚姻状况和年收入属性,他们的增益都是0.12,可以随机选择一个作为根节点。(以婚姻状况为根节点举例)

  • 婚姻状况作为根节点的Gini系数:
    在这里插入图片描述

  • 根据是否有房来进行划分:
    在这里插入图片描述

  • 根据年收入来进行划分:

    • 例如年收入为70和85这两个值时,中间值为77.5。 倘若以中间值77.5作为分割点,则得Gini系数增益为 :
      在这里插入图片描述
    • 所有年收入的Gini系数增益:
      在这里插入图片描述

⑥.最后构建的CART
在这里插入图片描述

3.剪枝

剪枝可以在一定程度上抑制过拟合的问题。

1).剪枝前

在这里插入图片描述

2).剪枝后

在这里插入图片描述

4.CART算法的优缺点

1).优点

①.小规模数据集有效

2).缺点

①.处理连续变量不好
②.类别较多时,错误增加的比较快
③.不能处理大量数据

5.决策树—CART

1).cart.csv中的数据

在这里插入图片描述

2).代码:

import numpy as np
from sklearn import tree
import graphviz
import csv

# 加载数据
DTree = open(r'D:\\Data\\cart.csv', 'r')
data_show = csv.reader(DTree)

# 属性名称
feature_names = data_show.__next__()
feature_names = np.array(feature_names)[1:-1]
print(feature_names)

# 标签名称
class_names = np.array(['no', 'yes'])
print(class_names)

data = np.genfromtxt('D:\\Data\\cart.csv', delimiter=',')

# 模型
x_data = data[1:, 1:-1]
print(x_data)

# 标签
y_data = data[1:, -1]
print(y_data)

# 创建决策树模型
model_DicisionTree = tree.DecisionTreeClassifier()
model_DicisionTree.fit(x_data, y_data)

# 导出决策树
dot_data = tree.export_graphviz(model_DicisionTree, out_file=None, feature_names=feature_names,
                                class_names=class_names, filled=True, rounded=True, special_characters=True)

graph = graphviz.Source(dot_data)
graph.render('cart')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

3).结果展示:

在这里插入图片描述

6.决策树—线性二分类示例

1)LR-testSet.csv中的数据:

2).代码:

import numpy as np
from sklearn import tree
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
import graphviz

# 加载数据
data = np.genfromtxt('D:\\Data\\LR-testSet.csv', delimiter=',')

x_data = data[:, :-1]
y_data = data[:, -1]

# 创建模型
model_DecisionTree = tree.DecisionTreeClassifier()
model_DecisionTree.fit(x_data, y_data)

predictions = model_DecisionTree.predict(x_data)
print(classification_report(y_data, predictions))

# 导出决策树
feature_names = np.array(['x', 'y'])
class_names = np.array(['label0', 'label1'])
dot_data = tree.export_graphviz(model_DecisionTree, out_file=None, feature_names=feature_names,
                                class_names=class_names, filled=True, rounded=True,
                                special_characters=True)

graph = graphviz.Source(dot_data)
graph.render('Linear_classification')

# 获取数据值所在范围
x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1
y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1].max() + 1

# 生成网格矩阵
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))

z = model_DecisionTree.predict(np.c_[xx.ravel(), yy.ravel()])
z = z.reshape(xx.shape)

# 绘制等高线
cs = plt.contourf(xx, yy, z)
plt.scatter(x_data[:, 0], x_data[:, 1], c=y_data)
plt.show()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

3).结果展示:

在这里插入图片描述

7.决策树—非线性二分类示例

1).CSV中的数据:

2).代码:

import numpy as np
from sklearn import tree
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import graphviz

# 加载数据
data = np.genfromtxt('D:\\Data\\LR-testSet2.txt', delimiter=',')

x_data = data[:, :-1]
y_data = data[:, -1]

# 分割数据
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data)

# 创建决策树模型
# max_depth,树的深度
# min_samples_split 内部节点再划分所需最小样本数
model_DecisionTree = tree.DecisionTreeClassifier(max_depth=7, min_samples_split=4)
model_DecisionTree.fit(x_train, y_train)

predictions = model_DecisionTree.predict(x_train)
print(classification_report(predictions, y_train))

predictions = model_DecisionTree.predict(x_test)
print(classification_report(predictions, y_test))

# 导出决策树
feature_names = np.array(['x', 'y'])
class_names = np.array(['label0', 'label1'])
dot_data = tree.export_graphviz(model_DecisionTree, out_file=None, feature_names=feature_names,
                                class_names=class_names, filled=True, rounded=True,
                                special_characters=True)

graph = graphviz.Source(dot_data)
graph.render('Linear_classification')

# 获取数据值所在范围
x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1
y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1].max() + 1

# 生成网格矩阵
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))

z = model_DecisionTree.predict(np.c_[xx.ravel(), yy.ravel()])
z = z.reshape(xx.shape)

# 绘制等高线
cs = plt.contourf(xx, yy, z)
plt.scatter(x_data[:, 0], x_data[:, 1], c=y_data)
plt.show()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

3).结果展示:

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/797445
推荐阅读
相关标签
  

闽ICP备14008679号