MACHINE LEARNING • LESSON 1 • BUILD IT

Understand the Python Code

We just built a simple house-price prediction program. Now let's slow down and understand exactly what each part of the code is doing.

Don't just copy the code. Understand what each line means.

The goal of this page is to connect every Python statement with the Machine Learning concepts we learned earlier.

The Complete Code

First, let's look at the complete program again.

Python
# Training data

sizes = [800, 1000, 1200, 1500]

prices = [300000, 400000, 500000, 650000]


# Learn a simple relationship

price_per_sqft = sum(prices) / sum(sizes)


# New house

new_house_size = 1400


# Make a prediction

predicted_price = new_house_size * price_per_sqft


print("Predicted price:", predicted_price)
STEP 01

Create the Training Data

Python
sizes = [800, 1000, 1200, 1500]

This creates a Python list called sizes.

The list contains the sizes of houses that we already know about.

WHAT IS INSIDE?
800 sq ft
1000 sq ft
1200 sq ft
1500 sq ft

These are examples from our training data.

STEP 02

Create the Known Prices

Python
prices = [300000, 400000, 500000, 650000]

This creates another list called prices.

Each price corresponds to the house at the same position in the sizes list.

SIZE PRICE
800 $300,000
1,000 $400,000
1,200 $500,000
1,500 $650,000

For example:

sizes[0] 800 prices[0] 300000
STEP 03

Learn a Simple Relationship

Python
price_per_sqft = sum(prices) / sum(sizes)

This is the most important line in our simple example.

We are calculating an overall price-per-square-foot relationship from the examples we have.

THE CALCULATION Total Prices ÷ Total Sizes

What does sum() do?

Python's sum() function adds all values in a list.

sum(sizes)

adds:

800 + 1000 + 1200 + 1500

Similarly:

sum(prices)

adds all the known house prices.

What are we doing conceptually?

We are using the examples to calculate a simple relationship between house size and price.

STEP 04

Store the Learned Value

The result of the calculation is stored in:

Python
price_per_sqft

This variable now contains the simple relationship we calculated from the training examples.

LEARNED INFORMATION price_per_sqft

A simple value representing the relationship between size and price.

Later, we will use this value when making a prediction.

STEP 05

Create a New Input

Python
new_house_size = 1400

This represents a new house.

NEW INPUT 1,400 sq ft

Notice that 1,400 was not part of our original training examples.

This is important because we want to use what we learned from existing examples to estimate the price of a new house.

STEP 06

Make the Prediction

Python
predicted_price = new_house_size * price_per_sqft

This line takes the new house size and applies the relationship we calculated earlier.

NEW INPUT 1,400
×
LEARNED VALUE price_per_sqft
=
PREDICTION predicted_price

The result is stored in the variable predicted_price.

STEP 07

Display the Prediction

Python
print("Predicted price:", predicted_price)

Python's print() function displays information in the terminal.

Predicted price: 542857.14

The exact number depends on the training data and calculation.

Now Look at the Whole Process

01 Training Data

sizes + prices

02 Calculate

Find relationship

03 Learned Value

price_per_sqft

04 New Input

1,400 sq ft

05 Prediction

predicted_price

What Does Each Variable Mean?

VARIABLE PURPOSE
sizes

Known house sizes used as training examples.

prices

Known prices corresponding to those houses.

price_per_sqft

The simple relationship calculated from the training examples.

new_house_size

The new input we want to make a prediction for.

predicted_price

The output produced from the new input.

Which Lines Are Training and Which Are Prediction?

TRAINING / LEARNING
sizes = [800, 1000, 1200, 1500]

prices = [300000, 400000, 500000, 650000]

price_per_sqft = sum(prices) / sum(sizes)
PREDICTION
new_house_size = 1400

predicted_price = \
    new_house_size * price_per_sqft
Remember the difference.

The first part uses existing examples to calculate the relationship. The second part uses that relationship with new input.

Is This How Real Machine Learning Works?

Not exactly.

This program is intentionally simplified so that you can see the basic idea without a Machine Learning library hiding the mathematics.

What We Built

A simple relationship calculated directly from our example data.

Real Machine Learning

Algorithms can learn multiple parameters and much more complex relationships from data.

Later in the course, we will use proper Machine Learning algorithms such as Linear Regression.

Common Beginner Confusions

Is sizes the model?

No. It contains training examples.

Is new_house_size training data?

No. It is new input used for prediction.

Does prediction change the model?

Not in this program. The prediction simply uses the learned relationship.

Is this a complete ML algorithm?

No. It is a simplified example designed to teach the basic workflow.

KEY TAKEAWAY

Understand the code, not just the syntax.

The important thing is the connection: training examples provide information, a simple relationship is calculated, and that relationship is then used with new input to produce a prediction.

NEXT TOPIC

Try It Yourself

Now that you understand every part of the program, it's time to change the data, run the program again, and see how those changes affect the prediction.