Menu
CoddyTech

Least Squares Fit

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 36 of 64.

Guessing lines could go on forever. np.polyfit(x, y, 1) finds the best one at once: the line with the smallest sum of squared residuals. Here is a storm's snow depth:

hours = np.array([1, 2, 3, 5, 6])
depth = np.array([14, 22, 35, 52, 61])
slope, intercept = np.polyfit(hours, depth, 1)
print(np.round(slope, 2), np.round(intercept, 2))

This prints 9.5 4.5: about 9.5 cm more snow per hour, and 4.5 cm at hour 0. No other line misses by less:

Snow depth against hours as five dots, with three lines: two dashed guesses with sums of squared residuals 159 and 18, and the thick polyfit line with 6.5

The 1 asks for a straight line, and x comes first. This is linear regression, the first model you train in section 3.

challenge icon

Challenge

Medium

A drone maker wants to know which measurement predicts flight time best. drones.csv holds 15 flights: payload_g, wind_kmh and flight_min. The starter loads it and defines ssr(x, y, slope, intercept).

  1. For each column in columns, fit the line that predicts flight_min from it.
  2. Print its equation and SSR as below: slope to 3 decimals, intercept and SSR to 2.
  3. Print the column with the smaller SSR.

For another drone:

payload_g: flight_min = -0.001 * payload_g + 21.55, SSR 164.94
wind_kmh: flight_min = -0.481 * wind_kmh + 28.13, SSR 9.1
Better predictor: wind_kmh

Try it yourself

import numpy as np
import pandas as pd


# Returns the sum of squared residuals of the line slope * x + intercept (lesson 35)
def ssr(x, y, slope, intercept):
    return ((y - (slope * x + intercept)) ** 2).sum()


drones = pd.read_csv("drones.csv")
columns = ["payload_g", "wind_kmh"]

# 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 Statistics for ML

Practice on your own: Online Python compiler