ElasticNet 是一种使用L1和L2先验作为正则化矩阵的线性回归模型.这种组合用于只有很少的权重非零的稀疏模型,比如:class:Lasso, 但是又能保持:class:Ridge 的正则化属性.我们可以使用 l1_ratio 参数来调节L1和L2的凸组合(一类特殊的线性组合)。
当多个特征和另一个特征相关的时候弹性网络非常有用。Lasso 倾向于随机选择其中一个,而弹性网络更倾向于选择两个.
在实践中,Lasso 和 Ridge 之间权衡的一个优势是它允许在循环过程(Under rotate)中继承 Ridge 的稳定性.
弹性网络的目标函数是最小化:
ElasticNetCV 可以通过交叉验证来用来设置参数 alpha
()
和 l1_ratio
()
- print(__doc__)
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.linear_model import lasso_path, enet_path
- from sklearn import datasets
- diabetes = datasets.load_diabetes()
- X = diabetes.data
- y = diabetes.target
- X /= X.std(axis=0) # Standardize data (easier to set the l1_ratio parameter)
- # Compute paths
- eps = 5e-3 # the smaller it is the longer is the path
- print("Computing regularization path using the lasso...")
- alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False)
- print("Computing regularization path using the positive lasso...")
- alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(
- X, y, eps, positive=True, fit_intercept=False)
- print("Computing regularization path using the elastic net...")
- alphas_enet, coefs_enet, _ = enet_path(
- X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)
- print("Computing regularization path using the positve elastic net...")
- alphas_positive_enet, coefs_positive_enet, _ = enet_path(
- X, y, eps=eps, l1_ratio=0.8, positive=True, fit_intercept=False)
- # Display results
- plt.figure(1)
- ax = plt.gca()
- ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
- l1 = plt.plot(-np.log10(alphas_lasso), coefs_lasso.T)
- l2 = plt.plot(-np.log10(alphas_enet), coefs_enet.T, linestyle='--')
- 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)
- ax = plt.gca()
- ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
- l1 = plt.plot(-np.log10(alphas_lasso), coefs_lasso.T)
- l2 = plt.plot(-np.log10(alphas_positive_lasso), coefs_positive_lasso.T,
- linestyle='--')
- 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)
- ax = plt.gca()
- ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
- l1 = plt.plot(-np.log10(alphas_enet), coefs_enet.T)
- l2 = plt.plot(-np.log10(alphas_positive_enet), coefs_positive_enet.T,
- linestyle='--')
- 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()
MultiTaskLasso 是一种估计多元回归系数的线性模型, y 是一个2D数组,形式为(n_samples,n_tasks). 其限制条件是和其他回归问题一样,是选择的特征,同样称为 tasks.
接下来的图示比较了通过使用一个简单的Lasso或者MultiTaskLasso得到的W中非零的位置。 Lasso 估计量分散着非零值而MultiTaskLasso所有的列全部是非零的。
数学表达上,它包含了一个使用 先验作为正则化因子。其目标函数是最小化:
这里
MultiTaskLasso 类的实现使用了坐标下降算法来拟合系数。
拟合的时间序列模型
- import matplotlib.pyplot as plt
- import numpy as np
- from sklearn.linear_model import MultiTaskLasso, Lasso
- rng = np.random.RandomState(42)
- # Generate some 2D coefficients with sine waves with random frequency and phase
- n_samples, n_features, n_tasks = 100, 30, 40
- n_relevant_features = 5
- coef = np.zeros((n_tasks, n_features))
- times = np.linspace(0, 2 * np.pi, n_tasks)
- for k in range(n_relevant_features):
- coef[:, k] = np.sin((1. + rng.randn(1)) * times + 3 * rng.randn(1))
- X = rng.randn(n_samples, n_features)
- Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)
- coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T])
- coef_multi_task_lasso_ = MultiTaskLasso(alpha=1.).fit(X, Y).coef_
- ###############################################################################
- # Plot support and time series
- fig = plt.figure(figsize=(8, 5))
- plt.subplot(1, 2, 1)
- plt.spy(coef_lasso_)
- plt.xlabel('Feature')
- plt.ylabel('Time (or Task)')
- plt.text(10, 5, 'Lasso')
- plt.subplot(1, 2, 2)
- plt.spy(coef_multi_task_lasso_)
- plt.xlabel('Feature')
- plt.ylabel('Time (or Task)')
- plt.text(10, 5, 'MultiTaskLasso')
- fig.suptitle('Coefficient non-zero location')
- feature_to_plot = 0
- plt.figure()
- plt.plot(coef[:, feature_to_plot], 'k', label='Ground truth')
- plt.plot(coef_lasso_[:, feature_to_plot], 'g', label='Lasso')
- plt.plot(coef_multi_task_lasso_[:, feature_to_plot],
- 'r', label='MultiTaskLasso')
- plt.legend(loc='upper center')
- plt.axis('tight')
- plt.ylim([-1.1, 1.1])
- plt.show()