Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Classification Metrics
Accuracy is the fraction of predictions that were right. It is also, on imbalanced data, close to useless: if 1 percent of transactions are fraud, predicting "not fraud" every single time scores 99 percent accuracy and catches nothing.
The two that actually describe a classifier:
They trade off. Flag everything and recall is 1.0 while precision collapses. Flag nothing and precision is undefined while recall is 0. Which one you care about is a question about consequences, not about maths: a missed tumour and a false alarm are not equally bad.
from sklearn.metrics import accuracy_score, precision_score, recall_score accuracy_score(y_true, y_pred) precision_score(y_true, y_pred, zero_division=0) recall_score(y_true, y_pred, zero_division=0)
zero_division=0 says what to do when the denominator is zero, which happens whenever the model predicted no positives at all. Without it you get a warning and a nan.
Your task:
Write score_model(y_true, y_pred) that returns [accuracy, precision, recall], in that order, for binary labels of 0 and 1.
Example Tests
A perfect classifier scores 1.0 on all three
Input: {"y_pred":[0,1,0,1],"y_true":[0,1,0,1]}
Expected: [1,1,1]
One false positive lowers precision but not recall
Input: {"y_pred":[1,1,0,1],"y_true":[0,1,0,1]}
Expected: [0.75,0.66667,1]
One missed positive lowers recall but not precision
Input: {"y_pred":[0,1,0,1],"y_true":[0,1,1,1]}
Expected: [0.75,1,0.66667]
import numpy as np
from sklearn.metrics import accuracy_score, precision_score, recall_score
def score_model(y_true, y_pred):
"""
Report the three headline classification metrics.
Args:
y_true: 1-D array of true labels (0 or 1)
y_pred: 1-D array of predicted labels (0 or 1)
Returns:
A list [accuracy, precision, recall].
"""
# YOUR CODE HERE
passRun your code to see results
⌘↵ runs against the visible tests