决策树 Decision_trees - Ex 3: Plot the decision surface of a decision tree on the iris dataset

优质
小牛编辑
130浏览
2023-12-01

决策树/范例三: Plot the decision surface of a decision tree on the iris dataset

http://scikit-learn.org/stable/auto_examples/tree/plot_iris.html

此范例利用决策树分类器将资料集进行分类,找出各类别的分类边界。以鸢尾花资料集当作范例,每次取两个特征做训练,个别绘制不同品种的鸢尾花特征的分布范围。对于每对的鸢尾花特征,决策树学习推断出简单的分类规则,构成决策边界。

范例目的:

  1. 资料集:iris 鸢尾花资料集
  2. 特征:鸢尾花特征
  3. 预测目标:是哪一种鸢尾花
  4. 机器学习方法:decision tree 决策树

(一)引入函式库及内建测试资料库

  • from sklearn.datasets import load_iris将鸢尾花资料库存入,iris为一个dict型别资料。
  • 每笔资料中有4个特征,一次取2个特征,共有6种排列方式。
  • X (特征资料) 以及 y (目标资料)。
  • DecisionTreeClassifier 建立决策树分类器。
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.datasets import load_iris
  4. from sklearn.tree import DecisionTreeClassifier
  5. iris = load_iris()
  6. for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3],
  7. [1, 2], [1, 3], [2, 3]]):
  8. X = iris.data[:, pair]
  9. y = iris.target

(二)建立Decision Tree分类器

建立模型及分类器训练

  • DecisionTreeClassifier():决策树分类器。
  • fit(特征资料, 目标资料):利用特征资料及目标资料对分类器进行训练。
  1. clf = DecisionTreeClassifier().fit(X, y)

(三)绘制决策边界及训练点

  • np.meshgrid:利用特征之最大最小值,建立预测用网格 xx, yy
  • clf.predict:预估分类结果。
  • plt.contourf:绘制决策边界。
  • plt.scatter(X,y):将X、y以点的方式绘制于平面上,c为数据点的颜色,label为图例。
  1. plt.subplot(2, 3, pairidx + 1)
  2. x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
  3. y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  4. xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
  5. np.arange(y_min, y_max, plot_step))
  6. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) #np.c_ 串接两个list,np.ravel将矩阵变为一维
  7. Z = Z.reshape(xx.shape)
  8. cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
  9. plt.xlabel(iris.feature_names[pair[0]])
  10. plt.ylabel(iris.feature_names[pair[1]])
  11. plt.axis("tight")
  12. for i, color in zip(range(n_classes), plot_colors):
  13. idx = np.where(y == i)
  14. plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],
  15. cmap=plt.cm.Paired)
  16. plt.axis("tight")

Ex 3: Plot the decision surface of a decision tree on the iris dataset - 图1

(四)完整程式码

  1. print(__doc__)
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn.datasets import load_iris
  5. from sklearn.tree import DecisionTreeClassifier
  6. # Parameters
  7. n_classes = 3
  8. plot_colors = "bry"
  9. plot_step = 0.02
  10. # Load data
  11. iris = load_iris()
  12. for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3],
  13. [1, 2], [1, 3], [2, 3]]):
  14. # We only take the two corresponding features
  15. X = iris.data[:, pair]
  16. y = iris.target
  17. # Train
  18. clf = DecisionTreeClassifier().fit(X, y)
  19. # Plot the decision boundary
  20. plt.subplot(2, 3, pairidx + 1)
  21. x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
  22. y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  23. xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
  24. np.arange(y_min, y_max, plot_step))
  25. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) #np.c_ 串接两个list,np.ravel将矩阵变为一维
  26. Z = Z.reshape(xx.shape)
  27. cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
  28. plt.xlabel(iris.feature_names[pair[0]])
  29. plt.ylabel(iris.feature_names[pair[1]])
  30. plt.axis("tight")
  31. # Plot the training points
  32. for i, color in zip(range(n_classes), plot_colors):
  33. idx = np.where(y == i)
  34. plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],
  35. cmap=plt.cm.Paired)
  36. plt.axis("tight")
  37. plt.suptitle("Decision surface of a decision tree using paired features")
  38. plt.legend()
  39. plt.show()