abess-team / abess

Fast Best-Subset Selection Library
https://abess.readthedocs.io/
Other
474 stars 41 forks source link

`SelectFromModel` in `scikit-learn` enables the `abess` estimator #479

Closed Mamba413 closed 1 year ago

Mamba413 commented 1 year ago

Make the estimators in abess (like LinearRegression and LogisticRegression) can be used via SelectFromModel. See details about sklearn.feature_selection.SelectFromModel in https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFromModel.html#sklearn.feature_selection.SelectFromModel.

chenpnn commented 1 year ago

@Mamba413 Thanks for you suggestion! I have read the corresponding document. SelectFromModel selects features throuth a specific attribute (e.g. coef_), which measures the importance of features, of a base estimator (e.g. abess.LinearRegression). Therefore, abess is already compatible with SelectFromModel and a typical usage can be made as follows:

import numpy as np
from abess.datasets import make_glm_data
from abess import LinearRegression
from sklearn.feature_selection import SelectFromModel

np.random.seed(0)
n, p, k = 300, 1000, 5
data = make_glm_data(n=n, p=p, k=k, family='gaussian')
X, y = data.x, data.y
print(X.shape) # (300, 1000)

model = LinearRegression().fit(X, y)
sfm = SelectFromModel(model, prefit=True)
X_new = sfm.transform(X)
print(X_new.shape)  # (300, 5)
Mamba413 commented 1 year ago

@chenpnn Thx! It would be better to mark it on: https://abess.readthedocs.io/en/latest/auto_gallery/5-scikit-learn-connection/plot_1_scikit_learn.html#sphx-glr-auto-gallery-5-scikit-learn-connection-plot-1-scikit-learn-py.
You can feel free to reorganize this Tutorial page.

chenpnn commented 1 year ago

@chenpnn Thx! It would be better to mark it on: https://abess.readthedocs.io/en/latest/auto_gallery/5-scikit-learn-connection/plot_1_scikit_learn.html#sphx-glr-auto-gallery-5-scikit-learn-connection-plot-1-scikit-learn-py. You can feel free to reorganize this Tutorial page.

Ok, I have added an example to this tutorial page.

Mamba413 commented 1 year ago

Thanks for your contribution! It will be better to use a pull request in future.