• Sklearn库例子4:分类——Lasso分类例子


    Lasso回归:

    #-*- encoding:utf-8 -*-
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.metrics import r2_score
    
    np.random.seed(42)
    n_samples,n_features=50,200
    X=np.random.randn(n_samples,n_features)
    coef=3*np.random.randn(n_features)
    inds=np.arange(n_features)
    np.random.shuffle(inds)
    coef[inds[10:]]=0
    y=np.dot(X,coef)
    
    ##添加噪音:
    y+=0.01*np.random.normal((n_samples,))
    ####分离数据:
    n_samples=X.shape[0]
    X_train,y_train=X[:int(n_samples/2)],y[:int(n_samples/2)]
    X_test,y_test=X[int(n_samples/2):],y[int(n_samples/2):]
    
    ###Lasso
    from sklearn.linear_model import Lasso
    
    alpha=0.1
    lasso=Lasso(alpha=alpha)
    y_pred_lasso=lasso.fit(X_train,y_train).predict(X_test)
    r2_score_lasso=r2_score(y_test,y_pred_lasso)
    print(lasso)
    print('r^2 on test data:%f' %r2_score_lasso)
    
    
    from sklearn.linear_model import ElasticNet
    enet=ElasticNet(alpha=alpha,l1_ratio=0.7)
    y_pred_enet=enet.fit(X_train,y_train).predict(X_test)
    r2_score_enet=r2_score(y_test,y_pred_enet)
    print(enet)
    print('r^2 on test data:%f' %r2_score_enet)
    
    plt.plot(enet.coef_,label='Elastic net coefficients')
    plt.plot(lasso.coef_,label='Lasso coefficients')
    plt.plot(coef,'--',label='orginal coefficients' )
    plt.legend(loc='best')
    plt.title('Lasso R^2:%f,Elastic Net R^2:%f' %(r2_score_lasso,r2_score_enet))
    plt.show()
    

     输出:

    Lasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000,
       normalize=False, positive=False, precompute=False, random_state=None,
       selection='cyclic', tol=0.0001, warm_start=False)
    r^2 on test data:0.384710
    ElasticNet(alpha=0.1, copy_X=True, fit_intercept=True, l1_ratio=0.7,
          max_iter=1000, normalize=False, positive=False, precompute=False,
          random_state=None, selection='cyclic', tol=0.0001, warm_start=False)
    r^2 on test data:0.240176

  • 相关阅读:
    (转)剖析Delphi中的构造和析构
    求排列组合
    用链表写的猴子选大王
    查找文件
    在Delphi程序中应用IE浏览器控件
    汉字转UNICODE?
    webbrowser去掉边框
    自己写的猴子选大王
    数据库IDE查询实例
    Compiz Check测试Linux桌面3D兼容性
  • 原文地址:https://www.cnblogs.com/itdyb/p/5845563.html
Copyright © 2020-2023  润新知