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.
Multiple linear regression in 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.
y = 5 + 2*x1 + 3*x2
y = b0 + b1*age + b2*income + b3*education
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.
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")
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.
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.
Practice
Solution
Step 1: Understand the purpose of multiple linear regression
Multiple linear regression uses multiple inputs to predict a single output by finding the best weights for each input.Step 2: Compare options with this understanding
Only It predicts a result using several input features combined. correctly describes predicting a result using several inputs combined.Final Answer:
It predicts a result using several input features combined. -> Option AQuick Check:
Multiple inputs predict output = A [OK]
- Confusing regression with clustering
- Thinking it reduces features instead of weighting them
- Mixing it up with pathfinding algorithms
Solution
Step 1: Recall the multiple linear regression formula
The prediction is a sum of a bias term plus each input multiplied by its weight: y = w0 + w1*x1 + w2*x2 + ... + wn*xn.Step 2: Check each option against the formula
Onlyy = w0 + w1*x1 + w2*x2 + ... + wn*xnmatches the correct formula with addition and multiplication.Final Answer:
y = w0 + w1*x1 + w2*x2 + ... + wn*xn -> Option AQuick Check:
Sum of weighted inputs plus bias = B [OK]
- Using multiplication instead of addition between terms
- Dividing weights by inputs
- Subtracting weights instead of adding
Solution
Step 1: Apply the formula y = w0 + w1*x1 + w2*x2
Substitute values: y = 2 + 3*4 + (-1)*5.Step 2: Calculate the prediction
Calculate: 3*4 = 12, (-1)*5 = -5, sum = 2 + 12 - 5 = 9.Step 3: Recheck calculation
Sum is 9, but 10 says 10, so re-check: 2 + 12 - 5 = 9, so correct answer is 9.Final Answer:
9 -> Option BQuick Check:
2 + 12 - 5 = 9 [OK]
- Adding weights instead of multiplying by inputs
- Forgetting to add bias term w0
- Mixing up signs of weights
def predict(x1, x2, w0, w1, w2):
return w0 + w1 * x1 + w2 + x2
print(predict(3, 4, 1, 2, 3))Solution
Step 1: Analyze the formula inside the function
The formula should multiply each input by its weight: w0 + w1*x1 + w2*x2.Step 2: Identify the mistake in the code
The code uses w2 + x2 instead of w2 * x2, which adds instead of multiplies.Final Answer:
The term w2 + x2 should be w2 * x2 for correct multiplication. -> Option DQuick Check:
Multiplication needed, not addition = A [OK]
- Using + instead of * between weight and input
- Forgetting return statement (not here though)
- Mixing parameter order
- Incorrect print syntax
Solution
Step 1: Understand the meaning of weights in multiple linear regression
Each weight shows how much the output changes when that input increases by one unit, keeping others fixed.Step 2: Interpret w1=4 for hours studied
A weight of 4 means exam score increases by 4 points for each additional hour studied, assuming hours slept stays the same.Final Answer:
For each extra hour studied, exam score increases by 4 points, holding sleep constant. -> Option CQuick Check:
Weight shows effect per unit input = D [OK]
- Thinking weight means no effect
- Confusing positive and negative effect
- Mixing effects of different features
Ready to prove it?
Pass the test and this pattern moves unknown
