Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Regression

Lesson 26 of 46 in the free Introduction to Data Analytics notes on Siksha Sarovar, written by Rohit Jangra.

Regression

Regression is the supervised learning task of predicting a continuous numeric value from one or more input variables. Where classification answers "which class?", regression answers "how much?".

Terminology

TermMeaning
Dependent variable (Y)What you predict — also target, response, outcome
Independent variable (X)What you predict from — also predictor, feature, explanatory variable
Coefficient (β)How much Y changes per one-unit change in X
Intercept (β₀)Predicted Y when all X = 0
Residual (e)Actual Y − Predicted Ŷ, for one observation
Fitted value (Ŷ)The model's prediction

Classification vs Regression

BasisClassificationRegression
Output typeDiscrete class labelContinuous number
QuestionWhich category?How much / how many?
ExampleWill the student pass?What marks will the student score?
AlgorithmsNaïve Bayes, KNN, decision tree, SVMLinear, polynomial, ridge, lasso, SVR
EvaluationAccuracy, precision, recall, F1MAE, MSE, RMSE, R²
Decision boundarySeparates classesBest-fit line/surface through the points
Logistic regression is a classification algorithm despite its name — it predicts a probability of class membership, then thresholds it. A frequent exam trap.

Regression Evaluation Metrics

                     1
   MAE  (Mean Absolute Error)      = ─── Σ |yᵢ − ŷᵢ|
                                      n

                                      1
   MSE  (Mean Squared Error)       = ─── Σ (yᵢ − ŷᵢ)²
                                      n

   RMSE (Root Mean Squared Error)  = √MSE

                                          SS_res      Σ(yᵢ − ŷᵢ)²
   R² (Coefficient of Determination) = 1 − ────── = 1 − ──────────────
                                          SS_tot      Σ(yᵢ − ȳ)²

                                       (1 − R²)(n − 1)
   Adjusted R² = 1 − ───────────────────────────────────
                            n − k − 1        (k = number of predictors)

                                     1        | yᵢ − ŷᵢ |
   MAPE (Mean Absolute % Error) = ─── Σ  ──────────  × 100
                                     n            yᵢ
MetricUnitOutlier sensitivityInterpretation
MAESame as YLow"On average we're off by X units"
MSEY squaredHigh (squares the errors)Used for optimisation, hard to interpret
RMSESame as YHighMost reported; penalises large errors
Unitless (0–1)Moderate"% of variance in Y explained by the model"
Adjusted R²UnitlessModerateR² penalised for adding useless predictors
MAPE%HighComparable across different scales; breaks when Y ≈ 0
Why Adjusted R² exists: plain R² never decreases when you add a predictor, even a random one. Adjusted R² falls if the new predictor doesn't earn its place — so it is the honest metric for comparing models with different numbers of features.

Worked Example — Computing the Metrics

Actual: 10, 20, 30, 40 | Predicted: 12, 18, 33, 39

Errors (y − ŷ):  −2, +2, −3, +1

MAE  = (2 + 2 + 3 + 1) / 4 = 8/4 = 2.0
MSE  = (4 + 4 + 9 + 1) / 4 = 18/4 = 4.5
RMSE = √4.5 = 2.121

ȳ = (10+20+30+40)/4 = 25
SS_tot = (10−25)² + (20−25)² + (30−25)² + (40−25)²
       = 225 + 25 + 25 + 225 = 500
SS_res = 4 + 4 + 9 + 1 = 18

R² = 1 − 18/500 = 1 − 0.036 = 0.964   ->  96.4% of variance explained
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

y_true = np.array([10, 20, 30, 40])
y_pred = np.array([12, 18, 33, 39])

print(f"MAE  = {mean_absolute_error(y_true, y_pred):.4f}")            # 2.0000
print(f"MSE  = {mean_squared_error(y_true, y_pred):.4f}")             # 4.5000
print(f"RMSE = {np.sqrt(mean_squared_error(y_true, y_pred)):.4f}")    # 2.1213
print(f"R²   = {r2_score(y_true, y_pred):.4f}")                       # 0.9640

mape = np.mean(np.abs((y_true - y_pred) / y_true)) * 100
print(f"MAPE = {mape:.2f}%")                                          # 9.79%

Assumptions of Linear Regression — "LINE"

AssumptionMeaningHow to checkFix if violated
L — LinearityRelationship between X and Y is linearScatter plot, residual plotTransform X or Y; polynomial terms
I — IndependenceResiduals are independent of each otherDurbin-Watson test; plot residuals in orderTime-series models
N — NormalityResiduals are normally distributedQ-Q plot, Shapiro-Wilk on residualsTransform Y (log/Box-Cox)
E — Equal variance (homoscedasticity)Residual spread is constant across fitted valuesResiduals vs fitted plot (look for a funnel)Log transform, weighted least squares
(plus) No multicollinearityPredictors are not strongly correlated with each otherVIF > 10 signals a problemDrop or combine correlated predictors
# Checking assumptions with residual diagnostics
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
from sklearn.linear_model import LinearRegression

np.random.seed(42)
X = np.random.uniform(1, 50, 120).reshape(-1, 1)
y = 3.2 * X.ravel() + 15 + np.random.normal(0, 8, 120)

model = LinearRegression().fit(X, y)
fitted = model.predict(X)
resid = y - fitted

fig, axes = plt.subplots(1, 3, figsize=(16, 4))

axes[0].scatter(fitted, resid, alpha=0.6, color="#168B99")
axes[0].axhline(0, color="#ef4444", linestyle="--")
axes[0].set_title("Residuals vs Fitted\n(want: random cloud)")
axes[0].set_xlabel("Fitted"); axes[0].set_ylabel("Residual")

stats.probplot(resid, dist="norm", plot=axes[1])
axes[1].set_title("Q-Q Plot\n(want: points on the line)")

axes[2].hist(resid, bins=20, color="#10b981", edgecolor="white")
axes[2].set_title("Residual Distribution\n(want: bell shaped, centred at 0)")

plt.tight_layout(); plt.show()

stat, p = stats.shapiro(resid)
print(f"Shapiro-Wilk on residuals: p = {p:.4f}",
      "-> normality OK" if p > 0.05 else "-> normality violated")
# Multicollinearity check with VIF
from statsmodels.stats.outliers_influence import variance_inflation_factor

df = pd.DataFrame({
    "area_sqft":  [1000, 1500, 1200, 1800, 2000, 1350],
    "area_sqm":   [92.9, 139.4, 111.5, 167.2, 185.8, 125.4],   # perfectly redundant!
    "bedrooms":   [2, 3, 2, 4, 4, 3],
})

vif = pd.DataFrame({
    "feature": df.columns,
    "VIF": [variance_inflation_factor(df.values, i) for i in range(df.shape[1])],
})
print(vif)
# area_sqft and area_sqm have astronomically high VIF — they are the same variable
# in different units. Drop one.

Choosing a Regression Model

The next lessons apply these ideas concretely: linear regression in full detail, plus the two classifiers named in the syllabus.