Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mrdbourke
GitHub Repository: mrdbourke/zero-to-mastery-ml
Path: blob/master/section-2-data-science-and-ml-tools/scikit-learn-metric-comparison.ipynb
874 views
Kernel: Python 3

Comparing the metrics of different Scikit-Learn models

One of the most important things when comparing different models is to make sure they're compared on the same data splits.

For example, let's say you have model_1 and model_2 which each differ slightly.

If you want to compare and evaulate their results, model_1 and model_2 should both be trained on the same data (e.g. X_train and y_train) and their predictions should each be made on the same data, for example:

  • model_1.fit(X_train, y_train) -> model_1.predict(X_test) -> model_1_preds

  • model_2.fit(X_train, y_train) -> model_2.predict(X_test) -> model_2_preds

Note the differences here being the two models and the 2 different sets of predictions which can be compared against each other.

This short notebook compares 3 different models on a small dataset.

  1. A baseline RandomForestClassifier (all default parameters)

  2. A RandomForestClassifier tuned with RandomizedSearchCV (and refit=True)

  3. A RandomForestClassifier tuned with GridSearchCV (and refit=True)

The most important part is they all use the same data splits created using train_test_split() and np.random.seed(42).

import pandas as pd import numpy as np from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score from sklearn.model_selection import train_test_split

Import and split data

heart_disease = pd.read_csv("https://raw.githubusercontent.com/mrdbourke/zero-to-mastery-ml/master/data/heart-disease.csv") # Split into X & y X = heart_disease.drop("target", axis =1) y = heart_disease["target"] # Split into train & test np.random.seed(42) # seed for reproducibility X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

Make evaluation function

Our evaluation function will use all of the major classification metric functions from Scikit-Learn.

def evaluate_preds(y_true, y_preds): """ Performs evaluation comparison on y_true labels vs. y_pred labels on a classification. """ accuracy = accuracy_score(y_true, y_preds) precision = precision_score(y_true, y_preds) recall = recall_score(y_true, y_preds) f1 = f1_score(y_true, y_preds) metric_dict = {"accuracy": round(accuracy, 2), "precision": round(precision, 2), "recall": round(recall, 2), "f1": round(f1, 2)} print(f"Acc: {accuracy * 100:.2f}%") print(f"Precision: {precision:.2f}") print(f"Recall: {recall:.2f}") print(f"F1 score: {f1:.2f}") return metric_dict

Baseline model

Create model with default hyperparameters. See RandomForestClassifier documentation for more.

np.random.seed(42) # Make & fit baseline model clf = RandomForestClassifier() clf.fit(X_train, y_train) # Make baseline predictions y_preds = clf.predict(X_test) # Evaluate the classifier on validation set baseline_metrics = evaluate_preds(y_test, y_preds)
Acc: 83.61% Precision: 0.84 Recall: 0.84 F1 score: 0.84

RandomizedSearchCV

Find hyperparameters with RandomizedSearchCV.

Note: Although best parameters are found on different splits of X_train and y_train, because refit=True, once the best parameters are found, they are refit to the entire set of X_train and y_train. See the RandomizedSearchCV and cross-validation documentation for more.

