Lasso and Elastic Net for Sparse Signals:线性模型之套索和弹性网稀疏信号对比
来源:互联网 发布:澳门大学 知乎 编辑:程序博客网 时间:2024/05/25 18:09
这两个模型都是针对线性回归模型linear_model,区别在于使用了不同的损失函数或者不同的正则项函数
相关指数R2知识介绍
回归平方和+残差平方和=总偏差平方和
残差平方和=sum(y预测i-y观测i)^2
总偏差平方和=sum(y观测i-y观测平均)^2
回归平方和=sum(y预测i-y观测平均)^2
R2=1-残差平方和/总偏差平方和
import numpy as np # 数组库import matplotlib.pyplot as plt # 作图库from sklearn.metrics import r2_score # 使用R2相关指数作为模型指标 ,metrics:指标库# 构造数据集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 # sparsify coef 权重赋值y = np.dot(X, coef) # 构造训练的目标数据集# add noise 加入噪声y += 0.01 * np.random.normal((n_samples,)) # 构造有噪声的目标数据集# Split data in train set and test set # 分割出训练和测试集n_samples = X.shape[0]X_train, y_train = X[:n_samples / 2], y[:n_samples / 2] # 训练集x,yX_test, y_test = X[n_samples / 2:], y[n_samples / 2:] # 测试集x,y
Lasso:平方损失+L1范数
from sklearn.linear_model import Lassoalpha = 0.1lasso = 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)# 根据观测和预测得出R2print(lasso)print("r^2 on test data : %f" % r2_score_lasso)Out: 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:平方损失+L1和L2范数的混合使用
from sklearn.linear_model import ElasticNet #导入ElasticNet模块enet = ElasticNet(alpha=alpha, l1_ratio=0.7) # 导入ElasticNet模型y_pred_enet = enet.fit(X_train, y_train).predict(X_test)#训练并预测r2_score_enet = r2_score(y_test, y_pred_enet)#根据预测和观测得出R2print(enet)print("r^2 on test data : %f" % r2_score_enet)Out: 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.24017
Lasso和ElasticNet的在相同训练集下,训练出来的权重个数及大小,并与原有构造数据做数据可视化
plt.plot(lasso.coef_,color='lightgreen',linewidth=2,label='Lasso coefficients')# lasso每个权重的值为纵坐标,下标为横坐标(i,wi)i=1...200(200个特征)plt.plot(enet.coef_,color='gold',linewidth=2,label='Elastic net coefficients')# ElasticNet每个权重的值为纵坐标,下标为横坐标(i,wi)i=1...200(200个特征)plt.plot(coef,'--',color='navy',linewidth=2,label='Original coefficients')# 原始的每个权重的值为纵坐标,下标为横坐标(i,wi)i=1...200(200个特征)plt.xlabel('the index of coefficient')# 横轴为权重的位置索引plt.ylabel('the values of the index coefficient')# 纵轴为对应索引的权重的值plt.legend(loc='best') #图例plt.title("Lasso R^2: %f, Elastic Net R^2: %f" % (r2_score_lasso, r2_score_enet))plt.show()
注:个人笔记
0 0
- Lasso and Elastic Net for Sparse Signals:线性模型之套索和弹性网稀疏信号对比
- LASSO, sparse group LASSO, elastic net
- LASSO, sparse group LASSO, elastic net
- 弹性网络( Elastic Net) 多任务 Lasso回归 MultiTaskLasso
- Lasso和Elastic net的区别
- Ridge & LASSO & Elastic Net
- 回归的正则化模型:岭回归和Lasso回归(套索回归)
- 稀疏结构模型——套索模型、组套索、重叠组套索模型
- 计算机视觉和模式识别中的稀疏表示(Sparse Representation for Computer Vision and Pattern Recognition)
- 计算机视觉和模式识别中的稀疏表示(Sparse Representation for Computer Vision and Pattern Recognition)
- 3.Lasso线性模型
- 岭回归、LASSO与elastic net
- 《Sparse and Redundant Representations》第九章 稀疏搜索算法在信号处理中的应用
- 深入信号和槽(Signals and Slots in Depth)
- 深入信号和槽(Signals and Slots in Depth)
- QT的信号和槽机制:Signals and Slots
- 4.弹性网络( Elastic Net)
- 4.弹性网络( Elastic Net)
- CodeForces - 451B Sort the Array 简单模拟
- pgfincore安装
- 把标准时间改成一般时间
- Kafka安装及部署
- IllegalStateException: The specified child already has a parent问题解决办法
- Lasso and Elastic Net for Sparse Signals:线性模型之套索和弹性网稀疏信号对比
- 套接字Socket
- 结构体和联合体
- 三种方式清除浮动
- 使用apt安装pip时无法打开锁文件的解决办法
- 快速出现品牌关键词在百度360下拉框中进行推广
- python入门学习教程之List(列表)
- 匈牙利算法-二分图的最大匹配
- 并查集