• 线性模型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()
  • 相关阅读:
    c++调用win32API控制打印机打印
    php socket 通信
    [SDOI2015][BZOJ3991] 寻宝游戏|set|dfs序|虚树|树上倍增LCA
    [NOI2015][BZOJ4195] 程序自动分析|并查集|离散化
    [NOI2015][BZOJ4196] 软件包管理器|树链剖分
    [HEOI2014][BZOJ3611] 大工程|虚树|树型dp|dfs序|树上倍增LCA
    [Usaco2007 Mar][BZOJ1638] Cow Traffic 奶牛交通|动态规划
    [HDU2222]Keywords Search|AC自动机
    [POI2007][BZOJ1103] 大都市meg|dfs序|树状数组
    [Usaco2007 Dec][BZOJ1690] 奶牛的旅行|分数规划|二分|SPFA
  • 原文地址:https://www.cnblogs.com/St-Lovaer/p/12245978.html
Copyright © 2020-2023  润新知