Table of Contents

    Multiple Linear Regression

    MACHINE LEARNING

    Multiple Linear Regression

    Predicting a continuous value using more than one input feature.

    What is Multiple Linear Regression?

    Multiple Linear Regression (MLR) is a type of regression where the target variable (Y) is predicted using two or more independent variables (X₁, X₂, X₃, ...).

    In simple words — Multiple Linear Regression uses multiple input features to predict one continuous output.

    Simple vs Multiple Linear Regression

    Aspect Simple Linear Regression Multiple Linear Regression
    Number of Inputs One feature (X) Two or more features (X₁, X₂, ..., Xₙ)
    Equation Y = b₀ + b₁X Y = b₀ + b₁X₁ + b₂X₂ + ... + bₙXₙ
    Graph Straight line (2D) Hyperplane (n-dimensional)
    Use Case Predict salary from experience Predict house price from area, location, rooms

    Mathematical Formula

    MULTIPLE LINEAR REGRESSION EQUATION
    $$ Y = b_0 + b_1X_1 + b_2X_2 + b_3X_3 + \dots + b_nX_n + \epsilon $$

    Where:

    • Y = predicted output (dependent variable)
    • b₀ = intercept (constant)
    • b₁, b₂, ..., bₙ = coefficients (weights for each feature)
    • X₁, X₂, ..., Xₙ = input features
    • ε = error term

    Real-Life Example

    Predicting a House Price using:

    • Area (sq. ft)
    • Number of bedrooms
    • Location score
    • Age of the house
    EXAMPLE EQUATION
    $$ Price = b_0 + b_1(Area) + b_2(Bedrooms) + b_3(Location) + b_4(Age) $$

    Key Assumptions of Multiple Linear Regression

    1

    Linearity

    The relationship between input features and output is linear.

    2

    Independence

    Observations are independent of each other.

    3

    Homoscedasticity

    The variance of errors should be constant across all values of X.

    4

    Normality of Errors

    Errors (residuals) should be normally distributed.

    5

    No Multicollinearity

    Independent variables should not be highly correlated with each other.

    How Does MLR Work?

    Step-by-Step Process

    • Collect and clean dataset.
    • Choose multiple independent variables (X).
    • Define the dependent variable (Y).
    • Split data into train/test sets.
    • Fit the regression model.
    • Predict outputs on test data.
    • Evaluate model using MSE, RMSE, R².

    Python Example

    Prerequisites: Python 3.x, pandas, numpy, scikit-learn.
    pip install pandas numpy scikit-learn
    import pandas as pd
    from sklearn.linear_model import LinearRegression
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import mean_squared_error, r2_score
    
    # Sample dataset
    data = {
        "Area":     [1000, 1500, 2000, 2500, 3000, 3500, 4000],
        "Bedrooms": [2, 3, 3, 4, 4, 5, 5],
        "Age":      [10, 5, 15, 8, 3, 2, 1],
        "Price":    [40, 55, 65, 80, 100, 120, 140]   # in lakhs
    }
    df = pd.DataFrame(data)
    
    # Features and target
    X = df[["Area", "Bedrooms", "Age"]]
    y = df["Price"]
    
    # Split data
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Train model
    model = LinearRegression()
    model.fit(X_train, y_train)
    
    # Predict
    y_pred = model.predict(X_test)
    
    # Evaluate
    print("Coefficients:", model.coef_)
    print("Intercept:", model.intercept_)
    print("MSE:", mean_squared_error(y_test, y_pred))
    print("R² Score:", r2_score(y_test, y_pred))
    Output The model learns the impact of each feature (Area, Bedrooms, Age) and predicts house price accurately.

    Interpreting Coefficients

    Each coefficient (b₁, b₂, ...) tells how much the output (Y) changes when that feature increases by 1 unit, keeping others constant.

    FeatureCoefficientMeaning
    Area+0.03Price increases ₹0.03L per sq. ft
    Bedrooms+5Each extra room adds ₹5L
    Age-2Older houses cost ₹2L less per year

    Evaluation Metrics

    MetricDescription
    MAEMean Absolute Error
    MSEMean Squared Error
    RMSERoot Mean Squared Error
    Goodness of fit (0 to 1)
    Adjusted R²R² adjusted for number of predictors
    Adjusted R² is preferred over R² in Multiple Linear Regression because it penalizes adding useless features.

    Problem: Multicollinearity

    Multicollinearity occurs when two or more independent variables are highly correlated, making the model unstable.

    How to detect:

    • Correlation Matrix
    • Variance Inflation Factor (VIF)
    from statsmodels.stats.outliers_influence import variance_inflation_factor
    import pandas as pd
    
    vif = pd.DataFrame()
    vif["Feature"] = X.columns
    vif["VIF"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]
    print(vif)
    Rule VIF > 5 indicates strong multicollinearity — consider dropping one of the correlated features.

    Real-Life Analogy

    Multiple Linear Regression = Baking a Cake

    The taste of a cake (Y) depends on multiple ingredients (X): flour, sugar, butter, eggs. Each ingredient contributes differently (coefficients). Adjusting them properly gives the perfect cake — just like the perfect prediction.

    Real-World Applications

    Real Estate

    • Predict property prices
    • Estimate rent based on location and size

    E-Commerce

    • Forecast sales
    • Optimize pricing

    Healthcare

    • Predict patient recovery time
    • Estimate medical costs

    Finance

    • Predict stock prices
    • Credit risk modeling

    Automotive

    • Predict mileage
    • Estimate maintenance cost

    Weather

    • Predict rainfall
    • Estimate humidity from multiple sensors

    Advantages

    • Easy to interpret and implement.
    • Captures relationships between multiple features and the target.
    • Works well for many real-world structured datasets.
    • Provides understanding through coefficients.

    Disadvantages

    Limitation 1 Sensitive to outliers.
    Limitation 2 Assumes linear relationship — fails for complex, non-linear data.
    Limitation 3 Multicollinearity can distort results.
    Limitation 4 Overfitting risk if too many irrelevant features are added.

    Common Mistakes to Avoid

    Mistake 1 Including too many features without checking correlation.
    Mistake 2 Ignoring data scaling when needed.
    Mistake 3 Using categorical features directly without encoding.
    Mistake 4 Forgetting to check residual plots.

    Best Practices

    Quick Tips

    • Always handle missing values before training.
    • Encode categorical features properly.
    • Check correlation matrix for multicollinearity.
    • Use Adjusted R² for model selection.
    • Visualize residuals to verify assumptions.
    • Use cross-validation for stable performance.

    Importance of MLR

    Multi-Feature Modeling

    • Captures real-world complexity
    • Considers multiple inputs

    Predictive Power

    • Strong forecasting capability
    • Used in business analytics

    Foundation for Advanced ML

    • Used in Ridge/Lasso regression
    • Base of many ML models

    Interpretability

    • Coefficients reveal feature importance
    • Easy for business users to understand

    Golden Rule

    REMEMBER
    Multiple Inputs + Linear Relationship = Multiple Linear Regression

    Key Takeaway

    Multiple Linear Regression extends Simple Linear Regression by using multiple input variables to predict a single continuous output. It's one of the most widely used ML techniques for real-world structured data problems — especially when you need both accuracy and interpretability.