To write a python program to implement multivariate linear regression and predict the output.
- Hardware – PCs
- Anaconda – Python 3.7 Installation / Moodle-Code Runner
Import Pandas module
Import linear_module from sklearn
Declare data frame df to read the csv file
Declare variable X equal to df with two arguments Weight and Volume
Declare variable y equal to df with an argument CO2
Declare a variable regr equal to linear_model.LinearRegression()
declare regr.fit(X,y)
Print regr.coeff_
Print regr.intercept_
Declare variable predictedCO2 equal to regr.predict with two arguments 33300, 1300
Print variable predictedCO2
"Cars.Csv"
V\import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
df=pd.read_csv("cars.csv")
X=df[['Weight','Volume']]
y=df['CO2']
regr = linear_model.LinearRegression()
regr.fit(X,y)
#Coefficients and intercepts of Model
print('Coefficients: ', regr.coef_)
print('Intercept:',regr.intercept_)
#predict the c02 emission of a car where the weight is 300kg,and the volume is 1300cm3
predictedCO2= regr.predict([[330,130]])
print('Predicted CO2 for the corresponding weight and volume',predictedCO2)
Thus the multivariate linear regression is implemented and predicted the output using python program.