zoukankan      html  css  js  c++  java
  • 线性模型L1正则化——套索回归

     1 from sklearn.model_selection import train_test_split
     2 from sklearn.datasets import load_diabetes
     3 X,y=load_diabetes().data,load_diabetes().target
     4 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8)
     5 
     6 from sklearn.linear_model import Lasso
     7 import numpy as np
     8 lasso=Lasso().fit(X_train,y_train)
     9 print("the coefficient:{}".format(lasso.coef_))
    10 print('the intercept:{}'.format(lasso.intercept_))
    11 print("the score of this model:{:.3f}".format(lasso.score(X_test,y_test)))
    12 print("the model uses {}".format(np.sum(lasso.coef_!=0))+" features
    ")
     1 lasso01=Lasso(alpha=0.1,max_iter=100000).fit(X_train,y_train)
     2 print("the coefficient:{}".format(lasso01.coef_))
     3 print('the intercept:{}'.format(lasso01.intercept_))
     4 print("the score of this model:{:.3f}".format(lasso01.score(X_test,y_test)))
     5 print("the model uses {}".format(np.sum(lasso01.coef_!=0))+" features
    ")
     6 
     7 lasso001=Lasso(alpha=0.01,max_iter=100000).fit(X_train,y_train)
     8 print("the coefficient:{}".format(lasso001.coef_))
     9 print('the intercept:{}'.format(lasso001.intercept_))
    10 print("the score of this model:{:.3f}".format(lasso001.score(X_test,y_test)))
    11 print("the model uses {}".format(np.sum(lasso001.coef_!=0))+" features
    ")
    12 
    13 lasso0001=Lasso(alpha=0.001,max_iter=100000).fit(X_train,y_train)
    14 print("the coefficient:{}".format(lasso0001.coef_))
    15 print('the intercept:{}'.format(lasso0001.intercept_))
    16 print("the score of this model:{:.3f}".format(lasso0001.score(X_test,y_test)))
    17 print("the model uses {}".format(np.sum(lasso0001.coef_!=0))+" features
    ")
    18 
    19 lasso00001=Lasso(alpha=0.0001,max_iter=100000).fit(X_train,y_train)
    20 print("the coefficient:{}".format(lasso00001.coef_))
    21 print('the intercept:{}'.format(lasso00001.intercept_))
    22 print("the score of this model:{:.3f}".format(lasso00001.score(X_test,y_test)))
    23 print("the model uses {}".format(np.sum(lasso00001.coef_!=0))+" features
    ")
     1 import matplotlib.pyplot as plt
     2 plt.plot(lasso.coef_,'s',label='Lasso alpha=1')
     3 plt.plot(lasso01.coef_,'^',label='Lasso alpha=0.1')
     4 plt.plot(lasso001.coef_,'v',label='Lasso alpha=0.01')
     5 plt.plot(lasso0001.coef_,'o',label='Lasso alpha=0.001')
     6 plt.plot(lasso00001.coef_,'*',label='Lasso alpha=0.0001')
     7 plt.xlabel("coeffient index")
     8 plt.ylabel("coeffient magnitude")
     9 plt.legend(loc=(0,1.05))
    10 plt.show()
  • 相关阅读:
    初赛—算法复杂度
    2-SAT
    最小生成树
    18-短信验证码接口
    17-腾讯云短信开发
    16-手机号是否存在验证接口
    15-多方式登录
    14-登录注册页面
    13-Git
    12-跨域请求详解
  • 原文地址:https://www.cnblogs.com/St-Lovaer/p/12245978.html
Copyright © 2011-2022 走看看