简体   繁体   中英

How to plot multiple learning curve from different model on the same graph?

I have few models that I have trained, and wanted to plot the learning curve of each model on a single graph

I tried this, and worked. But it felt redundant.

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

Because I need to repeat the train_scores and test_scores for each model .

I tried it using for loop.

First , I saved the models in an array.

arr = [m1,m2,m3]

But when I started the for loop, it only produced a single line on the graph.

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")

Here is the desired output

期望输出

Will someone show me what am I lacking ? Your time is deeply appreciated.

i can't spot anything wrong with what you are doing.. This works for me (taken in part from 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM