-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG+2] Implemented SelectFromModel meta-transformer #4242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c438f78
Implemented SelectFromModel meta-transformer
8a28ea8
fix test failures
MechCoder 459cb9b
Remove warm start
MechCoder 2416e2a
Catch filters instead of removing the tests
MechCoder 9cee0d9
Added example to depict feature selction using SelectFromModel and Lasso
MechCoder 3d41053
Minor doc changes and removed _set_threshold and _set_importances
MechCoder 10176d9
Now a fitted estimator can be passed to SelectFromModel
MechCoder 2ee718c
Add narrative docs and fix examples
MechCoder acf5f16
Merge SelectFromModel and L1-selection examples
MechCoder 5a0db17
1. Added parameter prefit to pass in a fitted estimator.
MechCoder c805fbc
Refactor tests
MechCoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,6 +207,11 @@ Enhancements | |
the same. This allows gradient boosters to turn off presorting when building | ||
deep trees or using sparse data. By `Jacob Schreiber`_. | ||
|
||
- Added :class:`feature_selection.SelectFromModel` meta-transformer which can | ||
be used along with estimators that have `coef_` or `feature_importances_` | ||
attribute to select important features of the input data. By | ||
`Maheshakya Wijewardena`_, `Joel Nothman`_ and `Manoj Kumar`_. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also should have entry in API changes describing deprecation. |
||
Bug fixes | ||
......... | ||
|
||
|
@@ -269,6 +274,13 @@ API changes summary | |
fit method to the constructor in | ||
:class:`discriminant_analysis.QuadraticDiscriminantAnalysis`. | ||
|
||
- Models inheriting from ``_LearntSelectorMixin`` will no longer support the | ||
transform methods. (i.e, RandomForests, GradientBoosting, LogisticRegression, | ||
DecisionTrees, SVMs and SGD related models). Wrap these models around the | ||
metatransfomer :class:`feature_selection.SelectFromModel` to remove | ||
features (according to `coefs_` or `feature_importances_`) | ||
which are below a certain threshold value instead. | ||
|
||
.. _changes_0_1_16: | ||
|
||
Version 0.16.1 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
examples/feature_selection/plot_select_from_model_boston.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
=================================================== | ||
Feature selection using SelectFromModel and LassoCV | ||
=================================================== | ||
|
||
Use SelectFromModel meta-transformer along with Lasso to select the best | ||
couple of features from the Boston dataset. | ||
""" | ||
# Author: Manoj Kumar <mks542@nyu.edu> | ||
# License: BSD 3 clause | ||
|
||
print(__doc__) | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from sklearn.datasets import load_boston | ||
from sklearn.feature_selection import SelectFromModel | ||
from sklearn.linear_model import LassoCV | ||
|
||
# Load the boston dataset. | ||
boston = load_boston() | ||
X, y = boston['data'], boston['target'] | ||
|
||
# We use the base estimator LassoCV since the L1 norm promotes sparsity of features. | ||
clf = LassoCV() | ||
|
||
# Set a minimum threshold of 0.25 | ||
sfm = SelectFromModel(clf, threshold=0.25) | ||
sfm.fit(X, y) | ||
n_features = sfm.transform(X).shape[1] | ||
|
||
# Reset the threshold till the number of features equals two. | ||
# Note that the attribute can be set directly instead of repeatedly | ||
# fitting the metatransformer. | ||
while n_features > 2: | ||
sfm.threshold += 0.1 | ||
X_transform = sfm.transform(X) | ||
n_features = X_transform.shape[1] | ||
|
||
# Plot the selected two features from X. | ||
plt.title( | ||
"Features selected from Boston using SelectFromModel with " | ||
"threshold %0.3f." % sfm.threshold) | ||
feature1 = X_transform[:, 0] | ||
feature2 = X_transform[:, 1] | ||
plt.plot(feature1, feature2, 'r.') | ||
plt.xlabel("Feature number 1") | ||
plt.ylabel("Feature number 2") | ||
plt.ylim([np.min(feature2), np.max(feature2)]) | ||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just "Feature selection from estimated models" or something...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be more useful to explicitly mention the name of the model in the title.