Machine Learning Lesson 1: What ML Is and Your First Model

Machine Learning Lesson 1: What ML Is and Your First Model featured illustration

📎 This article includes 3 downloadable practice files ↓

⏱ 4 min read

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
  1. The five words you need
  2. Install
  3. The data
  4. The script
  5. What it printed
  6. Try it
  7. Practice
  8. Continue learning

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.

Advertisement

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.

💡 Every scikit-learn model works the same way: create it, call fit(X, y), call predict(new X). Once you know this pattern you can try any model by changing one line.
⚠️ X must be a table (DataFrame or 2-D array), even with one column. pune[“area_sqft”] (single brackets) gives a Series and fit will complain; use pune[[“area_sqft”]].

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.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong