ProgramGuru

What is machine learning?

What is machine learning?

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.

What you’ll learn

  • Distinguish a rule written by a person from a model fitted to examples.
  • Identify the input and known outcome in a past sale.
  • Explain the separate jobs of training and prediction.
  • Calculate a simple estimate and compare it with a new actual sale.
  • Recognize why a neat training fit does not prove reliable real-world prices.

Learn from past sales. Estimate a new sale.

Training

Both the area and sale price are known.

Past sales

Area (m²)Sold for (USD)
50$150,000
75$200,000
100$250,000
125$300,000

Learned settings

Starting value
$50,000
Rate per m²
$2,000
Prediction

Only the new house’s area is supplied.

New house

90 m²

Actual sale price: unknown

Estimated price

$230,000

$50,000 + ($2,000 × 90)

Reuse the same settings

90 m²
The fitted settings stay the same when you change the new house’s area. Its actual sale price is not supplied. These fictional records illustrate the process; they do not establish reliable real-world prices.

A written rule versus a learned pattern

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.

What supplies the calculation?

Written policy

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.

Learned relationship

Estimate a future sale price

Known floor area
90 m²
Settings fitted from past sales
$50,000 + $2,000 × area
Estimated price
$230,000

Needs evaluation on new sales.

The fee follows an agreed policy. The price estimate follows a learned relationship and can be wrong.

Understand the house-sale records

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.

Four fictional sales in one neighborhood
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.

How training finds a relationship

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.

Two proposed lines compared with four fictional salesRecorded prices rise from 150,000 dollars at 50 square metres to 300,000 dollars at 125 square metres. Line A matches only the first sale. Line B passes through all four recorded points.Sale price (USD)$150k$200k$250k$300k5075100125Floor area (m²)
Dots are recorded sales. Dashed orange is line A; solid blue is line B. Here “k” means thousand dollars. Both lines and every comparison remain visible.

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.

Compare both proposed estimates with the same 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.

Predict the price of a new house

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.

Train and predict with Python

Prepare and run

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: 230000

Follow the program in order

  1. The imports provide NumPy’s array tools and scikit-learn’s LinearRegression class.
  2. X 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.
  3. y holds four target prices in the matching row order. The first price belongs to the first area, and so on.
  4. LinearRegression() creates an unfitted model. model.fit(X, y) uses both the inputs and their known prices to learn its settings.
  5. [[90]] describes one new row with one feature. predict returns an array of estimates; [0] selects the first estimate.
  6. intercept_ reads the learned starting value; coef_[0] reads the rate for the first—and only—feature. These attributes become available after fitting.
  7. 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.

Check a sale the model has not seen

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: 12000

Checking 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.

Try a prediction yourself

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?

Worked answer

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.

When machine learning helps—and its limits

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.

Quiz: identify the inputs and target

For this task, which pairing correctly describes one training example?
Choose one answer. You can try again.

Answer and explanation

Floor area as input; actual sale price as the known target.

The area is the information used to estimate the price. A past example includes its actual price so training can fit the relationship.

Quiz: training versus prediction

A model has already been fitted to the four past sales. What does predict([[90]]) do?
Choose one answer. You can try again.

Answer and explanation

Uses the learned relationship to estimate a price for a 90 m² house.

The new input is one row containing one area. Predict applies the existing model and returns an estimate; it does not retrain it.

Quiz: can we trust this model?

The model exactly matches all four invented training sales. What is the most useful next check?
Choose one answer. You can try again.

Answer and explanation

Compare estimates with actual prices from separate, representative sales.

New outcomes provide evidence about behavior beyond the training examples. Enough representative cases and suitable error measures are needed to judge usefulness.

Recap: explain the learning process

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.

    Next lesson: Supervised, unsupervised and reinforcement learning →