Chapter DTrain a tiny modelPage 1 of 8

Train a tiny model

Define the lab goal and success criteria

Training estimates model parameters from examples, while evaluation asks whether those learned parameters predict rows that were not used for fitting.

~14 minLab goal

1Try it yourself

Code Lab

Train a tiny model

Fit a simple line from toy examples, then predict.

Before you start

Why this matters

Before running anything, predict one observable result from the case: a small linear relationship between study hours and score must be fit with one row held out. Write the prediction beside the command or code line that should cause it. This makes the session an experiment rather than a transcription exercise.

2Learn the idea

Read

Explain the chapter step

Begin by writing the success condition in observable terms. For this case, success is not familiarity with the vocabulary; it is producing a fitted linear regression with a held-out prediction, mean absolute error, and recorded coefficient. Record the starting state so you can distinguish an improvement from a result that was already present.

On this page, the practical job is to state a measurable outcome before changing anything. The running case is a small linear relationship between study hours and score must be fit with one row held out.

Read

Run the working example

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error

X_train = [[1], [2], [3], [4]]
y_train = [52, 60, 68, 76]
X_test, y_test = [[5]], [84]

model = LinearRegression()
model.fit(X_train, y_train)
prediction = model.predict(X_test)
print(f"slope={model.coef_[0]:.1f}")
print(f"prediction={prediction[0]:.1f}")
print(f"mae={mean_absolute_error(y_test, prediction):.1f}")

Expected evidence:

slope=8.0
prediction=84.0
mae=0.0

The output may include version-specific details such as hashes, paths, fitted thresholds, or final decimal places. Compare the structural facts described here rather than copying placeholders. If the structure differs, stop and inspect the earliest unexpected line.

Read

Read it line by line

  1. scikit-learn expects X as rows by features, so each hour value sits inside a one-item list.
  2. fit estimates coefficient and intercept using only training rows.
  3. predict applies frozen parameters to the held-out row.
  4. mean absolute error measures the average magnitude of mistakes in the target's units.

These lines form one chain: training pairs of study hours and observed scores plus one untouched test pair becomes a fitted slope and intercept, one held-out prediction, and an error value. Change only one input first. When several values change together, you cannot tell which change caused the new behavior.

Read

Common errors and fixes

  • First failure: a one-dimensional X_train triggers an expected-2D-array error; reshape or use nested rows. Re-run the smallest command that proves the repair.
  • Second failure: calling predict before fit raises a not-fitted error. Preserve the failing input as a test when it represents a realistic mistake.
  • Misleading success: mixing the held-out row into fitting makes the evaluation optimistic and defeats the test. A clean-looking final line cannot cancel contradictory intermediate evidence.

When debugging, copy the exact error text and inspect names, paths, shapes, types, and versions. Explain the cause in one sentence before changing code. That discipline prevents a guessed repair from creating a second defect.

Read

Evidence for this stage

Use more than one test row for a credible metric, compare against a simple mean baseline, inspect residuals, and repeat with an outlier. Zero error on one manufactured point demonstrates mechanics, not production quality.

For the current explain step, save the smallest useful evidence: the relevant command, its output, and the input that produced it. Do not use a screenshot as the only record when text can be copied and searched. Keep generated artifacts separate from source inputs so rerunning the example does not destroy the evidence it is meant to evaluate.

Save code and dependency versions before considering model serialization. Publish the supported input shape, target units, evaluation set description, baseline comparison, and known extrapolation limits.

Read

Reflect on the result

Return to your opening prediction. Mark it correct or rewrite it with the condition you missed. Then explain the difference between a successful execution and a trustworthy result for this specific example.

Checking tutor…

Continue learning · glossary & guides
  1. Which line or command establishes the current step's most important fact?
  2. What output would reveal that a one-dimensional X_train triggers an expected-2D-array error?
  3. Can a new user reproduce a fitted linear regression with a held-out prediction, mean absolute error, and recorded coefficient from the stated setup?