📖 Chapter 8 of 12 · AI for New Learner- Senior 1 · Class 9
Chapter 8: Your First Real ML Model
In this chapter
- Veer from Surat — trains his first scikit-learn model.
- Scikit-learn — the library that runs 80% of Indian ML projects.
- Decision Tree from scratch — easy to read, easy to debug.
- Train, predict, score — all in 9 lines of Python.
- Your first model deployed on a real Indian dataset.
The Big Idea
Everything so far has led to this chapter. You will train a REAL machine-learning model. It will make REAL predictions. With 9 lines of Python and scikit-learn, you will accomplish more than 90% of people who claim to ‘do AI’. This is the moment you stop being a student and start being a builder. Welcome.
Did you know?
Scikit-learn was started in 2007 by David Cournapeau as a Google Summer of Code project. Today it powers ML at companies from Spotify to ICICI Bank. It is the most-used ML library in the world — and learning it is your direct ticket to an Indian AI internship.
Builder Profile: Veer
Surat, Gujarat · Veer, age 14
Veer is from Surat. His father owns a small diamond-cutting business. After watching Senior 1 of this book, he decided to try sklearn — the library his cousin (now at IIT-B) had told him about.
His first model was on the Iris dataset — the ‘Hello World’ of ML. He typed 9 lines of code. He pressed Enter. The model trained in 0.03 seconds and got 97% accuracy on flowers it had never seen. Veer stared at the screen for a full minute.
“I had built a thinking machine,” he says. “In 9 lines. On a 30,000-rupee laptop. After that day I knew — there is nothing magical about AI. There is only data, code, and one library called sklearn. Now I’m trying it on real diamond-quality data from my father’s workshop.”
💻 CODE LAB first_model.py — your first model in 9 lines
from sklearn.datasets import load_iris # famous Iris dataset
from sklearn.tree import DecisionTreeClassifier # our model
from sklearn.model_selection import train_test_split
iris = load_iris() # 150 flowers, 4 measurements each
X, y = iris.data, iris.target # X = features, y = labels
X_tr, X_te, y_tr, y_te = train_test_split(
X, y, test_size=0.2, random_state=42) # split 80/20
model = DecisionTreeClassifier() # make an empty tree
model.fit(X_tr, y_tr) # TRAIN on the 80%
print(model.score(X_te, y_te)) # score on the unseen 20%
# Output: 0.97 (97% accuracy!)
✍ WORKED EXAMPLE Classify Iris Flowers in 9 Lines
Problem: Given four measurements, predict the iris species (setosa, versicolor, virginica) from 150 labelled flowers. Walk through training and evaluating a Decision Tree.
- IMPORT — bring in load_iris, DecisionTreeClassifier, and train_test_split from sklearn.
- LOAD — iris = load_iris(). iris.data has 150 rows × 4 columns; iris.target has 150 labels.
- SPLIT — train_test_split with test_size=0.2, random_state=42. Now 120 training, 30 test rows.
- BUILD — model = DecisionTreeClassifier(). Empty tree, no training yet.
- TRAIN — model.fit(X_train, y_train). The tree learns rules like ‘if petal_length < 2.5 → setosa’.
- EVALUATE — model.score(X_test, y_test) returns accuracy on the 30 unseen flowers.
- PREDICT — model.predict([[5.1, 3.5, 1.4, 0.2]]) returns the species for ONE new flower.
Show answer
✓ Answer: 97% accuracy on flowers the model has NEVER seen. You just built real ML.
Beyond Iris — real Indian data
Now do it on data you care about. Five Indian problems to build on: a cricket score predictor (last 5 years of IPL data, predict over-by-over runs); a crop disease classifier (the PlantVillage Indian images, 38 disease types); a mandi price forecaster (daily prices from agmarknet.gov.in); a Hindi text classifier (spam vs not-spam in Hindi SMS using Bhashini sample data); and a diamond grader (clarity from cut, colour, carat — a real industry use case).
⚠ COMMON MISTAKE
✗ Mistake: Testing on data you also trained on. Your accuracy will look amazing — and the model will fail in the real world.
✓ Fix: ALWAYS split into train and test BEFORE training. Use train_test_split with random_state. NEVER train on test data.
Solved illustrations
1. Why scikit-learn and not write the model from scratch?
Show answer
Answer: sklearn has been battle-tested by millions of engineers. Your hand-coded model has bugs — guaranteed. Real engineers use libraries. Save scratch-coding for LEARNING, not production.
2. What does test_size=0.2 mean?
Show answer
Answer: It tells train_test_split to put 20% of data aside for testing and use 80% for training. Standard convention. Smaller datasets sometimes use 0.3 (more test data for honest measurement).
3. What does random_state=42 do?
Show answer
Answer: Pins the random split. Same seed = same split every time. Without it, every run gives a different split, making bugs impossible to reproduce. Always set it during learning.
4. What is a Decision Tree?
Show answer
Answer: A model that asks yes/no questions in a tree shape. ‘Is petal length > 2.5?’ If yes, ask another question; if no, ask a different one. The leaves are predictions. Easy to read — you can SEE why the model decided.
5. My model is 97% accurate on Iris but 60% on my own data. Why?
Show answer
Answer: Iris is a CLEAN dataset — perfectly labelled, no missing values. YOUR data is dirty. 60% on dirty data is normal for a first attempt. Spend more time on cleaning (Ch 7!) — the model code stays the same.
Questions — Chapter 8
A. scikit-learn essentials
- The library used in this chapter is -.
- To train a model, call: model.(X_train, y_train).
- To get the accuracy, call: model.(X_test, y_test).
- test_size=0.2 means: % for testing, % for training.
- The ‘Hello World’ dataset of ML is the dataset.
B. Real model thinking
- Why pin random_state during learning?
- What is wrong with testing on training data?
- Why a Decision Tree before a Neural Network?
- Veer trained his model in 9 lines. Why is the 80% data work so important?
- Pick a problem from the list above. Sketch (in words) your 9-line model.
Ship-It Challenge
Run the 9-line Iris code on Google Colab today. Screenshot the accuracy. Save it.
Check your understanding — Chapter 8
Tap an answer to check it instantly. These are for practice — nothing is saved or sent.
Q1. Why use scikit-learn instead of writing the model from scratch?
- Scratch code is always better
- sklearn is battle-tested by millions; hand-coded models have bugs — save scratch-coding for learning, not production
- sklearn is not free
Show answer
B. sklearn is battle-tested by millions; hand-coded models have bugs — save scratch-coding for learning, not production
Q2. What does test_size=0.2 mean in train_test_split?
- Use 2 rows for testing
- Put 20% of data aside for testing and use 80% for training
- Test the model 2 times
Show answer
B. Put 20% of data aside for testing and use 80% for training
Q3. What does random_state=42 do?
- Makes the model 42% accurate
- Pins the random split so you get the same split every run, making bugs reproducible
- Adds 42 rows of data
Show answer
B. Pins the random split so you get the same split every run, making bugs reproducible
Q4. Your model is 97% on Iris but 60% on your own data. Why?
- Your computer is faulty
- Iris is a clean, perfectly labelled dataset; your own data is dirty — 60% on a first attempt is normal, so clean more
- sklearn is broken
Show answer
B. Iris is a clean, perfectly labelled dataset; your own data is dirty — 60% on a first attempt is normal, so clean more