如何在同一张图上绘制来自不同模型的多个学习曲线?

时间:2019-03-28 06:33:51

标签: python matplotlib machine-learning graph

我训练的模型很少,并希望将每个模型的学习曲线绘制在一张图上

我尝试了这个,然后工作了。但是感觉很多余。

train_sizes, train_scores, test_scores = learning_curve(model, 
                                                        train_dummies, 
                                                        y,
                                                        cv=5,
                                                      scoring='neg_mean_squared_error')

因为我需要为每个模型重复 train_scores test_scores

我使用 for 循环进行了尝试。

首先,我将模型保存在数组中。

arr = [m1,m2,m3]

但是当我启动 for 循环时,它只在图形上产生了一条直线。

for i in arr:
  train_sizes, train_scores, test_scores = learning_curve(i, 
                                                    train_dummies, 
                                                    y,
                                                    cv=5,
                                              scoring='neg_mean_squared_error')
  train_mean = np.mean(train_scores, axis=1)
  train_std = np.std(train_scores, axis=1)

  test_mean = np.mean(test_scores, axis=1)
  test_std = np.std(test_scores, axis=1)


  plt.plot(train_sizes, test_mean, label="Cross-validation score")

这是所需的输出

Desired output

有人会告诉我我缺少什么吗?您的时间深表感谢。

1 个答案:

答案 0 :(得分:1)

我无法发现您所做的任何事情。。这对我有用(部分摘自here):

import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn.model_selection import learning_curve

digits = load_digits()
X, y = digits.data, digits.target
for i in [GaussianNB(), SVC(gamma=0.001)]:
    (train_sizes,
     train_scores,
     test_scores) = learning_curve(i, X, y, cv=5)
    test_mean = np.mean(test_scores, axis=1)
    plt.plot(train_sizes, test_mean, label="Cross-validation score")

plt.legend()
plt.show()