【问题标题】:Plotting Learning Curves [Scitkit Learn] - How to set x-axis values/labels?绘制学习曲线 [Scikit Learn] - 如何设置 x 轴值/标签?
【发布时间】:2017-08-11 11:38:02
【问题描述】:

我正在尝试使用它来绘制学习曲线。

http://scikit-learn.org/0.15/auto_examples/plot_learning_curve.html.

我想查看一组固定的训练规模。

所以在我手动设置的 plot_learning_curve 函数中。训练规模为 [10, 500, 1000, 2500, 5000]。但是,x 轴不会更新以在 x 轴上显示这些特定值。

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                    n_jobs=1):

train_sizes = [10, 500, 1000, 2500, 5000]

plt.figure()
plt.title(title)
print(ylim)
if ylim is not None:
    plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(
    estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
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)
plt.grid()


print(train_sizes)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                 train_scores_mean + train_scores_std, alpha=0.1,
                 color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                 test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
         label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
         label="Cross-validation score")

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

Image

你能看到它仍然显示 1000、2000、3000、4000、5000

【问题讨论】:

  • train_sizes 不是轴标签参数,而是要绘制的实际数据。您应该注意您发布的图片中的红色和绿色圆圈。它们对应于您指定的内容。

标签: python matplotlib scikit-learn


【解决方案1】:

使用xticks 参数

plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
         label="Cross-validation score", xticks = train_sizes)

【讨论】:

    猜你喜欢
    • 2020-11-15
    • 1970-01-01
    • 2020-04-11
    • 2012-05-28
    • 2016-08-24
    • 1970-01-01
    • 2016-03-12
    • 2016-11-29
    • 2016-10-06
    相关资源
    最近更新 更多