scikit learn之Elastic Net

弹性网络

弹性网络是一个lasso和ridge的结合
下图是官方文档的解释,感觉说的还是很清楚~
在这里插入图片描述

Lasso and Elastic Net

from itertools import cycle
from sklearn.linear_model import lasso_path,enet_path
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt 

diabetes = datasets.load_diabetes()
x = diabetes.data
y = diabetes.target
x = x/x.std(axis=0)#对于每个变量标准化,(easier to set the l1_ratio parameter)
eps = 5e-3 
alphas_lasso,coef_lasso,_ = lasso_path(x,y,eps,fit_intercept = False)
#The alphas and coefs along the path where models are computed.
#注意后面的这个_,是用来填补后面的变量滴
alphas_positive_lasso,coef_positive_lasso,_ = lasso_path(x,y,eps,positive=True
                                                         ,fit_intercept = False)
#强制把系数约束为正数
alphas_enet,coef_enet,_ = enet_path(x,y,eps=eps,l1_ratio=0.8,fit_intercept=False)

alphas_positive_enet,coef_positive_enet,_ = enet_path(x,y,eps=eps,
                                                      l1_ratio=0.8,
                                                      fit_intercept=False,
                                                      positive=True)
plt.figure(1)
colors = cycle(['b','r','g','c','k'])
neg_log_alphas_lasso = -np.log10(alphas_lasso)
neg_log_alphas_enet  = -np.log10(alphas_enet)
for coef_l,coef_e,c in zip(coef_lasso,coef_enet,colors):
    l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
    l2 = plt.plot(neg_log_alphas_enet, coef_e, linestyle='--', c=c)
plt.xlabel('-Log(alpha)')
plt.ylabel('coefficients')
plt.title('Lasso and Elastic-Net Paths')
plt.legend((l1[-1], l2[-1]), ('Lasso', 'Elastic-Net'), loc='lower left')
plt.axis('tight')  


plt.figure(2)
neg_log_alphas_positive_lasso = -np.log10(alphas_positive_lasso)
for coef_l, coef_pl, c in zip(coef_lasso, coef_positive_lasso, colors):
    l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
    l2 = plt.plot(neg_log_alphas_positive_lasso, coef_pl, linestyle='--', c=c)

plt.xlabel('-Log(alpha)')
plt.ylabel('coefficients')
plt.title('Lasso and positive Lasso')
plt.legend((l1[-1], l2[-1]), ('Lasso', 'positive Lasso'), loc='lower left')
plt.axis('tight')


plt.figure(3)
neg_log_alphas_positive_enet = -np.log10(alphas_positive_enet)
for (coef_e, coef_pe, c) in zip(coef_enet, coef_positive_enet, colors):
    l1 = plt.plot(neg_log_alphas_enet, coef_e, c=c)
    l2 = plt.plot(neg_log_alphas_positive_enet, coef_pe, linestyle='--', c=c)

plt.xlabel('-Log(alpha)')
plt.ylabel('coefficients')
plt.title('Elastic-Net and positive Elastic-Net')
plt.legend((l1[-1], l2[-1]), ('Elastic-Net', 'positive Elastic-Net'),
           loc='lower left')
plt.axis('tight')
plt.show()

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

猜你喜欢

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