scikitlearn之Lasso回归

lasso回归

基本语法

from sklearn import linear_model
reg = linear_model.Lasso(alpha=0.1)
reg.fit([[0,0],[1,1],[2,2]],[0,1,2])
reg.coef_ #coeficients
reg.intercept_
reg.predict([[1,1]])

和之前的线性回归之类的都很像哦~

Lasso and Elastic Net for Sparse Signals

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#五十个样本量,200个变量
x = np.random.randn(n_samples,n_features)#返回一个50*200的矩阵,每个数字都是来自标准正态分布的
#np.random.randn(d1,d2,d3...dn)括号里的都是维度The dimensions of the returned array, 
coef = 3*np.random.randn(n_features)
inds= np.arange(n_features)#生成一个数字索引,个数是变量的个数
np.random.shuffle(inds)#把这个索引打乱
coef[inds[10:]] = 0#挑选了10个变量的系数不是零
y = np.dot(x,coef)#参考文件,矩阵相乘
#和y2 = np.matmul(x,coef)是一样的
y+= 0.01*np.random.randn(n_samples)#增加一个扰动项
n_samples = x.shape[0] 
X_train,X_test = x[:n_samples//2],x[n_samples//2:]
Y_train,Y_test = y[:n_samples//2],y[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)
# ElasticNet
from sklearn.linear_model import ElasticNet
enet = ElasticNet(alpha=alpha,l1_ratio=0.7)
#The ElasticNet mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an L2 penalty. For l1_ratio = 1 it is an L1 penalty.
# For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.
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)
#plot
plt.plot(enet.coef_,color = 'lightgreen',label='elastic')
plt.plot(lasso.coef_,color = 'gold',label='lasso')
plt.plot(coef,'--',color='navy',label='original')#用虚线画图
plt.legend(loc='best')#图例
plt.title("Lasso R^2: %f, Elastic Net R^2: %f"
          % (r2_score_Lasso, r2_score_enet))

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43451186/article/details/86535607