Your first machine learning model.
Today you'll train a real machine learning model that predicts flat prices, in about 15 lines of Python. You'll see the full process that professionals follow: split the data, train, predict and measure.
The ML recipe
- Collect data (features + label).
- Split into training data (to learn) and test data (to check).
- Choose a model and train it.
- Predict on the test data.
- Measure how good the predictions are.
Why split? If you test a student with the exact questions they practised, a high score proves nothing. Test data is the "new exam paper".
Model 1: predicting a number (regression)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
df = pd.DataFrame({
"area_sqft": [900,1000,1100,1200,1300,1400,1500,1650,1800,2000,1250,950],
"bedrooms": [2,2,2,2,3,3,3,3,3,4,2,2],
"price_lakh":[55,60,66,72,80,90,96,118,125,140,76,57],
})
X = df[["area_sqft", "bedrooms"]] # features
y = df["price_lakh"] # label
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)
model = LinearRegression()
model.fit(X_train, y_train) # learn
pred = model.predict(X_test) # predict
print("Average error (lakh):", mean_absolute_error(y_test, pred))
print("1600 sqft, 3 BHK:", model.predict(pd.DataFrame({"area_sqft":[1600],"bedrooms":[3]})))
Mean absolute error tells you, on average, how far predictions are from real prices. Smaller is better.
Model 2: predicting a category (classification)
Will a student pass? (1 = pass, 0 = fail)
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
df = pd.DataFrame({
"hours_studied": [1,2,3,4,5,6,7,8,2,9,3,7],
"attendance": [50,60,65,70,80,85,90,95,55,98,75,88],
"passed": [0,0,0,1,1,1,1,1,0,1,0,1],
})
X = df[["hours_studied", "attendance"]]
y = df["passed"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)
clf = DecisionTreeClassifier(max_depth=2)
clf.fit(X_train, y_train)
print("Accuracy:", accuracy_score(y_test, clf.predict(X_test)))
You want to predict next month's sales in rupees. Which type of problem is it?
Predicting a number is regression.
We used tiny datasets so the code is easy to read. Real models need much more data, and their results here will change a lot with small changes. That's normal, and an important lesson in itself.
Why do we keep test data separate from training data?
Testing on unseen data shows whether the model learned real patterns or just memorised.
Your task (45 min)
Run both models in Google Colab. Then change the data: add 5 more rows of your own realistic examples and re-run. Did the error or accuracy change? Write 3 lines explaining why small datasets give unstable results.
Lesson 11 complete!
You trained and evaluated a regression model and a classification model, and you understand train/test splits and accuracy. Next: using AI through an API, plus the Module 3 test.
Next: Using AI through an API →