zoukankan      html  css  js  c++  java
  • 吴裕雄 python 机器学习——模型选择学习曲线learning_curve模型

    import numpy as np
    import matplotlib.pyplot as plt
    
    from sklearn.svm import LinearSVC
    from sklearn.datasets import load_digits
    from sklearn.model_selection import learning_curve
    
    #模型选择学习曲线learning_curve模型
    def test_learning_curve():
        ### 加载数据
        digits = load_digits()
        X,y=digits.data,digits.target
        #### 获取学习曲线 ######
        train_sizes=np.linspace(0.1,1.0,endpoint=True,dtype='float')
        abs_trains_sizes,train_scores, test_scores = learning_curve(LinearSVC(),X, y,cv=10, scoring="accuracy",train_sizes=train_sizes)
        ###### 对每个 C ,获取 10 折交叉上的预测得分上的均值和方差 #####
        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)
        ####### 绘图 ######
        fig=plt.figure()
        ax=fig.add_subplot(1,1,1)
    
        ax.plot(abs_trains_sizes, train_scores_mean, label="Training Accuracy", color="r")
        ax.fill_between(abs_trains_sizes, train_scores_mean - train_scores_std,train_scores_mean + train_scores_std, alpha=0.2, color="r")
        ax.plot(abs_trains_sizes, test_scores_mean, label="Testing Accuracy", color="g")
        ax.fill_between(abs_trains_sizes, test_scores_mean - test_scores_std,test_scores_mean + test_scores_std, alpha=0.2, color="g")
    
        ax.set_title("Learning Curve with LinearSVC")
        ax.set_xlabel("Sample Nums")
        ax.set_ylabel("Score")
        ax.set_ylim(0,1.1)
        ax.legend(loc='best')
        plt.show()
        
    #调用test_learning_curve()
    test_learning_curve()

  • 相关阅读:
    Win10怎么默认用Windows照片查看程序打开图片
    Class类文件结构——访问标志
    栈的压入弹出序列
    Java中length,length()以及size()的区别
    包含min函数的栈
    extends和implements的区别
    顺时针打印矩阵
    二叉树的镜像
    Class类文件结构解读——常量池
    进制、字(word)、字节(byte)、位(bit)的关系
  • 原文地址:https://www.cnblogs.com/tszr/p/10802374.html
Copyright © 2011-2022 走看看