✦ New Age Learning 🔍
Home › Library › AI for New Learner- Senior 2 › Chapter 3: Linear Models — The Workhorses

📖 Chapter 3 of 13 · AI for New Learner- Senior 2 · Class 10

Chapter 3: Linear Models — The Workhorses

In this chapter

  • Aditya from Vadodara — predicting cricket scores with linear regression.
  • Linear Regression: y = mx + c, but for the real world.
  • Logistic Regression: how linear models classify (yes/no).
  • When linear is enough — and when it is too simple.
  • Real Indian sklearn code for both models.

The Big Idea

Linear models are the OLDEST type of ML — and still the most useful for many problems. They are fast, easy to explain, hard to break. RBI uses them for credit scoring. Indian Railways uses them for demand forecasting. Sarvam uses them as baselines. NEVER skip a linear model and go straight to neural networks. The boring, simple thing often wins.

Did you know?

The maths behind linear regression was invented in 1805 by Adrien-Marie Legendre. 220+ years later, it still powers half of all production ML systems — including parts of Google Search and the UPI fraud check.

Builder Profile: Aditya

Vadodara, Gujarat · age 14

Aditya fits a linear-regression line to 200 matches — his score predictions land within 15 runs, 80% of the time.

Aditya loves cricket. His school team’s coach used to predict the opponent’s final score with vibes — “They will probably get 180 today.” Aditya thought: I can do better.

He collected 200 matches of school-tournament data: 7 features per match — opening partnership, average run rate, overs left, wickets in hand, pitch type, weather, opponent strength. The target: final score. He used sklearn’s LinearRegression. Five lines of code.

The model wrote a formula: final_score = 1.7×opening + 12×runrate + 8×oversleft − 14×wickets_lost + ... His predictions were within 15 runs 80% of the time. The coach was suspicious. “This is just averages.” Aditya smiled. “Exactly. That is why it works.” Today his model is on his school’s sports board before every match. The coach now consults it.

⊕ MATH MINUTE Linear Regression — The Core Idea

y = mx + c is a LINE. m is the slope, c is the intercept. Linear regression FINDS the m and c that make the line fit your data best. With many features (x1, x2, x3...) the formula grows — same idea, more variables. AI’s job: find the m’s and c that minimise the error.

y = m₁x₁ + m₂x₂ + ... + mₙxₙ + c

⊕ MATH MINUTE Logistic Regression — Squashed for Classification

What if y must be YES (1) or NO (0)? Use the SIGMOID function. It squashes any number to a value between 0 and 1 — perfect as a probability. Above 0.5 means YES; below 0.5 means NO. Used for spam detection, loan approval, disease prediction.

P(yes) = 1 / (1 + e^(−(m₁x₁ + m₂x₂ + c)))

💻 CODE LAB Linear Regression in Real sklearn

import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# Load data (rooms, area, age, price)
df = pd.read_csv('mumbai_houses.csv')
X = df[['rooms', 'area_sqft', 'age_years']]
y = df['price_lakhs']

# Split: train on 80%, test on 20%
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42)

model = LinearRegression()
model.fit(X_train, y_train)

score = model.score(X_test, y_test)   # test on UNSEEN data
print('R2 accuracy:', round(score, 3))
print('Predicted price:', model.predict([[3, 1200, 5]]))

⚙ TRY IT TODAY

After model.fit(), run print(model.coef_) and print(model.intercept_). These ARE the m’s and c. Which feature has the biggest weight? Which has the smallest? Does it match your intuition? Hint: larger area should have a higher coefficient; a negative coefficient means that feature LOWERS the price.

Solved illustrations

1. When should I use linear regression vs logistic regression?

Show answer

Answer: Linear regression predicts NUMBERS (price, score, temperature). Logistic regression predicts YES/NO categories (spam? loan approved?). Same maths underneath; different output.

2. What is train_test_split and why do we need it?

Show answer

