为每个分类器绘制图

时间:2018-07-08 20:53:42

标签: python matplotlib

这是我的kfolds代码

kf = KFold(class_label.shape[0], n_folds=5, shuffle=True). 
for train_index, test_index in kf:.
print("Train:", train_index, "Validation:",test_index). 
X_train, X_test = np.array(x)[train_index], np.array(x)[test_index]. 
y_train, y_test = np.array(class_label)[train_index], np.array(class_label)[test_index]

情节应该看起来像这样,但有10行 the plot should look like this but with 10 lines

我想为每一折画一条线,所以总共应该有十条线:

test_score = []. 
train_score = []. 
for depth in range(20):. 
    clf = DecisionTreeClassifier(max_depth = depth + 1). 
    clf.fit(X_train,y_train). 
    train_score.append(clf.score(X_train,y_train)). 
    test_score.append(clf.score(X_test,y_test)). 

plt.figure(figsize = (8,8)). 
plt.plot(range(20),train_score). 
plt.plot(range(20), test_score). 
plt.xlabel('Tree Depth'). 
plt.ylabel('Accuracy'). 
plt.legend(['Training set','Test set']). 

1 个答案:

答案 0 :(得分:0)

你能做这样的事情吗?在这里,我只是将循环嵌套在一起以得到列表列表,其中外部列表​​是折痕,内部列表是每个深度的每个折痕的分数。

kf = KFold(class_label.shape[0], n_folds=5, shuffle=True)
kf_test_scores = []
kf_train_scores = []
for train_index, test_index in kf:
    print("Train:", train_index, "Validation:",test_index)
    X_train, X_test = np.array(x)[train_index], np.array(x)[test_index].
    y_train, y_test = np.array(class_label)[train_index], np.array(class_label)[test_index]

    test_score = []
    train_score = []
    for depth in range(20):
        clf = DecisionTreeClassifier(max_depth = depth + 1)
        clf.fit(X_train,y_train)
        train_score.append(clf.score(X_train,y_train))
        test_score.append(clf.score(X_test,y_test))

    kf_test_scores.append(test_score)
    kf_train_scores.append(train_score)

plt.figure(figsize = (8,8))
for fold in range(len(kf_test_scores)):
    plt.plot(range(20), kf_train_scores[fold], label="Fold {0} Train Score"). 
    plt.plot(range(20), kf_test_scores[fold], label="Fold {0} TestScore"). 

plt.xlabel('Tree Depth'). 
plt.ylabel('Accuracy'). 
plt.legend(['Training set','Test set']).