33. Linear Regression in Python with sklearn
33.1 Why Predict a Number (Regression)
Why. Many India business questions are numbers: rent in ₹, daily pedha sales, delivery minutes. Classification says yes/no. Regression predicts a number.
What. Linear regression draws a straight-line style relationship: “as flat size goes up, rent tends to go up.” sklearn’s LinearRegression finds the best-fitting line (or plane) from your training rows.
How.
Spot a regression question
- Ask: “Is the answer a number I care about in ₹ / kg / minutes?”
- If yes → regression (this chapter). If yes/no or category → Chapter 34.
- Write one sentence: “I will predict ___ from ___.” Example: predict monthly rent from area_sqft and city tier (fictional Pune flats).
Worked example — fictional rent rows.
import pandas as pd
rent = pd.DataFrame({
"area_sqft": [450, 600, 750, 900, 1100],
"rent_inr": [12000, 15500, 19000, 22500, 27000],
})
print(rent)
print("Target y = rent_inr (a number). Feature X = area_sqft.")
What you should see. A tiny table where larger flats tend to cost more. Linear regression will learn that slope from data — not from a hand-typed formula.
Fictional data
Rents and flat sizes are teaching samples only, not real listings.
Ravindra Bagale's Tip
Remember this simple rule: regression predicts a number, and classification predicts a class or category. For example, predicting next month's sales in rupees is regression, while predicting whether a customer will leave is classification. Interviewers often ask this, so be ready to say it clearly in one line.
Ravindra Bagale's Tip – मराठी
हा सोपा नियम लक्षात ठेवा: regression एक number predict करतो, आणि classification एखादा class किंवा category predict करतो. उदाहरणार्थ, पुढच्या महिन्याची sales रुपयांत predict करणं म्हणजे regression, तर customer सोडून जाईल का हे predict करणं म्हणजे classification. Interview मध्ये हा प्रश्न अनेकदा विचारला जातो, म्हणून हे एका ओळीत स्पष्टपणे सांगता यायला हवं.
Ravindra Bagale's Tip – हिंदी
यह आसान नियम याद रखो: regression एक number predict करता है, और classification कोई class या category predict करता है. उदाहरण के लिए, अगले महीने की sales रुपयों में predict करना regression है, जबकि customer छोड़कर जाएगा या नहीं यह predict करना classification है. Interview में यह सवाल अक्सर पूछा जाता है, इसलिए इसे एक लाइन में साफ़ बोलने के लिए तैयार रहो.