Answer: It splits your dataset into a training set (80%) and a test set (20%). You train on 80% and EVALUATE on the unseen 20%. If you test on the same data you trained on, you cheat yourself — the model ‘memorises’ instead of learning.

3. What does R² (R-squared) mean?

Show answer

Answer: An accuracy measure for regression. 1.0 = perfect predictions. 0.0 = no better than predicting the average. Negative = worse than the average. Aim for 0.7+ on most problems. 0.95+ is suspicious — check for data leakage (Ch 6).

4. What is ‘random_state=42’ for?

Show answer

Answer: It makes the random split repeatable. Anyone running your code gets the same train/test split. Important for reproducibility — a core engineering practice. (Why 42? It’s a Hitchhiker’s Guide to the Galaxy reference. Programmer humour.)

5. What if my data is non-linear?

Show answer

Answer: Two options: (a) transform features — e.g. add x², x×y as new columns, then linear regression can fit them; (b) use trees or neural networks (Chapters 4 and 5). Always TRY linear first as a baseline.

Questions — Chapter 3

A. Compute (write the answers)

  1. If m₁=2, m₂=3, c=5, and x₁=4, x₂=1 — what is y?
  2. A sigmoid output of 0.82 — is this a ‘yes’ or ‘no’ prediction?
  3. R² of 0.3 — would you ship this model? Why or why not?
  4. test_size=0.25 — how many rows in the test set if the dataset has 800 rows?
  5. model.coef_ returns [12.3, -2.1, 8.7]. Which feature has the biggest impact?

B. Short answers

  1. Why train/test split? What goes wrong without it?
  2. Explain logistic regression to a 9-year-old in 2 sentences.
  3. List 2 cases where linear is a bad choice. What would you use instead?
  4. Aditya predicts cricket scores with linear. What features would YOU add?
  5. Write 5 lines of sklearn code to train a LogisticRegression on any data.

Ship-It Challenge

Find a CSV with 50+ rows. Train LinearRegression. Print R² and 3 predictions.

Check your understanding — Chapter 3

Tap an answer to check it instantly. These are for practice — nothing is saved or sent.

Q1. When do you use linear regression vs logistic regression?

  1. They are interchangeable
  2. Linear regression predicts NUMBERS (price, score); logistic regression predicts YES/NO categories
  3. Logistic is only for images
Show answer

B. Linear regression predicts NUMBERS (price, score); logistic regression predicts YES/NO categories

Q2. What does R² (R-squared) mean?

  1. The number of rows
  2. A regression accuracy measure: 1.0 = perfect, 0.0 = no better than predicting the average, negative = worse
  3. The learning rate
Show answer

B. A regression accuracy measure: 1.0 = perfect, 0.0 = no better than predicting the average, negative = worse

Q3. What does the sigmoid function do in logistic regression?

  1. Sorts the data
  2. Squashes any number to a value between 0 and 1 — perfect as a probability, with >0.5 meaning YES
  3. Removes outliers
Show answer

B. Squashes any number to a value between 0 and 1 — perfect as a probability, with >0.5 meaning YES

Q4. Why should you always try a linear model first?

  1. It is the only model that exists
  2. It is fast, explainable, hard to overfit, and often wins — never skip straight to neural networks
  3. Neural networks are banned
Show answer

B. It is fast, explainable, hard to overfit, and often wins — never skip straight to neural networks

All chapters in AI for New Learner- Senior 2

  1. Chapter 1: Where You Are Now — The ML Lifecycle
  2. Chapter 2: From Idea to Dataset
  3. Chapter 3: Linear Models — The Workhorses
  4. Chapter 4: Decision Trees & Random Forests
  5. Chapter 5: Neural Networks Demystified
  6. Chapter 6: Evaluation — Does It Actually Work?
  7. Chapter 7: Indian Datasets & Real Problems
  8. Chapter 8: Deploy Your AI — Get a Public URL
  9. Capstone Build-It Projects
  10. Final Book Exam
  11. AI / ML / Python Glossary
  12. Career & College Pathways
  13. Series Completion Diploma

Practise for Class 10