Igegre
Igegre

Reputation: 55

Selecting columns of dataframe where Lasso coefficient is nonzero

I did LASSO regression with a data-set using scikit and pandas. I want to try to fit OLS to the features selected by LASSO. I have something like

lassomodel = LassoCV(alphas = [1, 0.1, 0.001, 0.0005]).fit(X_train, y_train)

and

lassomodel.coef_ 

and I want to get the dataframe or numpy array with all the features whose LASSO coefficients is unequal to zero.

Upvotes: 1

Views: 1876

Answers (1)

Ben.T
Ben.T

Reputation: 29635

IIUC, you can use boolean indexing on X_train where the coefficients are not equal to 0

If X_train is a numpy array then you do:

X_train[:,lassomodel.coef_!=0]

If X_train is a pandas dataframe, then you do:

X_train.iloc[:,lassomodel.coef_!=0]

Upvotes: 3

Related Questions