scikit-learn 1 cheatsheet

scikit-learn 1: Evaluation and tuning

8 common scikit-learn 1 tasks.

Measure accuracy

The fraction of predictions that are correct. Be careful with imbalanced classes.

from sklearn.metrics import accuracy_score

print(accuracy_score(y_test, predictions))

Get precision, recall and F1

Print per-class precision, recall, F1 and support.

from sklearn.metrics import classification_report

print(classification_report(y_test, predictions))

See where the model is wrong

Rows are true classes and columns are predicted classes.

from sklearn.metrics import confusion_matrix

print(confusion_matrix(y_test, predictions))

Evaluate a regression model

Report error in the target's own units and the share of variance explained.

from sklearn.metrics import mean_squared_error, r2_score

rmse = mean_squared_error(y_test, reg.predict(X_test)) ** 0.5
print(rmse, r2_score(y_test, reg.predict(X_test)))

Cross-validate

Score a model on five different train and test splits for a more reliable estimate.

from sklearn.model_selection import cross_val_score

scores = cross_val_score(clf, X, y, cv=5)
print(scores.mean(), scores.std())

Tune hyperparameters

Try every combination of settings with cross-validation and keep the best.

from sklearn.model_selection import GridSearchCV

search = GridSearchCV(RandomForestClassifier(random_state=42),
                      {"n_estimators": [50, 100], "max_depth": [3, None]}, cv=3)
search.fit(X_train, y_train)
print(search.best_params_, search.best_score_)

See which features matter

Read how much each feature contributed to a random forest.

importances = clf.feature_importances_

Save and load a model

Persist a fitted model or pipeline with joblib. Only load files you trust.

import joblib

joblib.dump(model, "model.joblib")
loaded = joblib.load("model.joblib")

Caution: Loading a joblib or pickle file can run arbitrary code. Never load one from an untrusted source.

Was this page helpful?