【发布时间】:2020-04-02 22:26:17
【问题描述】:
我需要做一个决策树,并通过图表上的标签来表示数据(如两个插图所示)。我对决策树没有问题,不幸的是,图中没有输入点。我在代码中尝试了几处更改。 代码来自scikit learn网站Plot the decision surface of a decision tree on the iris dataset
下面有一个使用的数据示例(X、Y、C5)(来自一个 excel 文件):
Path = "Documents/Apprentissage/Python/Script/ClustAllRepres12.xlsx"
Wordbook = xlrd.open_workbook(Path)
Sheet = Wordbook.sheet_by_index(0)
X=[]
Y=[]
C5=[]
for i in range(1, Sheet.nrows):
X.append(Sheet.cell_value(i, 0))
Y.append(Sheet.cell_value(i, 1))
C5.append(Sheet.cell_value(i, 8))
X
输出[]: [8.0, 9.0, 9.0, 9.0, 9.0, 10.0, 10.0, 11.0, 11.0, 11.0, 11.0, 11.0, 12.0, 12.0, 12.0, 12.0, 12.0, 13.0, 13.0, 13.0, 14.0, 14.0, 14.0, 15.0, 15.0, 15.0, 15.0, 16.0, 16.0, 16.0, 16.0, 17.0, 17.0, 17.0, 17.0, 18.0, 18.0, 18.0, 18.0, 18.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 20.0, 21.0, 21.0, 22.0]
type(X)
输出[]:列表
Y
输出[]: [45.0, 17.0, 18.0, 24.0, 25.0, 27.0, 36.0, 38.0, 39.0, 24.0, 37.0, 40.0, 24.0, 31.0, 35.0, 36.0, 37.0, 39.0, 32.0, 33.0, 35.0, 43.0, 27.0, 31.0, 35.0, 42.0, 18.0, 39.0, 43.0, 31.0, 42.0, 28.0, 32.0, 35.0, 43.0, 51.0, 52.0, 17.0, 19.0, 53.0, 49.0, 51.0, 53.0, 58.0, 16.0, 58.0, 59.0, 50.0, 52.0, 54.0]
type(Y)
输出[]:列表
C5
输出[]: [2.0, 4.0, 3.0, 3.0, 3.0, 4.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 1.0, 4.0, 1.0, 3.0, 1.0, 1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 1.0, 5.0, 1.0, 3.0, 1.0, 1.0, 4.0, 4.0, 4.0, 1.0, 5.0, 1.0, 5.0, 2.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0]
type(C5)
输出[]:列表
有来自 scikit learn 网站的代码:
import pandas as pd
import numpy as np
import xlrd
import matplotlib.pyplot as plt
df_list = pd.DataFrame(
{'X': X,
'Y': Y,
})
df = df_list.iloc[:,0:2].values #transform to float type
import sklearn
from sklearn.tree import DecisionTreeClassifier, plot_tree
#parameters
n_classes = 5
plot_colors = "ryb"
plot_step = 0.02
for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3],[1, 2], [1, 3], [2, 3]]):
# We only take the two corresponding features
X = df #gives better result without [:, pair]
y = C5
#train
clf = DecisionTreeClassifier().fit(X, y)
#plot the decision boundary
plt.subplot(2, 3, pairidx +1)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))
plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.RdYlBu)
# Plot the training points
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color, label=C5[i],
cmap=plt.cm.RdYlBu, edgecolor='black', s=15)
plt.suptitle("Decision surface of a decision tree using paired features")
plt.legend(loc='lower right', borderpad=0, handletextpad=0)
plt.axis("tight")
plt.figure()
clf = DecisionTreeClassifier().fit(df_list, C5)
plot_tree(clf, filled=True)
plt.show()
这是我的图表:
如您所见,图中未显示点。 我举一个等待结果的例子:
【问题讨论】:
-
请记住,最好的问题包含可重复的示例,但所讨论的示例也很少。
-
那么你的问题到底是什么?
-
您必须将您的问题缩小到一件特定的事情。最早的部分哪里出了问题/不完整?
-
我已经提出了这个问题,我还扩展了数据集@PiRocks
-
问题是输出图中没有点(我添加了输出图片)@NicolasGervais
标签: python decision-tree graph-visualization