Skip to content

Instantly share code, notes, and snippets.

@WillKoehrsen
Created August 15, 2018 20:07
Show Gist options
  • Save WillKoehrsen/27d12fba73d729cd3c50b20442925087 to your computer and use it in GitHub Desktop.
Save WillKoehrsen/27d12fba73d729cd3c50b20442925087 to your computer and use it in GitHub Desktop.
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