Menu
CoddyTech

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:

Eight emails as bars of their chance of being spam, from 0.02 for Mom to 0.97 for WIN NOW. Bars above a dashed line at 0.5 are red: spam

Will a restaurant booking be cancelled? LogisticRegression learns it from 60 past bookings: days booked ahead, and cancelled (1) or kept (0):

Sixty bookings as dots at 0 (kept) and 1 (cancelled) against days booked ahead, and an orange S curve rising from 0.07 to 0.93. It reads 0.09 at 3 days and 0.72 at 40 days

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 icon

Challenge

Medium

A 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.

  1. Fit a LogisticRegression (max_iter=1000) on the winds.
  2. Print each slot's chance of a flight, to a whole percent.
  3. 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 3

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler