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

    import numpy as np
    import matplotlib.pyplot as plt
    
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    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)
    
    #ElasticNet回归
    def test_ElasticNet(*data):
        X_train,X_test,y_train,y_test=data
        regr = linear_model.ElasticNet()
        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_ElasticNet
    test_ElasticNet(X_train,X_test,y_train,y_test)
    
    def test_ElasticNet_alpha_rho(*data):
        X_train,X_test,y_train,y_test=data
        alphas=np.logspace(-2,2)
        rhos=np.linspace(0.01,1)
        scores=[]
        for alpha in alphas:
                for rho in rhos:
                    regr = linear_model.ElasticNet(alpha=alpha,l1_ratio=rho)
                    regr.fit(X_train, y_train)
                    scores.append(regr.score(X_test, y_test))
        ## 绘图
        alphas, rhos = np.meshgrid(alphas, rhos)
        scores=np.array(scores).reshape(alphas.shape)
        fig=plt.figure()
        ax=Axes3D(fig)
        surf = ax.plot_surface(alphas, rhos, scores, rstride=1, cstride=1, cmap=cm.jet,linewidth=0, antialiased=False)
        fig.colorbar(surf, shrink=0.5, aspect=5)
        ax.set_xlabel(r"$alpha$")
        ax.set_ylabel(r"$
    ho$")
        ax.set_zlabel("score")
        ax.set_title("ElasticNet")
        plt.show()
        
    # 调用 test_ElasticNet_alpha_rho
    test_ElasticNet_alpha_rho(X_train,X_test,y_train,y_test)

  • 相关阅读:
    set转成toarray()
    list和set的拉拉扯扯的关系
    【转载】VNC和远程桌面的区别
    笔记本最小安装centos7 连接WiFi的方法
    mysql 索引优化 性能调优 锁
    PageHelper 自动去掉排序参数问题
    抽奖算法 百万次抽奖 单线程环境下 约 3.5 秒
    gitlab 安装和使用
    sharding sphere 分表分库 读写分离
    mycat 安装 分表 分库 读写分离
  • 原文地址:https://www.cnblogs.com/tszr/p/11177889.html
Copyright © 2011-2022 走看看