Compute Precision, Recall, and F1
Advanced · Write the code · from Evaluating ML Models: Cross-Validation, Metrics, and Overfitting
From the lesson: Evaluating ML Models: Cross-Validation, Metrics, and Overfitting
You've trained a model and it gets 95% accuracy. Should you celebrate? Not yet. That number might be lying to you. Maybe your test set was too easy. Maybe the model memorized the training data. Maybe accuracy is the wrong metric entirely.
Read the lessonTrain a LogisticRegression(random_state=42) on the imbalanced dataset. Print three lines: "Precision: X.XXXX", "Recall: X.XXXX", "F1: X.XXXX" - each rounded to 4 decimal places.
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.metrics import precision_score, recall_score, f1_score
X, y = make_classification(n_samples=500, n_features=6,
weights=[0.9, 0.1],
random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
# Train and print precision, recall, F1
More Model Evaluation exercises
- Run Cross-ValidationAdvanced
- Detect OverfittingAdvanced
- Find Best HyperparametersAdvanced
- Complete Evaluation PipelineAdvanced
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz