Last active
October 30, 2019 12:18
-
-
Save yuyasugano/96cb60f3a76b7a762d8616e5579a8fab to your computer and use it in GitHub Desktop.
Model-based Selection
This file contains 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
# Model-based Selection | |
from sklearn.feature_selection import SelectFromModel | |
select = SelectFromModel(RandomForestClassifier(n_estimators=100, random_state=42), | |
threshold="1.25*mean") | |
select.fit(X_train_full, y_train_full.values.ravel()) | |
X_train_model = select.transform(X_train_full) | |
print(X_train_model.shape) | |
X_test_model = select.transform(X_test_full) | |
mask = select.get_support() | |
print(mask) | |
plt.matshow(mask.reshape(1, -1), cmap='gray_r') | |
plt.xlabel("Technical Indexes") | |
# GradientBoost Classifier | |
print('--------------------------Without Model-based Selection-------------------------------------') | |
pipe_gb = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) | |
pipe_gb.fit(X_train_full, y_train_full.values.ravel()) | |
print('Train Accuracy: {:.3f}'.format(accuracy_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full)))) | |
print('Test Accuracy: {:.3f}'.format(accuracy_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full)))) | |
print('Train F1 Score: {:.3f}'.format(f1_score(y_train_full.values.ravel(), pipe_gb.predict(X_train_full), average='micro'))) | |
print('Test F1 Score: {:.3f}'.format(f1_score(y_test_full.values.ravel(), pipe_gb.predict(X_test_full), average='micro'))) | |
# GradientBoost Classifier with Model-based Selection | |
print('----------------------------With Model-based Selection--------------------------------------') | |
pipe_gb_model = Pipeline([('scl', StandardScaler()), ('est', GradientBoostingClassifier(random_state=39))]) | |
pipe_gb_model.fit(X_train_model, y_train_full.values.ravel()) | |
print('Train Accuracy: {:.3f}'.format(accuracy_score(y_train_full.values.ravel(), pipe_gb_model.predict(X_train_model)))) | |
print('Test Accuracy: {:.3f}'.format(accuracy_score(y_test_full.values.ravel(), pipe_gb_model.predict(X_test_model)))) | |
print('Train F1 Score: {:.3f}'.format(f1_score(y_train_full.values.ravel(), pipe_gb_model.predict(X_train_model), average='micro'))) | |
print('Test F1 Score: {:.3f}'.format(f1_score(y_test_full.values.ravel(), pipe_gb_model.predict(X_test_model), average='micro'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment