Calculate the service fee
- Known sale price
- $200,000
- Policy supplied by a person
- 1% of the sale price
- Calculated fee
- $2,000
No training data needed.
Imagine you want to estimate the price of a house before it sells. You have records of earlier sales, including each house’s floor area and final price. Could a program use those examples to estimate the price of another house?
Machine learning is a way of building computer systems that learn patterns from examples. Many of these systems use the learned pattern to make predictions for new cases. We call the learned representation a model. Here, its job is to estimate a price from a floor area.
In this lesson, all sales are fictional, all prices are in US dollars, and floor area is measured in square metres (m²). We place the houses in one neighborhood to keep the story manageable. The deliberately tidy numbers help us inspect the process; real sales are less predictable.
Both the area and sale price are known.
| Area (m²) | Sold for (USD) |
|---|---|
| 50 | $150,000 |
| 75 | $200,000 |
| 100 | $250,000 |
| 125 | $300,000 |
Only the new house’s area is supplied.
90 m²
Actual sale price: unknown
$230,000
$50,000 + ($2,000 × 90)
Reuse the same settings
Some tasks already have an exact rule. If a house-sale service charges a fee of 1% of the sale price, a programmer can write that calculation directly: a $200,000 sale has a $2,000 fee. No training examples are needed to discover a policy the service has already specified.
Estimating a future sale price is different. There is no official rule saying every 90 m² house must sell for one particular price. We can use past sales to fit a relationship and then check whether its estimates are useful.
Both approaches still involve programming. For machine learning, people define the task, choose the measurements, select a learning method and evaluate the result. The program learns numerical settings within that design; it does not decide the entire problem for itself.
No training data needed.
Needs evaluation on new sales.
Each row below describes one past house sale. Its floor area is the information we supply to the model. Its sale price is the known outcome we want the model to learn to estimate. Keeping the two values together is essential: a price paired with the wrong house teaches the wrong relationship.
| Floor area (m²) | Actual sale price (USD) |
|---|---|
| 50 | $150,000 |
| 75 | $200,000 |
| 100 | $250,000 |
| 125 | $300,000 |
An input measurement is called a feature; here there is one feature, floor area. The outcome being predicted is called the target; here it is sale price. The collection of examples is the dataset. These are names for roles you can already see in the table.
Because these training examples include known target values, this is supervised learning. Predicting a numeric quantity such as price is a regression task. Other tasks can predict categories or find groups; the types of machine learning lesson explains those differences.
For the new house, we know the area is 90 m² but do not yet know its final sale price. That unknown price must not be handed to the model as an input. Our estimate should use information available when we actually need it.
First, we choose the kind of relationship the program is allowed to fit. For this small example, we choose a straight line: estimated price = starting value + rate × floor area. This choice is called the model family. It limits the relationships this model can represent.
The learning procedure then chooses the two numerical settings using the past sales. The starting value is called the intercept, and the rate is called the slope. These learned settings are the model’s parameters. A different set of sales could produce different settings.
Line B matches all four training prices. This does not tell us whether it will predict a new sale accurately.
Scroll the table sideways to compare both lines with the recorded prices.
| Area (m²) | Actual (USD) | Line A (USD) | Line B (USD) |
|---|---|---|---|
| 50 | $150,000 | $150,000 | $150,000 |
| 75 | $200,000 | $175,000 | $200,000 |
| 100 | $250,000 | $200,000 | $250,000 |
| 125 | $300,000 | $225,000 | $300,000 |
Line A uses a starting value of $100,000 and a rate of $1,000 per m². For the 100 m² house, it estimates $200,000 while the recorded price is $250,000: it misses by $50,000. Line B uses $50,000 and $2,000 per m²; it matches all four invented prices.
The comparison illustrates what fitting is trying to accomplish. The Python library does not simply click between these two candidates or guess numbers until they look right. Its linear-regression procedure chooses settings that minimize the sum of squared differences between predicted and recorded prices. You do not need to derive that calculation here; the linear regression lesson explores it further.
Training is the process that fits those settings. In our code, fit performs it. Fitting the examples perfectly is possible here because we invented them to lie on a line; it is not a promise about real housing data.
After training, our fictional model has learned this relationship: estimated price = 50,000 + 2,000 × floor area. To make a prediction, we keep those settings fixed and supply a new area. The new house’s actual sale price is not required for this step.
New floor area: 90 m²
Area contribution: 2,000 × 90 = $180,000
Estimated price: $50,000 + $180,000 = $230,000
This is a prediction, not a guarantee or a professional valuation. In the code, predict applies the fitted model. Requesting another prediction does not automatically retrain it; updating a deployed model requires a separate, deliberate process.
The $50,000 starting value is a mathematical setting of this line, not evidence that a zero-area house would sell for $50,000. Our examples cover 50–125 m². Predictions far beyond the observed range need particular caution.
Use the course Python environment with NumPy and scikit-learn. The data is included below; no file or download is needed. If arrays are unfamiliar, review rows, columns and array shapes. You can understand the preceding explanation without running this program.
Python · complete runnable example
import numpy as np
from sklearn.linear_model import LinearRegression
# Four fictional past sales: floor area in square metres.
X = np.array([[50], [75], [100], [125]])
# Corresponding sale prices, in US dollars, in the same row order.
y = np.array([150000, 200000, 250000, 300000])
model = LinearRegression()
model.fit(X, y)
new_house = [[90]]
prediction = model.predict(new_house)[0]
print("Starting value:", round(model.intercept_))
print("Dollars per extra square metre:", round(model.coef_[0]))
print("Predicted price for 90 square metres:", round(prediction))Expected output
Starting value: 50000
Dollars per extra square metre: 2000
Predicted price for 90 square metres: 230000X holds four input rows, with one area in each row. The inner brackets in [[50], [75], [100], [125]] represent a four-row, one-column feature table.y holds four target prices in the matching row order. The first price belongs to the first area, and so on.LinearRegression() creates an unfitted model. model.fit(X, y) uses both the inputs and their known prices to learn its settings.[[90]] describes one new row with one feature. predict returns an array of estimates; [0] selects the first estimate.intercept_ reads the learned starting value; coef_[0] reads the rate for the first—and only—feature. These attributes become available after fitting.round formats these results as whole-dollar numbers here. It does not change the fitted model.The output connects directly to the calculation: starting value 50,000, rate 2,000 and estimate 230,000. The variable names X and y are common conventions, not special Python keywords.
Suppose a separate 90 m² house later sells for $242,000. It was not among the four training sales. We can now compare the earlier estimate of $230,000 with this newly known outcome. The model underestimated the sale by $12,000.
Prediction: $230,000
Actual sale: $242,000
Absolute difference: |242,000 − 230,000| = $12,000
The complete Python program below repeats the training data so you can inspect the comparison in this section. The new actual price is used only for checking the result; it is not supplied to fit.
Python · complete runnable example
import numpy as np
from sklearn.linear_model import LinearRegression
# Four fictional past sales: floor area in square metres.
X = np.array([[50], [75], [100], [125]])
# Corresponding sale prices, in US dollars, in the same row order.
y = np.array([150000, 200000, 250000, 300000])
model = LinearRegression()
model.fit(X, y)
# This separate house was not included in X or y.
predicted_price = model.predict([[90]])[0]
actual_price = 242000
absolute_error = abs(actual_price - predicted_price)
print("Predicted price:", round(predicted_price))
print("Actual sale price:", actual_price)
print("Difference:", round(absolute_error))Expected output
Predicted price: 230000
Actual sale price: 242000
Difference: 12000Checking predictions against outcomes the model did not train on is part of evaluation. The purpose is to learn how well the relationship carries over to new cases. A single sale explains the idea but cannot establish reliability: useful evaluation needs enough representative cases and a comparison with a simple alternative.
Whether a $12,000 difference is acceptable depends on the intended use. A rough browsing estimate and a consequential lending decision have different requirements. The training and test data lesson explains how to keep evaluation examples separate.
Your task: Use this already fitted relationship: estimated price = 50,000 + 2,000 × area. What price does it predict for an 80 m² house? If that house later sells for $218,000, how far did the estimate miss? Would making this prediction itself change the fitted settings?
The area contribution is 2,000 × 80 = $160,000. Adding the starting value gives a predicted price of $210,000. The actual price is $8,000 higher, so the absolute difference is $8,000. Prediction uses the existing settings; it does not update them.
Now question the original fit: the model matched all four training prices. Does that prove it will estimate real house sales reliably?
No. We deliberately constructed those rows to follow a line. Reliable use would require evaluation on separate, representative sales, suitable inputs and a decision about acceptable errors. A neat pattern in this dataset also does not prove that physically adding one square metre would cause a house’s price to rise by exactly $2,000.
House area can be informative, but real prices also depend on location, condition, age, sale date and other factors. Two equally sized houses can sell for different amounts. A model using only area cannot distinguish those differences; collecting more relevant measurements may help, but does not guarantee accuracy.
A learning system does not understand a house in the way a buyer does. It applies a relationship fitted within a chosen design. Incorrect records, unrepresentative examples and changes in the market can make its predictions unreliable. Data about one neighborhood should not automatically be treated as evidence about every neighborhood.
Learning is useful when examples can reveal a relationship that is difficult to express as a dependable fixed rule. Besides prices, systems may classify emails as spam or recognize objects in images. These tasks require different inputs and models; see machine learning applications.
Use ordinary programmed rules for exact policies and calculations, such as the agreed 1% service fee. Do not introduce a trained model where the correct operation is already specified. In either approach, people still need to validate inputs, handle failures and consider the consequences of wrong results.
This introduction establishes the process. The later house-price project with a real dataset adds a larger workflow; the linear regression lesson adds mathematical and diagnostic detail.
For this supervised example, each past sale pairs a floor area with a known price. Training fits a model to those pairs. Prediction supplies a new area and uses the existing model to produce an estimate. Evaluation compares estimates with outcomes from cases the model did not train on.
Our fictional line is estimated price = 50,000 + 2,000 × area. It estimates $230,000 for 90 m². The separate actual sale of $242,000 differs by $12,000. That difference reminds us that a fitted relationship is an estimate whose usefulness must be checked.
Transfer the idea: suppose a delivery service estimates journey time from earlier trips. What could be an input, what would be the target, and what would count as a new example?
Distance could be an input; the recorded journey duration is the target. A later trip not used in training can help check the estimate. Traffic and route conditions show why distance alone may not be enough.
Technical reference: scikit-learn’s LinearRegression training and prediction API. Its fitting objective supports this example; a manual two-line comparison is an explanation, not its implementation.
Reaching the recap and completing the quizzes are separate milestones.