[英]How is the scikit Lasso/LARS used as a regressive feature selection tool?
我有大約22個數據預測變量x_i,我想減少到一定數量以便最好地描述y。 基本問題...但是,我還不清楚如何使用scikit和linearmodel.lassoLars來執行此任務。
從他們的示例文檔中,代碼就像:
alpha = 0.1
lasso = Lasso(alpha=alpha)
y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)
因此它執行回歸和套索,但是我不確定如何使用y_pred_lasso來輸出我想要的東西,即來自22個最能准確描述y_train的原始預測變量的變量。
調用fit
,可以使用Lasso
實例的coef_
屬性訪問選定的coef_
。 此屬性存儲每個要素的權重。
>>> lasso = Lasso(alpha=alpha).fit(X_train, y_train)
>>> lasso.coef_ != 0
array([ True, True, True, False, False, True, True, True, True,
True, True, True, True], dtype=bool)
>>> import numpy as np
>>> np.nonzero(lasso.coef_)
(array([ 0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 12]),)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.