[关闭]
@hanxiaoyang 2016-10-26T00:02:14.000000Z 字数 1373 阅读 6344

plot learning curve

机器学习


  1. from sklearn.svm import LinearSVC
  2. from sklearn.learning_curve import learning_curve
  3. #绘制学习曲线,以确定模型的状况
  4. def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
  5. train_sizes=np.linspace(.1, 1.0, 5)):
  6. """
  7. 画出data在某模型上的learning curve.
  8. 参数解释
  9. ----------
  10. estimator : 你用的分类器。
  11. title : 表格的标题。
  12. X : 输入的feature,numpy类型
  13. y : 输入的target vector
  14. ylim : tuple格式的(ymin, ymax), 设定图像中纵坐标的最低点和最高点
  15. cv : 做cross-validation的时候,数据分成的份数,其中一份作为cv集,其余n-1份作为training(默认为3份)
  16. """
  17. plt.figure()
  18. train_sizes, train_scores, test_scores = learning_curve(
  19. estimator, X, y, cv=5, n_jobs=1, train_sizes=train_sizes)
  20. train_scores_mean = np.mean(train_scores, axis=1)
  21. train_scores_std = np.std(train_scores, axis=1)
  22. test_scores_mean = np.mean(test_scores, axis=1)
  23. test_scores_std = np.std(test_scores, axis=1)
  24. plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
  25. train_scores_mean + train_scores_std, alpha=0.1,
  26. color="r")
  27. plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
  28. test_scores_mean + test_scores_std, alpha=0.1, color="g")
  29. plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
  30. label="Training score")
  31. plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
  32. label="Cross-validation score")
  33. plt.xlabel("Training examples")
  34. plt.ylabel("Score")
  35. plt.legend(loc="best")
  36. plt.grid("on")
  37. if ylim:
  38. plt.ylim(ylim)
  39. plt.title(title)
  40. plt.show()
  41. #少样本的情况情况下绘出学习曲线
  42. plot_learning_curve(LinearSVC(C=10.0), "LinearSVC(C=10.0)",
  43. X, y, ylim=(0.8, 1.01),
  44. train_sizes=np.linspace(.05, 0.2, 5))
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注