Lesson 01 � Beginner

LINEAR REGRESSION:
SEEDHI LINE SE
PREDICTION.

Linear regression sabse basic aur important ML algorithm hai. Ek straight line se data fit karte hain � jaise ghar ka size se price predict karna, ya salary se monthly expense. Equation hai y = mx + b � bas itna hi.

? 22 min✓ Beginner✓ Prerequisite: Supervised Learning

WHY: Linear regression kyun seekhein?

Linear regression ML ki foundation hai � ye algorithm data ke beech relationship samajhta hai aur future predictions deta hai. House price prediction, salary estimation, sales forecasting � sab linear regression se shuru hota hai. Agar ek variable doosre ko affect kar raha hai, toh linear regression quantify karta hai ki kitna affect kar raha hai.

FIT LINE (y = mx + b)

Model ki equation � straight line jo data points ke sabse close ho. m = slope (line kitni steep hai), b = intercept (line y-axis pe kahan milti hai).

COEFFICIENT (Slope)

X badhne par y kitna badhega. Agar coefficient 50 hai, toh X ka 1 unit badhne se y 50 units badhega. Direction bhi batata hai � positive ya negative.

INTERCEPT

Jab X zero ho tab y ki value. Line y-axis pe jahan milti hai wo point hai intercept. Real-world mein meaningful ho bhi sakta hai, nahi bhi.

R� (GOODNESS OF FIT)

Model kitna data explain kar raha hai. 1.0 = perfect fit, 0.0 = kuch nahi samjha. � lekin overfitting se bachna hai.

HOW: Linear regression kaam kaise karta hai

Linear regression ek simple equation dhundhta hai: y = mx + b. Algorithm sabse best line fit karta hai � wo line jo sabse kam error (distance) rakhe actual data points se. Ye Ordinary Least Squares (OLS) method se hota hai.

python
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import numpy as np

# House price prediction
X = np.array([[1000],[1500],[2000],[2500],[3000]]) # sq ft
y = np.array([50000,75000,100000,125000,150000]) # price

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LinearRegression()
model.fit(X_train, y_train)

print(f"Coefficient: {model.coef_[0]:.2f}")
print(f"Intercept: {model.intercept_:.2f}")
print(f"R�: {model.score(X_test, y_test):.2f}")
print(f"Predict 2200sqft: ${model.predict([[2200]])[0]:.0f}")
Mental model: Socho tumhe ghar dekhna hai aur agent ne kaha "har 500 sq ft ke liye ?25,000 badhega, aur base price ?25,000 hai." Ye wahi linear regression hai � coefficient (slope) aur intercept.

FIT LINE: y = mx + b kya hai

Ye equation line ki kahani hai. m (coefficient) batata hai ki X ka 1 unit badhne se y kitna badhega. b (intercept) batata hai ki jab X = 0 ho, tab y ki value kya hai. Dono milke line define karte hain jo data ko best fit kare.

python
import numpy as np
from sklearn.linear_model import LinearRegression

# Salary prediction: experience (years) se salary predict karo
X = np.array([[1],[2],[3],[4],[5],[6],[7]])
y = np.array([30000,35000,45000,50000,60000,65000,75000])

model = LinearRegression()
model.fit(X, y)

m = model.coef_[0] # Slope
b = model.intercept_ # Intercept

print(f"Equation: y = {m:.0f}x + {b:.0f}")
print(f"Har saal ?{m:.0f} badhega salary")
print(f"Start mein ?{b:.0f} salary hogi")

# Prediction
years = 8
pred = model.predict([[years]])
print(f"\n{years} saal baad salary: ?{pred[0]:.0f}")

R�: Model kitna achha hai

R� (R-squared) batata hai ki model kitna data explain kar raha hai. 1.0 matlab perfect prediction, 0.0 matlab model ne kuch nahi samjha. Negative R� bhi ho sakta hai � matlab model mean se bhi bura hai!

python
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import numpy as np

# Multiple metrics
X = np.array([[1000],[1500],[2000],[2500],[3000]])
y = np.array([50000,75000,100000,125000,150000])

model = LinearRegression()
model.fit(X, y)

y_pred = model.predict(X)

# R� score
r2 = r2_score(y, y_pred)
# Mean Squared Error
mse = mean_squared_error(y, y_pred)

print(f"R�: {r2:.4f}")
print(f"MSE: {mse:.2f}")
print(f"RMSE: {np.sqrt(mse):.2f}")

# Perfect fit se compare
print(f"\nModel {r2*100:.1f}% data explain kar raha hai")
Rule of thumb: R� > 0.7 = achha model, > 0.9 = bahut achha, < 0.5 = weak relationship. Lekin R� high ho toh check karo ki overfitting toh nahi ho rahi. Training aur test dono pe R� dekho.

Try it: linear regression model build karo

Editor mein sample data diya hai � house prices predict karo. Coefficient aur R� check karo. Naya data daalo, model ko retrain karo aur dekho predictions kaise change hote hain.

Python playgroundFirst run download kar sakta hai
Run Python dabayein

Quick check

y = mx + b mein m aur b kya hai?

Socho: line equation mein slope kya hota hai aur y-axis pe intercept kya hota hai?

Common linear regression mistakes

Linear Regression clear?

Ab Logistic Regression par chalo � jahan continuous nahi, categories predict karte hain. Same concept hai, bas output alag hai.