06

Data Scientist · Step 6 of 8 · 11 min

Evaluating models honestly

Choose the right metric, cross-validate, and check a model for leakage and unfair errors.

Accuracy is often the wrong number

Accuracy is the share of predictions that are right. It is easy to understand and easy to be fooled by. When one class is rare, such as fraud or a disease, a model can be very accurate by always predicting “no” and still be useless.

Precision asks: of the cases the model flagged, how many were real? Recall asks: of the real cases, how many did the model catch? A confusion matrix shows all four counts at once and is worth printing every time.

Which mistake costs more?

The right metric depends on the cost of each kind of mistake. Missing a fraud case may cost far more than checking an innocent transaction, so you favour recall. Flagging a loyal customer as a fraudster may cost trust, so you also watch precision.

Most classifiers give a probability, and you choose the threshold for saying “yes”. Moving it trades precision against recall. Pick it with the people who will live with the mistakes, not alone in a notebook.

Cross-validation and leakage

One train/test split can be lucky or unlucky. Cross-validation splits the data several ways, trains and tests each time, and reports the average and spread. In scikit-learn, cross_val_score does it for you. If scores vary a lot between folds, your result is less certain than one number suggests.

Leakage can also hide in your process. Scaling or filling missing values using the whole dataset before splitting lets test information slip into training. Put those steps inside a scikit-learn Pipeline so they are learned from training data only.

Check who the errors fall on

A model with good overall numbers can still fail one group badly. Break your metrics down by meaningful groups, such as region, age band or device, and compare error rates. If a hiring or lending model makes far more mistakes for one group, that is a problem of fairness and often of law.

Removing a sensitive column does not fix this by itself, because other columns can stand in for it. Measure, report what you find, and involve people who understand the context.

Try it

Fool yourself with accuracy, then fix it

  1. 1.Open the free Kaggle “Credit Card Fraud Detection” dataset in a Kaggle notebook, or any dataset with a rare positive class.
  2. 2.Fit a DummyClassifier that always predicts the majority class and print its accuracy.
  3. 3.Fit a LogisticRegression and print classification_report and confusion_matrix.
  4. 4.Write two sentences on which threshold you would choose and why.
In a dataset where 1% of transactions are fraud, a model reaches 99% accuracy. What should you check first?

Check yourself

In a dataset where 1% of transactions are fraud, a model reaches 99% accuracy. What should you check first?

Read alongside