2017-08-11 54 views
0

我試圖用這個繪製一條學習曲線。繪製學習曲線[Scitkit學習] - 如何設置x軸值/標籤?

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

+0

'train_sizes'不是軸標籤參數,但要繪製的實際數據。你應該注意你張貼的圖像中的紅色和綠色圓圈。它們與您指定的內容相對應。 –

回答

0

使用xticks參數

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