1. from sklearn.metrics import mean_square_error
    2. def plot_learning_curve(algo,X_train,X_test,y_train,y_test): # algo的意思是学习器
    3. train_score = []
    4. test_score = []
    5. for i in range(1, len(X_train)+1):
    6. algo.fit(X_train[:i],y_train[:i])
    7. y_train_predict = alog.predict(X_train[:i])
    8. train_score.append(mean_square_error(y_train[:i], y_train_predict))
    9. y_test_predict = alog.predict(X_test)
    10. test_score.append(mean_square_error(y_test, y_test_predict))
    11. plt.plot([i for i in range(1,len(X_train)+1)], np.sqrt(train_score),label='train')
    12. plt.plot([i for i in range(1,len(X_test)+1)], np.sqrt(test_score),label='train')
    13. plt.legend() # 添加标签需要
    14. plt.axis([0, len(X_train)+1,0,4]) # 绘制范围,前面俩个是横轴,后面俩个是纵轴
    15. plt.show()
    16. plot_learning_curve(Linearregression(), X_train,X_test,y_train,y_test)