-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegression.py
77 lines (61 loc) · 2.9 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
# ensemble models for better performance in classification
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import RandomForestRegressor
#metrics to check our model performance
from sklearn.metrics import mean_squared_error, mean_absolute_error
from MiscellaneousFunctions import root_mean_squared_error
class Regression():
'''
This class implements multiple regression algorithms to already encoded and scaled data.
It required the data to be numeric and divided into train and test sets.
'''
def __init__(self):
self.linear_regressor = LinearRegression()
self.support_vector_regressor = SVR()
self.decision_tree_regressor = DecisionTreeRegressor()
self.random_forest_regressor = RandomForestRegressor()
self.adaboost_regressor = AdaBoostRegressor()
self.all_models = [self.linear_regressor, self.support_vector_regressor, self.decision_tree_regressor,
self.random_forest_regressor, self.adaboost_regressor]
self.all_model_names = ['Linear Regression', 'Support Vector Regressor', 'Decision Tree Regressor',
'Random Forest Regressor', 'Adaboost Regressor']
self.train_scores = []
self.test_scores = []
self.metric_list = [mean_absolute_error, mean_squared_error, root_mean_squared_error]
self.metrics = []
data = {'Model Names': self.all_model_names}
self.all_model_info = pd.DataFrame(data)
def fit(self, x_train, x_test, y_train, y_test):
'''
fits models to data and stores results for metrics
'''
self.x_train = x_train
self.x_test = x_test
self.y_train = y_train
self.y_test = y_test
for model in self.all_models:
model.fit(x_train, y_train)
train_score = model.score(x_train,y_train)
self.train_scores.append(train_score)
test_score = model.score(x_train,y_train)
self.test_scores.append(train_score)
y_predict = model.predict(self.x_test)
self.all_model_info['Train Score'] = self.train_scores
self.all_model_info['Test Score'] = self.test_scores
self.apply_metrics()
def apply_metrics(self):
self.metrics = []
for metric in self.metric_list:
metric_name = str(metric).split(' ')[1]
for model in self.all_models:
metric_item = metric(self.y_test, model.predict(self.x_test))
self.metrics.append(metric_item)
self.all_model_info[metric_name] = self.metrics
self.metrics = []
def display_report(self):
return self.all_model_info