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:
The 1 asks for a straight line, and x comes first. This is linear regression, the first model you train in section 3.
Challenge
MediumA 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).
- For each column in
columns, fit the line that predictsflight_minfrom it. - Print its equation and SSR as below: slope to 3 decimals, intercept and SSR to 2.
- 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_kmhTry 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town7Trend Lines
ResidualsLeast Squares FitDrawing the FitPredicting with a LineHow Good Is the Fit?Recap: Practice Hours2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler