Drawing the Fit
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 37 of 64.
Two numbers describe a fit; only a chart shows if it follows the data. A kitchen timed six kettle boils. Draw the line on the same ax as the scatter:
liters = np.array([0.5, 0.8, 1.0, 1.2, 1.5, 1.7])
minutes = np.array([1.6, 2.4, 2.7, 3.3, 3.9, 4.5])
slope, intercept = np.polyfit(liters, minutes, 1)
xs = np.linspace(liters.min(), liters.max(), 50)
fig, ax = plt.subplots()
ax.scatter(liters, minutes, label="Boils")
ax.plot(xs, slope * xs + intercept, color="C1", label="Fit")
ax.legend()
plt.show()xs runs from the smallest x to the largest, so the line starts and ends where the data does:
Drawing a model's predictions over its data is the quickest check that it learned something sensible. A line that misses the dots means a broken fit.
Challenge
MediumA building manager timed 12 elevator rides: floors traveled and seconds taken. The starter sets up the titled, labelled chart.
- Fit the line that predicts
secondsfromfloors. - Draw the rides as a scatter labelled
Rides, and the line labelledFit, from the fewest floors to the most. - Add a legend and show the chart.
It prints nothing; Coddy describes your chart. Another building's looks like this:
Try it yourself
import numpy as np
import matplotlib.pyplot as plt
floors = np.array([3, 7, 1, 12, 5, 9, 15, 2, 10, 6, 13, 8])
seconds = np.array([15.3, 27.0, 12.4, 36.0, 19.5, 28.8, 44.9, 12.7, 33.2, 19.4, 41.7, 27.0])
# Don't change the lines below
fig, ax = plt.subplots()
ax.set_title("Elevator Rides")
ax.set_xlabel("Floors")
ax.set_ylabel("Seconds")
# 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