topskill.dev
CoursesTracks
All courses/Machine Learning FundamentalsTry a tiny linear model
Supervised prediction algorithms
2 / 10
← Machine Learning Fundamentals
○What machine learning learns○Features and labels○Training and inference○Regression or classification○The machine learning process○Data quality and coverage○Representing data✦Module Quiz
○Linear regression●Try a tiny linear model○Logistic regression○Nearest neighbors○Decision trees○Random forests○Boosting○Naive Bayes○Support vector machines✦Module Quiz
○Clustering○K-means○Choosing and interpreting clusters○Principal component analysis○Neural networks and deep learning○Self-supervised learning○Transfer learning○Reinforcement learning✦Module Quiz
○Generalization and overfitting○Holding out evaluation data○Data leakage○Baselines○Bias and variance intuition○Reading learning curves✦Module Quiz
○Prediction errors and residuals○Measuring regression error○Squared error and large misses○The confusion matrix○Precision and recall○Try a held-out classifier✦Module Quiz
○Bias and limits○Monitoring and drift○Comparing models for a decision✦Module Quiz

Try a tiny linear model

The previous lesson described a line with three additional minutes per kilometer and an eight-minute starting value. Here the model learns that relationship from four invented trips instead of receiving those two numbers directly.

This short demonstration uses familiar NumPy arrays and one model from scikit-learn. Each row of distance is one trip, and its single column is the distance feature. minutes holds the target answers in the same order. The double brackets preserve the rows-and-columns shape even though there is only one feature.

LinearRegression() creates an unfitted model. fit(distance, minutes) learns from the paired examples. predict(...) applies the fitted model to a new row; it does not train on that row. Press Run beneath the example to see the estimate.

python
import numpy as np
from sklearn.linear_model import LinearRegression

# Invented trips: one distance feature per row.
distance = np.array([[1.0], [2.0], [3.0], [5.0]])
minutes = np.array([11.0, 14.0, 17.0, 23.0])

model = LinearRegression()
model.fit(distance, minutes)
prediction = model.predict(np.array([[4.0]]))
print(f"Predicted minutes: {prediction[0]:.1f}")

The result is 20.0 minutes at four kilometers. The .1f formatting prints one decimal place, and [0] selects the only prediction in the returned array.

These invented times lie exactly on a line so that the mechanism is easy to see. The four-kilometer input is new, but this demonstration has no independent observed answer for it. It shows how fitting and prediction differ; it does not measure model quality. Later lessons introduce held-out evaluation.

Before continuing, identify the line that learns and the line that only uses what was learned. They are fit and predict, respectively.

Fact 1
1 / 1
FACT 1

Follow which arrays enter fit; predict uses an already fitted model.