-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolynomialRegression.py
49 lines (40 loc) · 1.72 KB
/
PolynomialRegression.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
from sklearn import datasets
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.preprocessing import PolynomialFeatures
import pandas as pd
import numpy as np
class PolynomialRegression:
lm = linear_model.LinearRegression()
def LoadDataset(self):
print('Loading dataset')
self.data = datasets.load_boston()
self.dataframe = pd.DataFrame(self.data.data,columns=self.data.feature_names)
self.target = pd.DataFrame(self.data.target,columns=['MEDV'])
self.x = self.dataframe
self.y = self.target["MEDV"]
def SplitDataset(self):
print('Splitting dataset')
self.x_train,self.x_test,self.y_train,self.y_test = train_test_split(self.x,self.y,test_size=0.3,random_state=5)
polynomialFeatures = PolynomialFeatures(degree=2)
self.x_train_poly = polynomialFeatures.fit_transform(self.x_train)
self.x_test_poly = polynomialFeatures.fit_transform(self.x_test)
def TrainModel(self):
print('Training model')
self.lm.fit(self.x_train_poly,self.y_train)
def Predict(self):
print('Predicting output')
self.y_predicted = self.lm.predict(self.x_test_poly)
def TestModel(self):
print('R2 Score: ',metrics.r2_score(self.y_test,self.y_predicted))
print('Explained Variance Score:', metrics.explained_variance_score(self.y_test, self.y_predicted))
print('Mean Squared Error:', metrics.mean_squared_error(self.y_test, self.y_predicted))
def main():
pr = PolynomialRegression()
pr.LoadDataset()
pr.SplitDataset()
pr.TrainModel()
pr.Predict()
pr.TestModel()
main()