Linear Regression sabse simple aur sabse zyada use hone wala ML model hai. Ye data me ek seedhi line fit karta hai aur usse numbers predict karta hai — jaise experience se salary, ya ghar ke size se price.
Formula: y = m·x + c — jaha m slope (x ek badhne pe y kitna badhega) aur c intercept (x = 0 pe y) hai.
Simple linear regression
Example
import numpy as np
from sklearn.linear_model import LinearRegression
experience = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(-1, 1)
salary = np.array([30, 35, 41, 44, 50, 56, 59, 65, 70, 76]) # hazaar Rs
model = LinearRegression()
model.fit(experience, salary)
print("Slope (m):", round(model.coef_[0], 3))
print("Intercept (c):", round(model.intercept_, 3))
print("12 saal experience pe salary:", round(model.predict([[12]])[0], 1), "hazaar")
Output
Slope (m): 5.03
Intercept (c): 24.933
12 saal experience pe salary: 85.3 hazaar
Matlab har ek saal ke experience pe salary lagbhag 5.03 hazaar badhti hai. reshape(-1, 1) isliye kiya kyunki scikit-learn features ko hamesha 2D (rows × columns) me chahta hai.
Example
import matplotlib.pyplot as plt
plt.scatter(experience, salary, color="#2563eb", label="Asli data")
plt.plot(experience, model.predict(experience), color="#f97316", label="Best fit line")
plt.xlabel("Experience (years)")
plt.ylabel("Salary (Rs '000)")
plt.legend()
plt.show()
Model aisi line chunta hai jisse har point ki line se doori (error) ka square karke jodne pe total sabse kam ho. Isko Ordinary Least Squares kehte hain.
Multiple linear regression
Real life me prediction kai features pe depend karta hai. scikit-learn ka diabetes dataset lete hain — 10 health measurements se ek saal baad bimari ki progress predict karni hai:
Example
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
data = load_diabetes(as_frame=True)
X, y = data.data, data.target
print(X.shape)
print(X.columns.tolist())
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
reg = LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
Output
(442, 10)
['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
Evaluation: MAE, RMSE, R²
Example
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
print("MAE :", round(mean_absolute_error(y_test, y_pred), 2))
print("RMSE:", round(np.sqrt(mean_squared_error(y_test, y_pred)), 2))
print("R² :", round(r2_score(y_test, y_pred), 3))
print("Target ka range:", y.min(), "se", y.max())
Output
MAE : 42.79
RMSE: 53.85
R² : 0.453
Target ka range: 25.0 se 346.0
Metric | Matlab | Accha kab |
|---|---|---|
MAE | Average kitna galat (same unit me) | Jitna kam utna accha |
RMSE | Bade errors ko zyada saza deta hai | Jitna kam utna accha |
R² | Target ke variation ka kitna hissa model samjha | 1 ke jitna paas utna accha |
R² = 0.45 ka matlab model bimari ki progress ke variation ka lagbhag 45% samjha paaya. Medical data me itna theek-thaak hai — baaki variation un cheezon se hai jo dataset me hain hi nahi.
Kaunsa feature kitna important?
Example
import pandas as pd
coefs = pd.Series(reg.coef_, index=X.columns).sort_values(key=abs, ascending=False)
print(coefs.round(1).head(5))
Output
s1 -931.5
s5 736.2
bmi 542.4
s2 518.1
bp 347.7
dtype: float64
Is dataset ke features pehle se scaled hain, isliye coefficients ko seedha compare kar sakte hain. Bina scaling ke data me pehle StandardScaler lagaiye.
Linear Regression ke assumptions
- Features aur target ka rishta lagbhag linear ho
- Errors ek-doosre se independent hon
- Errors ka spread har jagah lagbhag same ho (homoscedasticity)
- Features aapas me bahut zyada correlated na hon (multicollinearity)
Poori gehraai ke saath: Linear Regression — A Complete Deep Dive.