How to tune a BigQuery ML classification model to achieve a desired precision or recall

This means that we will falsely identify 1% of on-time flights as being late.Tuning probability threshold in SQLHere’s a query that will return the threshold without having to draw a graph and mouse over it:WITH roc AS (SELECT * from ML.ROC_CURVE(MODEL flights.delayed,(SELECT IF(arr_delay < 15, 0, 1) AS late, carrier, origin, dest, dep_delay, taxi_out, distanceFROM `cloud-training-demos.flights.tzcorr` as fJOIN `cloud-training-demos.flights.trainday` as tON f.FL_DATE = t.FL_DATEWHERE arr_delay IS NOT NULL AND is_train_day = 'True')))SELECT threshold, recall, false_positive_rate,ABS(recall – 0.7) AS from_desired_recallFROM rocORDER BY from_desired_recall ASCLIMIT 1This gives us the threshold we need to use to get a recall of 0.7:Choosing the threshold that gives us a recall of 0.7.Now, when we do ML predictions, we can ignore the predicted label and instead threshold the predicted probability ourselves:WITH predictions AS (SELECT * from ML.PREDICT(MODEL flights.delayed,(SELECT IF(arr_delay < 15, 0, 1) AS late, carrier, origin, dest, dep_delay, taxi_out, distanceFROM `cloud-training-demos.flights.tzcorr` as fJOIN `cloud-training-demos.flights.trainday` as tON f.FL_DATE = t.FL_DATEWHERE arr_delay IS NOT NULL AND is_train_day = 'False'LIMIT 10)))SELECT IF(p.prob > 0.3348, 1, 0) AS predicted_late_70recall,* EXCEPT (predicted_late_probs, label)FROM predictionsCROSS JOIN UNNEST(predicted_late_probs) AS pWHERE p.label = 1 — we are thresholding on Prob(late-flights)Enjoy!. More details

Leave a Reply