-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_regression.py
62 lines (43 loc) · 1.41 KB
/
multiple_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
#from sklearn.metrics import r2_score
path = 'FuelConsumptionCo2.csv'
df = pd.read_csv(path)
print('\nReading the Data :')
print(df.head())
print('\nData Exploration :')
print(df.describe())
print('\nFeatures Selection :')
cdf = df[['ENGINESIZE', 'CYLINDERS', 'FUELCONSUMPTION_COMB', 'CO2EMISSIONS']]
print(cdf.head())
#Data Plotting
plt.scatter(cdf['ENGINESIZE'], cdf['CO2EMISSIONS'])
plt.xlabel('Engine Size')
plt.ylabel('CO2 Emissions')
plt.show()
plt.scatter(cdf['CYLINDERS'], cdf['CO2EMISSIONS'])
plt.xlabel('Cylinders')
plt.ylabel('CO2 Emissions')
plt.show()
plt.scatter(cdf['FUELCONSUMPTION_COMB'], cdf['CO2EMISSIONS'])
plt.xlabel('Fuel Consumption')
plt.ylabel('CO2 Emissions')
plt.show()
#Creating Train and Test Dataset
msk = np.random.rand(len(cdf)) < 0.8
train = cdf[msk]
test = cdf[~msk]
train_x = train[['ENGINESIZE', 'CYLINDERS', 'FUELCONSUMPTION_COMB']]
train_y = train[['CO2EMISSIONS']]
test_x = test[['ENGINESIZE', 'CYLINDERS', 'FUELCONSUMPTION_COMB']]
test_y = test[['CO2EMISSIONS']]
#Modelling
reg = linear_model.LinearRegression()
reg.fit(train_x, train_y)
print('\nCo-efficients:', reg.coef_)
#Model Evaluation
predictions = reg.predict(test_x)
print('\nVariance Score :', reg.score(test_x, test_y))
#print(r2_score(test_y, predictions))