from sklearn.model_selection import RandomizedSearchCV # Setup the parameters grid grid = {"n_estimators": [10, 100, 200, 500, 1000, 1200], "max_depth": [None, 5, 10, 20, 30], "max_features": ["auto", "sqrt"], "min_samples_split": [2, 4, 6], "min_samples_leaf": [1, 2, 4]} # Instantiate RandomForestClassifier clf = RandomForestClassifier(n_jobs=1) # Setup RandomizedSearchCV rs_clf = RandomizedSearchCV(estimator=clf, param_distributions=grid, n_iter=10, # number of models to try cv=5, verbose=2, random_state=42, # set random_state to 42 for reproducibility refit=True) # set refit=True (default) to refit the best model on the full dataset # Fit the RandomizedSearchCV version of clf rs_clf.fit(X_train, y_train) # 'rs' is short for RandomizedSearch
Fitting 5 folds for each of 10 candidates, totalling 50 fits [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10 [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10, total= 0.1s [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers. [Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s remaining: 0.0s
[CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10, total= 0.1s [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10 [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10, total= 0.1s [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10 [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10, total= 0.1s [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10 [CV] n_estimators=100, min_samples_split=6, min_samples_leaf=1, max_features=auto, max_depth=10, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.1s [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30 [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30, total= 1.2s [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30 [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30, total= 1.2s [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30 [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30, total= 1.2s [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30 [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30, total= 1.2s [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30 [CV] n_estimators=1200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=30, total= 1.2s [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.2s [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.2s [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.2s [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.2s [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=200, min_samples_split=6, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 0.2s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30 [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30 [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30 [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30 [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30, total= 0.1s [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30 [CV] n_estimators=100, min_samples_split=2, min_samples_leaf=4, max_features=auto, max_depth=30, total= 0.1s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 1.2s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 1.2s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 1.2s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 1.4s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=2, max_features=sqrt, max_depth=None, total= 1.2s [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10 [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10, total= 0.5s [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10 [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10, total= 0.5s [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10 [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10, total= 0.6s [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10 [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10, total= 0.6s [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10 [CV] n_estimators=500, min_samples_split=4, min_samples_leaf=2, max_features=sqrt, max_depth=10, total= 0.5s [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20 [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20, total= 1.1s [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20 [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20, total= 1.2s [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20 [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20, total= 1.1s [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20 [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20, total= 1.0s [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20 [CV] n_estimators=1000, min_samples_split=2, min_samples_leaf=4, max_features=sqrt, max_depth=20, total= 1.0s [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20 [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20, total= 0.0s [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20 [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20, total= 0.0s [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20 [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20, total= 0.0s [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20 [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20, total= 0.0s [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20 [CV] n_estimators=10, min_samples_split=2, min_samples_leaf=2, max_features=auto, max_depth=20, total= 0.0s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20 [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20, total= 1.2s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20 [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20, total= 1.4s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20 [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20, total= 1.3s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20 [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20, total= 1.3s [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20 [CV] n_estimators=1200, min_samples_split=2, min_samples_leaf=1, max_features=sqrt, max_depth=20, total= 1.3s
[Parallel(n_jobs=1)]: Done 50 out of 50 | elapsed: 29.7s finished
RandomizedSearchCV(cv=5, error_score=nan, estimator=RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None, criterion='gini', max_depth=None, max_features='auto', max_leaf_nodes=None, max_samples=None, min_impurity_decrease=0.0, min_impurity_split=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs... random_state=None, verbose=0, warm_start=False), iid='deprecated', n_iter=10, n_jobs=None, param_distributions={'max_depth': [None, 5, 10, 20, 30], 'max_features': ['auto', 'sqrt'], 'min_samples_leaf': [1, 2, 4], 'min_samples_split': [2, 4, 6], 'n_estimators': [10, 100, 200, 500, 1000, 1200]}, pre_dispatch='2*n_jobs', random_state=42, refit=True, return_train_score=False, scoring=None, verbose=2)
# Check best parameters of RandomizedSearchCV rs_clf.best_params_
{'n_estimators': 100, 'min_samples_split': 2, 'min_samples_leaf': 4, 'max_features': 'auto', 'max_depth': 30}
# Evaluate RandomizedSearch model rs_y_preds = rs_clf.predict(X_test) # Evaluate the classifier on validation set rs_metrics = evaluate_preds(y_test, rs_y_preds)
Acc: 85.25% Precision: 0.85 Recall: 0.88 F1 score: 0.86

GridSearchCV

Find best hyperparameters using GridSearchCV.

Note: Although best parameters are found on different splits of X_train and y_train, because refit=True, once the best parameters are found, they are refit to the entire set of X_train and y_train. See the GridSearchCV and cross-validation documentation for more.

from sklearn.model_selection import GridSearchCV # Setup grid-2 (refined version of grid) grid_2 = {'n_estimators': [100, 200, 500], 'max_depth': [None], 'max_features': ['auto', 'sqrt'], 'min_samples_split': [6], 'min_samples_leaf': [1, 2]} # Instantiate RandomForestClassifier clf = RandomForestClassifier(n_jobs=1) # Setup GridSearchCV gs_clf = GridSearchCV(estimator=clf, param_grid=grid_2, cv=5, verbose=2, refit=True) # set refit=True (default) to refit the best model on the full dataset # Fit the GridSearchCV version of clf gs_clf.fit(X_train, y_train) # 'gs' is short for GridSearch
Fitting 5 folds for each of 12 candidates, totalling 60 fits [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s remaining: 0.0s
[CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.6s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.6s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.6s [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.6s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.6s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.6s [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=auto, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=1, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=100, total= 0.1s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=200, total= 0.2s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.5s [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500 [CV] max_depth=None, max_features=sqrt, min_samples_leaf=2, min_samples_split=6, n_estimators=500, total= 0.5s
[Parallel(n_jobs=1)]: Done 60 out of 60 | elapsed: 17.1s finished
GridSearchCV(cv=5, error_score=nan, estimator=RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None, criterion='gini', max_depth=None, max_features='auto', max_leaf_nodes=None, max_samples=None, min_impurity_decrease=0.0, min_impurity_split=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1, oob_score=False, random_state=None, verbose=0, warm_start=False), iid='deprecated', n_jobs=None, param_grid={'max_depth': [None], 'max_features': ['auto', 'sqrt'], 'min_samples_leaf': [1, 2], 'min_samples_split': [6], 'n_estimators': [100, 200, 500]}, pre_dispatch='2*n_jobs', refit=True, return_train_score=False, scoring=None, verbose=2)
# Find best parameters of GridSearchCV gs_clf.best_params_
{'max_depth': None, 'max_features': 'auto', 'min_samples_leaf': 2, 'min_samples_split': 6, 'n_estimators': 200}
# Evaluate GridSearchCV model gs_y_preds = gs_clf.predict(X_test) # Evaluate the classifier on validation set gs_metrics = evaluate_preds(y_test, gs_y_preds)
Acc: 85.25% Precision: 0.85 Recall: 0.88 F1 score: 0.86

Compare metrics

Compare all of the found metrics between the models.

compare_metrics = pd.DataFrame({"baseline": baseline_metrics, "random search": rs_metrics, "grid search": gs_metrics}) compare_metrics.plot.bar(figsize=(10, 8));
Image in a Jupyter notebook