ITD 140 · ML I
Module 1 · Reading

1.4 Your First Model: y = mx + b

Your First Model: y = mx + b

What you'll learn on this page
  • How a simple linear equation is a machine learning model
  • What "training" actually does — finds m and b
  • Your first taste of scikit-learn code

The simplest model in the world

You already know the equation of a line:

\\[ y = mx + b \\]

This is a model. Give it an x, it gives you back a y. The numbers m (slope) and b (intercept) are the model — they're what gets "learned" during training.

Concrete example: Celsius to Fahrenheit

Pretend you don't know the conversion formula. You only have a bunch of paired observations from a thermometer that shows both scales:

Celsius (x) Fahrenheit (y)
032
1050
2068
3798.6
100212

Plot it:

y = 1.8x + 32

A line fits these points perfectly. The slope m1.8 and intercept b32. The model is y = 1.8x + 32.

Key Insight
Training is just the process of finding the best m and b from data. The model isn't the equation — the equation is the same for every line. The model is the specific numbers m=1.8 and b=32 that came out of training.

Doing it in scikit-learn (your first ML code)

import numpy as np
from sklearn.linear_model import LinearRegression

# The data — Celsius and Fahrenheit
X = np.array([[0], [10], [20], [37], [100]])  # inputs (note the brackets!)
y = np.array([32, 50, 68, 98.6, 212])         # outputs

# Create the model
model = LinearRegression()

# Train it (this is where m and b are found)
model.fit(X, y)

# Inspect what was learned
print(f"m (slope)     = {model.coef_[0]:.4f}")
print(f"b (intercept) = {model.intercept_:.4f}")

# Use it to predict
print(f"25°C is predicted to be {model.predict([[25]])[0]:.1f}°F")

Running this prints:

m (slope)     = 1.8000
b (intercept) = 32.0000
25°C is predicted to be 77.0°F

That's it. You just trained a machine learning model. The whole course is going to feel like this same pattern — load data, create a model, call .fit(), call .predict() — over and over with different algorithms.

Try it interactively

Open TensorFlow Playground and watch a neural network "find the rule" for a problem you can't draw a single line through. Set the dataset to the spiral (bottom right) and click play. Notice how the network's "rule" gets more complex as training progresses — it's doing the same fundamental thing as our Celsius example, just with thousands of parameters instead of two.

A subtle but important point

Pay attention to the shape of X
Notice we wrote X = np.array([[0], [10], [20], ...]) — each value is in its own list. scikit-learn always expects inputs as a 2D array (rows = examples, columns = features), even when there's only one feature. Forgetting this is the most common beginner error and produces a confusing error message. We'll see why this matters in Module 4.

Check your understanding

  1. In y = mx + b, after training, what part is "the model"?
  2. If you had only one data point (say, 20°C = 68°F), could you train this model? Why or why not?
  3. What does model.fit(X, y) actually do, in plain English?
Show answers
  1. The specific values of m and b that came out of training. The equation form is the same for every line — only the numbers are learned.
  2. No — with one point, infinitely many lines pass through it. You need at least 2 points to define a line (and more for the model to be confident).
  3. It finds the values of m and b that make the line pass as close as possible to all the data points at once.