MACHINE LEARNING • LESSON 1 • BUILD IT

Build It With Python

We have learned what Machine Learning is, what a model means, and how training and prediction are different. Now let's turn those ideas into a small Python program.

Can we build a simple prediction system ourselves?

Yes. We will use a small house-price example to understand the basic workflow before using a Machine Learning library.

What Are We Building?

We want to estimate the price of a house from its size.

For example, if we know the size of a new house, we want our program to produce an estimated price.

INPUT House Size

1,400 sq ft

OUR MODEL Learned Relationship

Size → Price

OUTPUT Estimated Price

Prediction

Step 1: Create Some Training Data

First, we need examples.

Each example contains a house size and its known price.

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

We will represent these examples in Python using two lists.

Step 2: Put the Data Into Python

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

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

Contains the house sizes we already know.

prices

Contains the corresponding known prices.

The values at the same position belong to the same house.

sizes[0] 800 prices[0] 300000

Step 3: Find a Simple Relationship

For this first Python example, we will make the problem deliberately simple.

We will calculate a simple average price per square foot from the training examples.

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

print(price_per_sqft)

The program adds all the known prices, adds all the known sizes, and calculates a simple overall price per square foot.

SIMPLE RELATIONSHIP price per sq ft = total price ÷ total size
Important:

This is intentionally a simple educational approach. It is not the same as training a professional regression model.

Step 4: Give the Program a New House

Now we have a new house that was not part of our training examples.

NEW HOUSE 1,400 sq ft

We can use the relationship we calculated to estimate its price.

Python
new_house_size = 1400

predicted_price = new_house_size * price_per_sqft

print(predicted_price)

Step 5: Put Everything Together

Here is the complete program.

Complete Python Program
# 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)

What Is This Program Doing?

01 Training Data

Existing house sizes and prices.

02 Learn Relationship

Calculate a simple price-per-square-foot relationship.

03 Learned Value

Store the calculated relationship.

04 New Input

1,400 sq ft house.

05 Prediction

Estimate its price.

Where Is the Model?

This is an important question.

In our simplified program, the learned relationship is represented by:

LEARNED VALUE price_per_sqft

We can think of this value as the small piece of learned information that our program uses to make the prediction.

But don't confuse this with a full Machine Learning model.

We are building intuition here. Real Machine Learning algorithms can learn much more complex relationships and usually contain multiple learned parameters.

Run the Program

Save the code in a Python file, for example:

house_price_prediction.py

Then run it from your terminal:

Terminal
python house_price_prediction.py

You should see a predicted price printed by the program.

Try Changing the New House

Change:

Python
new_house_size = 1400

to:

Python
new_house_size = 1800

Run the program again.

OBSERVE What happens to the predicted price?

The prediction changes because the new input changed.

This Is Not Yet a Real ML Library Model

We need to be precise here.

The program we built is useful for understanding the basic idea of learning a relationship and using it for prediction.

But it is not a replacement for algorithms such as Linear Regression from a Machine Learning library.

What Our Example Does

Calculates one simple relationship and uses it to estimate a price.

What Real ML Can Do

Learn more complex relationships using established algorithms and multiple features.

Connect This Back to What We Learned

DATA Examples
TRAINING Find Relationship
MODEL Learned Information
NEW DATA 1,400 sq ft
PREDICTION Estimated Price
KEY IDEA

We just turned the ML workflow into Python.

We started with examples, calculated a simple relationship, stored the learned information, and used it to make a prediction for a new house.

NEXT TOPIC

Understand the Python Code

We have a working program. Now we'll go through the code piece by piece and understand exactly what each line is doing.