Multiple linear regression helps us understand how several things together affect one result. It finds a simple formula to predict the result from many inputs.
0
0
Multiple linear regression in ML Python
Introduction
You want to predict house prices based on size, number of rooms, and location.
You want to estimate a student's test score using hours studied, sleep, and attendance.
You want to find how temperature, humidity, and wind speed affect electricity usage.
You want to understand how advertising on TV, radio, and newspapers impacts sales.
Syntax
ML Python
y = b0 + b1*x1 + b2*x2 + ... + bn*xn + error
y is the result we want to predict.
b0 is the starting point (intercept), and b1, b2, ..., bn are the weights for each input x1, x2, ..., xn.
Examples
This means y starts at 5, then adds 2 times x1 and 3 times x2.
ML Python
y = 5 + 2*x1 + 3*x2
Predict y using age, income, and education with their own weights.
ML Python
y = b0 + b1*age + b2*income + b3*education
Sample Program
This code trains a multiple linear regression model to predict house prices from size and number of rooms. It then predicts the price of a new house.
ML Python
from sklearn.linear_model import LinearRegression import numpy as np # Sample data: features are size (sq ft), number of rooms X = np.array([[1500, 3], [1800, 4], [2400, 3], [3000, 5], [3500, 4]]) # Target: house price in $1000s y = np.array([400, 500, 600, 650, 700]) model = LinearRegression() model.fit(X, y) print(f"Intercept (b0): {model.intercept_:.2f}") print(f"Coefficients (b1, b2): {model.coef_}") # Predict price for a house with 2000 sq ft and 3 rooms new_house = np.array([[2000, 3]]) predicted_price = model.predict(new_house) print(f"Predicted price for 2000 sq ft, 3 rooms: {predicted_price[0]:.2f} thousand dollars")
OutputSuccess
Important Notes
Make sure your input features are numbers and scaled similarly for better results.
Multiple linear regression assumes a straight-line relationship between inputs and output.
Check for outliers as they can affect the model a lot.
Summary
Multiple linear regression predicts a result using many inputs combined.
It finds weights for each input to best match the known results.
It is useful for simple predictions and understanding relationships.