What We Are Building
In this tutorial you will build a real machine learning model that predicts house prices based on features like size, number of bedrooms, and location. By the end you will understand the full ML workflow — from loading data to making predictions — using Scikit-learn, the industry standard ML library for Python.
What is Machine Learning?
Machine learning is teaching a computer to find patterns in data so it can make predictions on new data it has never seen before. Instead of writing rules manually — "if size > 100 sqm then price > 5M" — you show the model thousands of examples and it figures out the rules itself.
Step 1 — Install and Import Libraries
pip install scikit-learn pandas numpy
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
Step 2 — Load and Explore the Data
df = pd.read_csv("house_prices.csv")
print(df.head())
print(df.shape)
print(df.describe())
Always explore your data before building any model. Check for missing values, understand the range of each column, and look for anything unusual. A model built on bad data will always produce bad predictions — garbage in, garbage out.
Step 3 — Prepare Features and Target
In machine learning, X is your features (the inputs) and y is your target (what you want to predict):
X = df[["size_sqm", "bedrooms", "bathrooms", "distance_to_city"]]
y = df["price"]
Step 4 — Split Into Training and Test Sets
This is one of the most important steps. You train your model on one part of the data, and test it on a completely separate part it has never seen. This tells you how well your model will perform on real, new data:
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print(f"Training samples: {len(X_train)}")
print(f"Test samples: {len(X_test)}")
test_size=0.2 means 20% of the data goes to testing, 80% to training. random_state=42 makes the split reproducible — you get the same split every time you run the code.
Step 5 — Train the Model
model = LinearRegression()
model.fit(X_train, y_train)
That's it. Two lines. model.fit() is where the actual learning happens — the model looks at thousands of examples and figures out the relationship between the features and the price.
Step 6 — Evaluate the Model
predictions = model.predict(X_test)
mse = mean_squared_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
print(f"Mean Squared Error: {mse:,.0f}")
print(f"R² Score: {r2:.3f}")
The R² score tells you how much of the variation in price your model explains. An R² of 0.85 means your model explains 85% of price variation — that's a solid result for a first model. Anything above 0.7 is generally considered good.
Step 7 — Make Predictions on New Data
new_house = pd.DataFrame({
"size_sqm": [120],
"bedrooms": [3],
"bathrooms": [2],
"distance_to_city": [5]
})
predicted_price = model.predict(new_house)
print(f"Predicted price: KES {predicted_price[0]:,.0f}")
What to Try Next
You have just built, trained, and evaluated your first machine learning model. The next steps are to try a different algorithm like Random Forest, add more features to improve accuracy, handle missing values and categorical columns, and learn about cross-validation for more reliable evaluation. If you want to work through these next steps with a tutor who explains every decision — book a session and we will build on this together.
Want Help Applying This?
Book a 1-on-1 session and we'll work through this topic together with your actual code and data.
Book a Session