zoukankan      html  css  js  c++  java
  • 吴裕雄 python 机器学习——模型选择验证曲线validation_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 validation_curve
    
    #模型选择验证曲线validation_curve模型
    def test_validation_curve():
        '''
        测试 validation_curve 的用法 。验证对于 LinearSVC 分类器 , C 参数对于预测准确率的影响
        '''
        ### 加载数据
        digits = load_digits()
        X,y=digits.data,digits.target
        #### 获取验证曲线 ######
        param_name="C"
        param_range = np.logspace(-2, 2)
        train_scores, test_scores = validation_curve(LinearSVC(), X, y, param_name=param_name,param_range=param_range,cv=10, scoring="accuracy")
        ###### 对每个 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.semilogx(param_range, train_scores_mean, label="Training Accuracy", color="r")
        ax.fill_between(param_range, train_scores_mean - train_scores_std,train_scores_mean + train_scores_std, alpha=0.2, color="r")
        ax.semilogx(param_range, test_scores_mean, label="Testing Accuracy", color="g")
        ax.fill_between(param_range, test_scores_mean - test_scores_std,test_scores_mean + test_scores_std, alpha=0.2, color="g")
    
        ax.set_title("Validation Curve with LinearSVC")
        ax.set_xlabel("C")
        ax.set_ylabel("Score")
        ax.set_ylim(0,1.1)
        ax.legend(loc='best')
        plt.show()
        
    #调用test_validation_curve()
    test_validation_curve()

  • 相关阅读:
    phonegap 捕获图片,音频,视屏 api capture
    phonegap的照相机 API
    phonegap 的指南针 api Compass
    PhoneGap Geolocation结合百度地图api获取地理位置api
    PhoneGap Geolocation 获取地理位置 api
    PhoneGap实现重力感应
    PhoneGap模仿微信摇一摇功能
    75-扩展GCD-时间复杂度
    15- 1 << k 时的益出
    6-画图
  • 原文地址:https://www.cnblogs.com/tszr/p/10802635.html
Copyright © 2011-2022 走看看