Created
November 13, 2021 12:27
-
-
Save shinichi-takayanagi/e2ae117f9dd095d4b41fbd609f838fa9 to your computer and use it in GitHub Desktop.
Grid Search
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
# https://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html#sphx-glr-auto-examples-model-selection-plot-multi-metric-evaluation-py | |
# https://machinelearningmastery.com/nested-cross-validation-for-machine-learning-with-python/ | |
from numpy import mean | |
from numpy import std | |
from sklearn.datasets import make_classification | |
from sklearn.metrics import make_scorer, accuracy_score | |
from sklearn.model_selection import cross_val_score, KFold, RandomizedSearchCV, GridSearchCV, train_test_split | |
from sklearn.ensemble import RandomForestClassifier | |
# Data | |
X, y = make_classification(n_samples=1000, n_features=20, random_state=1, n_informative=10, n_redundant=10) | |
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123) | |
# Model | |
model = RandomForestClassifier(random_state=1) | |
grid = { | |
'n_estimators' : [10, 100, 500], | |
'max_features': [2, 4, 6] | |
} | |
scoring = {"AUC": "roc_auc", "Accuracy": make_scorer(accuracy_score)} | |
cv = KFold(n_splits=10, shuffle=True) | |
gs = GridSearchCV(model, param_grid=space, scoring=scoring, refit="Accuracy", n_jobs=-1, cv=cv, return_train_score=True) | |
gs.fit(X_train, y_train) | |
print(gs.score(X_test,y_test)) | |
print(gs.best_score_) | |
print(gs.best_estimator_) | |
print(gs.best_params_) | |
print(gs.best_index_) | |
print(gs.cv_results_["mean_test_Accuracy"]) | |
# ... or only score | |
print(cross_val_score(gs, X=X_test, y=y_test, cv=5).mean()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment