SwadhyAI Notes

All Python notes

Multiple Regression 📊

More Variables

Multiple regression is like linear regression, but with more than one independent value. Instead of predicting CO2 based only on engine size, we can also use the car's weight.

✨ Using Pandas & Sklearn

python
import pandas
from sklearn import linear_model

df = pandas.read_csv("cars.csv")

# Independent variables (Features)
X = df[['Weight', 'Volume']]
# Dependent variable (Target)
y = df['CO2']

regr = linear_model.LinearRegression()
regr.fit(X, y)

# Predict CO2 for a car with Weight 2300kg and Volume 1300cm3
predictedCO2 = regr.predict([[2300, 1300]])
print(predictedCO2)

🔢 Coefficients

The coefficient is a factor that describes the relationship with an unknown variable.

python
print(regr.coef_) 
# Output: [0.00755095 0.00780526]

This means that if the weight increases by 1kg, the CO2 emission increases by 0.0075g (assuming volume stays the same).

✨ Pro Tip

Always check if your independent variables are correlated with each other (Multicollinearity), as this can mess up the model.