
📎 This article includes 3 downloadable practice files ↓
A normal program follows rules you write: if area > 1000 then…. A machine learning model works the other way round. You show it many past examples with the answers, and it works out the rule itself. Then you use that rule on new cases.
In this article
The five words you need
- Features (X): the inputs, such as area, bedrooms, city.
- Target (y): the answer you want to predict, such as price.
- Training (fit): the model studies examples to find the pattern.
- Prediction (predict): the model applies what it learned to new rows.
- Regression vs classification: predicting a number (price) is regression; predicting a category (will leave / will stay) is classification.
Install
pip install scikit-learn pandas openpyxl joblib
Check with python -c "import sklearn; print(sklearn.__version__)". Any recent version works.
The data
flats.csv has 400 flat sales: city, area in sq ft, bedrooms, age, floor and the price in lakh. In this lesson we only use Pune flats and only one feature, area, so you can see exactly what the model learns.
The script
# Lesson 1: your first model - predict a flat's price from its area
import pandas as pd
from sklearn.linear_model import LinearRegression
flats = pd.read_csv("flats.csv")
pune = flats[flats["city"] == "Pune"]
X = pune[["area_sqft"]] # input (feature): always a table, so double brackets
y = pune["price_lakh"] # answer (target)
model = LinearRegression()
model.fit(X, y) # "learning" = finding the best line through the points
print(f"Each extra sq ft adds about Rs {model.coef_[0] * 100000:,.0f}")
print(f"Starting value (intercept): {model.intercept_:.1f} lakh")
new = pd.DataFrame({"area_sqft": [650, 1000, 1500]})
for area, p in zip(new["area_sqft"], model.predict(new)):
print(f"{area} sq ft -> predicted {p:.1f} lakh")
What it printed
Each extra sq ft adds about Rs 7,551
Starting value (intercept): -3.1 lakh
650 sq ft -> predicted 46.0 lakh
1000 sq ft -> predicted 72.4 lakh
1500 sq ft -> predicted 110.2 lakh
The model found a straight line: price is roughly intercept + slope × area. Each extra square foot adds about ₹7,551 in Pune in this data. The intercept is negative, which makes no sense for a 0 sq ft flat; it is just where the line crosses zero. Never read meaning into a prediction far outside the range of the training data.
Try it
- Change the city to Jaipur. How does the price per sq ft change?
- Predict a 3,000 sq ft flat. Is that sensible when the data only goes up to 2,200?
Practice
Download the script and data file below into one folder, open a terminal there and run python lesson-NN.py. Your numbers should match the expected output exactly (the data and random seeds are fixed). Then change one thing (a column, a setting) and see what moves.
📎 Practice files for this article
⬇ Download all 3 files (ZIP · 4 KB)
- 🐍Lesson 1 scriptThe complete script from this lesson, tested.⬇ PY · 769 B
- 📄Expected outputExactly what the script printed when we ran it.⬇ TXT · 182 B
- 🧾flats.csv400 flat sales in Pune, Jaipur, Indore and Lucknow (price in lakh), with a few gaps to fill.⬇ CSV · 11 KB
Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.
Atul (AJ) builds office-automation and local-first AI tools as Atulya AI, and works with Excel, SAP and Oracle EPM every day. He writes about the shortcuts, macros and automations that actually save time — plus the occasional wacky fact. Videos on YouTube: Lazy Automator AI (@LazyAutomatorAI).
More posts by Atul →Stuck on a step? Ask a question and the AI answers using this article.