Machine Learning
Metrics
Evaluate classifiers with confusion matrix and reports.
By EZ4Code Team
metricsevaluation
Code
from sklearn.metrics import (accuracy_score, precision_score, recall_score,
f1_score, confusion_matrix, classification_report, roc_auc_score,
precision_recall_curve)
import numpy as np
y_true = np.array([0, 0, 1, 1, 1, 0, 1, 0])
y_pred = np.array([0, 1, 1, 1, 0, 0, 1, 0])
y_prob = np.array([0.1, 0.6, 0.8, 0.9, 0.4, 0.2, 0.7, 0.3])
print("accuracy:", accuracy_score(y_true, y_pred))
print("precision:", precision_score(y_true, y_pred))
print("recall:", recall_score(y_true, y_pred))
print("f1:", f1_score(y_true, y_pred))
print("confusion:", confusion_matrix(y_true, y_pred), sep="\n")
print(classification_report(y_true, y_pred))
print("roc_auc:", roc_auc_score(y_true, y_prob))
precision, recall, _ = precision_recall_curve(y_true, y_prob)
print("pr points:", len(precision))Explanation
scikit-learn provides per-class and aggregate metrics for classification: precision, recall, f1, and the confusion matrix all live in sklearn.metrics. roc_auc_score and precision_recall_curve operate on predicted probabilities rather than hard labels. classification_report prints a per-class table that is convenient to inspect during model iteration.
More Machine Learning Snippets
Data Preprocessing
Scale, encode, and impute features with sklearn.
Train Test Split
Split data into training and evaluation sets.
Classification
Train and predict with common classifiers.
Regression
Fit regressors and evaluate with RMSE and R2.
Clustering
Cluster with KMeans and DBSCAN.
Pipeline
Chain preprocessing and modeling with Pipeline.