zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 人工智能机器学习实战代码:岭回归

    import numpy as np
    import matplotlib.pyplot as plt
    
    from sklearn import datasets, linear_model
    from sklearn.model_selection import train_test_split
    
    def load_data():
        diabetes = datasets.load_diabetes()
        return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0)
    
    #岭回归
    def test_Ridge(*data):
        X_train,X_test,y_train,y_test=data
        regr = linear_model.Ridge()
        regr.fit(X_train, y_train)
        print('Coefficients:%s, intercept %.2f'%(regr.coef_,regr.intercept_))
        print("Residual sum of squares: %.2f"% np.mean((regr.predict(X_test) - y_test) ** 2))
        print('Score: %.2f' % regr.score(X_test, y_test))
    
    # 产生用于回归问题的数据集
    X_train,X_test,y_train,y_test=load_data() 
     # 调用 test_Ridge
    test_Ridge(X_train,X_test,y_train,y_test)
    
    def test_Ridge_alpha(*data):
        X_train,X_test,y_train,y_test=data
        alphas=[0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000]
        scores=[]
        for i,alpha in enumerate(alphas):
            regr = linear_model.Ridge(alpha=alpha)
            regr.fit(X_train, y_train)
            scores.append(regr.score(X_test, y_test))
        ## 绘图
        fig=plt.figure()
        ax=fig.add_subplot(1,1,1)
        ax.plot(alphas,scores)
        ax.set_xlabel(r"$alpha$")
        ax.set_ylabel(r"score")
        ax.set_xscale('log')
        ax.set_title("Ridge")
        plt.show()
        
    test_Ridge_alpha(X_train,X_test,y_train,y_test) # 调用 test_Ridge_alpha

  • 相关阅读:
    BZOJ 1391: [Ceoi2008]order
    BZOJ 4504: K个串
    2019 年百度之星·程序设计大赛
    POJ 2398 Toy Storage (二分 叉积)
    POJ 2318 TOYS (二分 叉积)
    HDU 6697 Closest Pair of Segments (计算几何 暴力)
    HDU 6695 Welcome Party (贪心)
    HDU 6693 Valentine's Day (概率)
    HDU 6590 Code (判断凸包相交)
    POJ 3805 Separate Points (判断凸包相交)
  • 原文地址:https://www.cnblogs.com/tszr/p/11177879.html
Copyright © 2011-2022 走看看