Created
August 15, 2018 20:07
-
-
Save WillKoehrsen/27d12fba73d729cd3c50b20442925087 to your computer and use it in GitHub Desktop.
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
from sklearn.metrics import f1_score, make_scorer | |
from sklearn.feature_selection import RFECV | |
from sklearn.ensemble import RandomForestClassifier | |
# Custom scorer for cross validation | |
scorer = make_scorer(f1_score, greater_is_better=True, average = 'macro') | |
# Create a model for feature selection | |
estimator = RandomForestClassifier(n_estimators = 100, n_jobs = -1) | |
# Create the object | |
selector = RFECV(estimator, step = 1, cv = 3, | |
scoring= scorer, n_jobs = -1) | |
# Fit on training data | |
selector.fit(train, train_labels) | |
# Transform data | |
train_selected = selector.transform(train) | |
test_selected = selector.transform(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment