“learning curve” implemented in Python sklearn

Home / Uncategorized / “learning curve” implemented in Python sklearn

Question:
I got codes from a net school teacher, the content is about a Kaggle "Titanic Example" ML project, and there is one about "learning curve" which is implemented in Python sklearn and its purpose is for model fitted status recognization. However, it came up
IndexError:too many indices for array

Could someone give any advice about this?
import numpy as np
import matplotlib.pyplot as plt
from sklearn.learning_curve import learning_curve

# use sklearn learning_curve to get training_score和cv_score,use matplotlib to plot learning curve
def plot_learning_curve(estimator, title, x, y, ylim=None, cv=None, n_jobs=1,
train_sizes=np.linspace(.05, 1., 20), verbose=0, plot=True):train_sizes, train_scores, test_scores = learning_curve(
estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes, verbose=verbose)

train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)

if plot:plt.figure()
plt.title(title)
if ylim is not None:plt.ylim(*ylim)
plt.xlabel(u"train set size")
plt.ylabel(u"score")
plt.gca().invert_yaxis()
plt.grid()

plt.fill_between(train_sizes, train_scores_mean – train_scores_std, train_scores_mean + train_scores_std,
alpha=0.1, color="b")
plt.fill_between(train_sizes, test_scores_mean – test_scores_std, test_scores_mean + test_scores_std,
alpha=0.1, color="r")
plt.plot(train_sizes, train_scores_mean, ‘o-‘, color="b", label=u"train set score")
plt.plot(train_sizes, test_scores_mean, ‘o-‘, color="r", label=u"cv score")

plt.legend(loc="best")
plt.draw()

plt.gca().invert_yaxis()
plt.show()

midpoint = ((train_scores_mean[-1] + train_scores_std[-1]) + (test_scores_mean[-1] – test_scores_std[-1])) / 2
diff = (train_scores_mean[-1] + train_scores_std[-1]) – (test_scores_mean[-1] – test_scores_std[-1])
return midpoint, diff

plot_learning_curve(clf, u"learning_curve", x, y)


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *