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.
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.
1,400 sq ft
Size → Price
Prediction
Step 1: Create Some Training Data
First, we need examples.
Each example contains a house size and its known price.
We will represent these examples in Python using two lists.
Step 2: Put the Data Into Python
sizes = [800, 1000, 1200, 1500]
prices = [300000, 400000, 500000, 650000]
Contains the house sizes we already know.
Contains the corresponding known prices.
The values at the same position belong to the same house.
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.
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.
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.
We can use the relationship we calculated to estimate its price.
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.
# 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?
Existing house sizes and prices.
Calculate a simple price-per-square-foot relationship.
Store the calculated relationship.
1,400 sq ft house.
Estimate its price.
Where Is the Model?
This is an important question.
In our simplified program, the learned relationship is represented by:
We can think of this value as the small piece of learned information that our program uses to make the prediction.
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:
python house_price_prediction.py
You should see a predicted price printed by the program.
Try Changing the New House
Change:
new_house_size = 1400
to:
new_house_size = 1800
Run the program again.
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.
Calculates one simple relationship and uses it to estimate a price.
Learn more complex relationships using established algorithms and multiple features.
Connect This Back to What We Learned
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.