本文主要是介绍画学习曲线的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import matplotlib.pyplot as plt
import numpy as np
import pandas as pdfrom sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import learning_curve
from sklearn.neighbors import KNeighborsClassifier#加载数据
data=pd.read_csv(r"D:\Desktop\data\59024 scikit-learn机器学习源码_20181031\code\datasets\pima-indians-diabetes\diabetes.csv")#from common.utils import plot_learning_curve
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):plt.figure()plt.title(title)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()# 生成网格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 pltX=data.iloc[:,0:8]
y=data.iloc[:,-1]#pandas对象加iloc,数组不用加
knn=KNeighborsClassifier(n_neighbors=2)cv=ShuffleSplit(n_splits=10,test_size=0.2,random_state=0)
plt.figure(figsize=(16,10),dpi=200)
plot_learning_curve(knn,"Learn Curve for KNN Diabetes",X,y,ylim=(0.0,1.01),cv=cv)
学习曲线说明:
横坐标表示训练样本的数量,纵坐标表示准确率
1:观察左上图,训练集准确率与验证集准确率收敛,但是两者收敛后的准确率远小于我们的期望准确率(上面那条红线),所以由图可得该模型属于欠拟合-高偏差(underfitting)问题。由于欠拟合,所以我们需要增加模型的复杂度,比如,增加特征、增加树的深度、减小正则项等等,此时再增加数据量是不起作用的。
2:观察右上图,训练集准确率高于期望值,验证集则低于期望值,两者之间有很大的间距,误差很大,对于新的数据集模型适应性较差,所以由图可得该模型属于过拟合-高方差(overfitting)问题。由于过拟合,所以我们降低模型的复杂度,比如减小树的深度、增大分裂节点样本数、增大样本数、减少特征数等等。
3:一个比较理想的学习曲线图应当是:低偏差、低方差,即收敛且误差小。
这篇关于画学习曲线的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!