美文网首页
sklearn 学习5

sklearn 学习5

作者: l_b_n | 来源:发表于2017-05-20 15:36 被阅读0次

    gamma值与loss

    import numpy as np
    from sklearn.datasets import load_digits
    from sklearn.cross_validation import train_test_split
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.cross_validation import cross_val_score
    import matplotlib.pyplot as plt
    from sklearn.svm import SVC 
    from sklearn.learning_curve import validation_curve
    
    
    digits = load_digits()
    X = digits.data
    y = digits.target
    #设定gamma的变化范围
    param_range = np.logspace(-6,-2.3,5)
    
    train_loss,test_loss = validation_curve(
            SVC(),X,y,param_name = 'gamma',param_range = param_range,
            cv = 10,scoring = 'mean_squared_error')
    
    train_loss_mean = -np.mean(train_loss,axis = 1)
    test_loss_mean = -np.mean(test_loss,axis = 1)
    
    plt.plot(param_range,train_loss_mean,'o-',color = 'r',label = 'training')
    plt.plot(param_range,test_loss_mean,'o-',color = 'g',label = 'test')
    plt.legend(loc = 'best')
    plt.show()
    
    
    gamma与loss

    x-axis : gamma
    y_axis: loss

    其中也体现了过拟合:在training data上的过分好

    相关文章

      网友评论

          本文标题:sklearn 学习5

          本文链接:https://www.haomeiwen.com/subject/xarpxxtx.html