機器學習與演算法(11)--彈性網路(Elastic Net)
阿新 • • 發佈:2018-12-31
彈性網路(Elastic Net)
彈性網路是一種使用 L1,L2範數作為先驗正則項訓練的線性迴歸模型.這種組合允許學習到一個只有少量引數是非零稀疏的模型,就像 Lasso一樣,但是它仍然保持一些像Ridge的正則性質。我們可利用 l1_ratio 引數控制L1和L2的凸組合。彈性網路是一不斷疊代的方法。
彈性網路最妙的地方是它永遠可以產生有效解。由於它不會產生交叉的路徑,所以產生的解都相當不錯。舉例來說,對一個隨機產生的50個城市的推銷員問題,彈性網路的解只有比德賓和威爾蕭的論文中所提的最具競爭力的演演算法長2%(什麼是最具競爭力的演演算法?有人說是林-克尼根(Lin-Kernighan)演演算法,也有人說是SA+OP)。但是彈性網路最吸引人的地方不在它的有效解,而在它收斂的速度。許多人試著去改善彈性網路收斂的速度,都有不錯的結果。舉例來說,柏爾(Burr)所提出的改良版可令50個城市的推銷員問題的收斂次數由1250大幅降為30次。一個最佳化的彈性網路的速度會比林-克尼根快兩倍。
彈性網路在很多特徵互相聯絡的情況下是非常有用的。Lasso 很可能只隨機考慮這些特徵中的一個,而彈性網路更傾向於選擇兩個。 在實踐中,Lasso 和 Ridge 之間權衡的一個優勢是它允許在迴圈過程(Under rotate)中繼承 Ridge 的穩定性。
在這裡,最小化的目標函式是:
應用案例
from itertools import cycle 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 positive 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() 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(coefs_lasso, coefs_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) ax = plt.gca() neg_log_alphas_positive_lasso = -np.log10(alphas_positive_lasso) for coef_l, coef_pl, c in zip(coefs_lasso, coefs_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) ax = plt.gca() neg_log_alphas_positive_enet = -np.log10(alphas_positive_enet) for (coef_e, coef_pe, c) in zip(coefs_enet, coefs_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()