3.Lasso线性模型
Lasso 是一種估計稀疏線性模型的方法.由于它傾向具有少量參數值的情況,對于給定解決方案是相關情況下,有效的減少了變量數量。 因此,Lasso及其變種是壓縮感知(壓縮采樣)的基礎。在約束條件下,它可以回復一組非零精確的權重系數(參考下文中的 CompressIve sensing(壓縮感知:重建醫學圖像通過lasso L1)點擊打開鏈接)。
用數學形式表達,Lasso 包含一個使用??先驗作為正則化因子的線性模型。其目標函數是最小化:
lasso 解決帶??懲罰項的最小平方和,其中??是一個常量,?是參數向量的?-norm
Lasso 類實現使用了坐標下降法(一種非梯度優化算法) 來擬合系數.參考另一種實現 Least Angle Regression最小角回歸
from sklearn import linear_model clf = linear_model.Lasso(alpha = 0.1) clf.fit([[0, 0], [1, 1]], [0, 1]) 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) clf.predict([[1, 1]])函數 lasso_path 對于lower-level任務非常有用。它能夠通過搜索所有可能的路徑上的值來計算系數.
3.1 設置正則化參數
alpha 參數控制估計的系數的稀疏程度。
3.1.1 使用交叉驗證
scikit-learn 暴露以下兩個類 LassoCV 和 LassoLarsCV 可以設置 Lasso alpha 參數.
LassoCV 基于下面解釋的算法 Least Angle Regression最小角回歸
對于含有很多共線性的高維的數據集,LassoCV 是最合適不過了。
然而,LassoLarsCV 在尋找 alpha 參數更相關的值時更具有優勢, 并且如果樣本相比于觀測的數量時,
通常比 LassoCV 更快.
3.1.2 基于模型選擇的信息約束
LassoLarsIC 建議使用Akaike information criterion (AIC) 和 Bayes Information criterion (BIC)。 由于在計算:math:alpha 過程中,當使用k-折交叉驗證的時候,正則化路徑只計算1次而不是k+1次,所以在計算上代價非常小。 然而,這種約束需要一個合適的對于解的自由度的估計(可參考矩陣的解的自由度),這可以從大量的樣本(漸進結果)導出并且 假設模型是正確的。例如,數據實際上是有該模型產生的,但是當問題是病態條件時這種數據可能會有問題(參考病態矩陣,條件數等概念),比如 特征維數大于樣本數.(小樣本問題)
print(__doc__)import timeimport numpy as np import matplotlib.pyplot as pltfrom sklearn.linear_model import LassoCV, LassoLarsCV, LassoLarsIC from sklearn import datasetsdiabetes = datasets.load_diabetes() X = diabetes.data y = diabetes.targetrng = np.random.RandomState(42) X = np.c_[X, rng.randn(X.shape[0], 14)] # add some bad features# normalize data as done by Lars to allow for comparison X /= np.sqrt(np.sum(X ** 2, axis=0))############################################################################## # LassoLarsIC: least angle regression with BIC/AIC criterionmodel_bic = LassoLarsIC(criterion='bic') t1 = time.time() model_bic.fit(X, y) t_bic = time.time() - t1 alpha_bic_ = model_bic.alpha_model_aic = LassoLarsIC(criterion='aic') model_aic.fit(X, y) alpha_aic_ = model_aic.alpha_def plot_ic_criterion(model, name, color):alpha_ = model.alpha_alphas_ = model.alphas_criterion_ = model.criterion_plt.plot(-np.log10(alphas_), criterion_, '--', color=color,linewidth=3, label='%s criterion' % name)plt.axvline(-np.log10(alpha_), color=color, linewidth=3,label='alpha: %s estimate' % name)plt.xlabel('-log(alpha)')plt.ylabel('criterion')plt.figure() plot_ic_criterion(model_aic, 'AIC', 'b') plot_ic_criterion(model_bic, 'BIC', 'r') plt.legend() plt.title('Information-criterion for model selection (training time %.3fs)'% t_bic)############################################################################## # LassoCV: coordinate descent# Compute paths print("Computing regularization path using the coordinate descent lasso...") t1 = time.time() model = LassoCV(cv=20).fit(X, y) t_lasso_cv = time.time() - t1# Display results m_log_alphas = -np.log10(model.alphas_)plt.figure() ymin, ymax = 2300, 3800 plt.plot(m_log_alphas, model.mse_path_, ':') plt.plot(m_log_alphas, model.mse_path_.mean(axis=-1), 'k',label='Average across the folds', linewidth=2) plt.axvline(-np.log10(model.alpha_), linestyle='--', color='k',label='alpha: CV estimate')plt.legend()plt.xlabel('-log(alpha)') plt.ylabel('Mean square error') plt.title('Mean square error on each fold: coordinate descent ''(train time: %.2fs)' % t_lasso_cv) plt.axis('tight') plt.ylim(ymin, ymax)############################################################################## # LassoLarsCV: least angle regression# Compute paths print("Computing regularization path using the Lars lasso...") t1 = time.time() model = LassoLarsCV(cv=20).fit(X, y) t_lasso_lars_cv = time.time() - t1# Display results m_log_alphas = -np.log10(model.cv_alphas_)plt.figure() plt.plot(m_log_alphas, model.cv_mse_path_, ':') plt.plot(m_log_alphas, model.cv_mse_path_.mean(axis=-1), 'k',label='Average across the folds', linewidth=2) plt.axvline(-np.log10(model.alpha_), linestyle='--', color='k',label='alpha CV') plt.legend()plt.xlabel('-log(alpha)') plt.ylabel('Mean square error') plt.title('Mean square error on each fold: Lars (train time: %.2fs)'% t_lasso_lars_cv) plt.axis('tight') plt.ylim(ymin, ymax)plt.show()
總結
以上是生活随笔為你收集整理的3.Lasso线性模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2.Ridge Regression 岭
- 下一篇: 5.Multil-task lasso(