Yes-or-No Predictions
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 16 of 58.
This chapter answers yes-or-no questions with chances. It ends with a spam filter you build, ranking emails like this:
Will a restaurant booking be cancelled? LogisticRegression learns it from 60 past bookings: days booked ahead, and cancelled (1) or kept (0):
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=1000) # steps enough to finish learning
model.fit(X, cancelled) # X: days ahead, one column
print(np.round(model.predict_proba([[3], [40]]), 2))This prints:
[[0.91 0.09]
[0.28 0.72]]Each booking's row holds the chance of class 0, then of class 1. [:, 1] keeps the chances of class 1, a cancellation.
They follow an S: never below 0 or above 1, as chances should.
Challenge
MediumA kite club books three launch slots for tomorrow. kites.csv holds 90 past launches: wind_kmh, and flew, 1 if the kite got airborne. The program reads each slot's forecast wind into slots.
- Fit a
LogisticRegression(max_iter=1000) on the winds. - Print each slot's chance of a flight, to a whole percent.
- Print the expected flights: the sum of the chances (1 decimal).
For another club:
6 km/h: 9% chance the kite flies
18 km/h: 64% chance the kite flies
30 km/h: 97% chance the kite flies
Expected flights: 1.7 of 3Try it yourself
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
# 90 past launches: the wind in km/h, and flew (1 if the kite got airborne)
kites = pd.read_csv("kites.csv")
# Tomorrow's three launch slots, by forecast wind
winds = []
for i in range(3):
winds.append(int(input()))
slots = pd.DataFrame({"wind_kmh": winds})
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter4Logistic Regression
Yes-or-No PredictionsThe Decision ThresholdWeights as EvidenceSeveral ClassesRecap: Spam Filter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler