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.
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.
# 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)
Create the Training Data
sizes = [800, 1000, 1200, 1500]
This creates a Python list called sizes.
The list contains the sizes of houses that we already know about.
These are examples from our training data.
Create the Known Prices
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.
For example:
sizes[0]
800
→
prices[0]
300000
Learn a Simple Relationship
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.
What does sum() do?
Python's sum() function adds all values in a list.
sum(sizes)
adds:
Similarly:
sum(prices)
adds all the known house prices.
We are using the examples to calculate a simple relationship between house size and price.
Store the Learned Value
The result of the calculation is stored in:
price_per_sqft
This variable now contains the simple relationship we calculated from the training examples.
A simple value representing the relationship between size and price.
Later, we will use this value when making a prediction.
Create a New Input
new_house_size = 1400
This represents a new house.
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.
Make the Prediction
predicted_price = new_house_size * price_per_sqft
This line takes the new house size and applies the relationship we calculated earlier.
The result is stored in the variable predicted_price.
Display the Prediction
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
sizes + prices
Find relationship
price_per_sqft
1,400 sq ft
predicted_price
What Does Each Variable Mean?
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?
sizes = [800, 1000, 1200, 1500]
prices = [300000, 400000, 500000, 650000]
price_per_sqft = sum(prices) / sum(sizes)
new_house_size = 1400
predicted_price = \
new_house_size * price_per_sqft
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.
A simple relationship calculated directly from our example data.
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
sizes the model?
No. It contains training examples.
new_house_size training data?
No. It is new input used for prediction.
Not in this program. The prediction simply uses the learned relationship.
No. It is a simplified example designed to teach the basic workflow.
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.