-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathregressionmultilinear.py
124 lines (103 loc) · 4.72 KB
/
regressionmultilinear.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import preprocessing as pre
import numpy as np
import pandas as pd
import time
from functools import wraps
def computeAutomaticBackwardElimination(XTrain, yTrain, XTest, sl):
import statsmodels.formula.api as sm
XTrain = np.insert(XTrain, 0, 1, axis=1)
XTest = np.insert(XTest, 0, 1, axis=1)
numVars = len(XTrain[0])
for i in range(0, numVars):
regressor_OLS = sm.OLS(yTrain, XTrain.astype(float)).fit()
maxVar = max(regressor_OLS.pvalues).astype(float)
if maxVar > sl:
for j in range(0, numVars - i):
if (regressor_OLS.pvalues[j].astype(float) == maxVar):
#print("Deletar coluna", j)
XTrain = np.delete(XTrain, j, 1)
XTest = np.delete(XTest, j, 1)
#regressor_OLS.summary()
return XTrain, XTest
def computeBackwardElimination(X, y):
#precisa do pip pra statsmodels e patsy
import statsmodels.formula.api as sm
#adicionamos 1 coluna pra incluir b0 no modelo
X = np.insert(X, 0, 1, axis=1)
#ajustamos o modelo para todos os possiveis preditores (variaveis independentes)
XOtimo = X[:,[0, 1, 2, 3, 4, 5, 6]]
regressor = sm.OLS(y, XOtimo.astype(float)).fit()
#examinamos o maior p-valor e se ele ultrapassar o limiar de 0.05, removemos
#print(regressor.summary())
#print(XOtimo[0,:])
#ajustamos o modelo removendo x5, pois esta recebeu maior p-valor
XOtimo = X[:,[0, 1, 2, 3, 4, 6]]
regressor = sm.OLS(y, XOtimo.astype(float)).fit()
#examinamos o maior p-valor e se ele ultrapassar o limiar de 0.05, removemos
#print(regressor.summary())
#print(XOtimo[0,:])
#ajustamos o modelo removendo x5, pois esta recebeu maior p-valor
XOtimo = X[:,[0, 1, 2, 3, 4]]
regressor = sm.OLS(y, XOtimo.astype(float)).fit()
#examinamos o maior p-valor e se ele ultrapassar o limiar de 0.05, removemos
#print(regressor.summary())
#print(XOtimo[0,:])
#ajustamos o modelo removendo x4, pois esta recebeu maior p-valor
XOtimo = X[:,[0, 1, 2, 3]]
regressor = sm.OLS(y, XOtimo.astype(float)).fit()
#examinamos o maior p-valor e se ele ultrapassar o limiar de 0.05, removemos
#print(regressor.summary())
#print(XOtimo[0,:])
#ajustamos o modelo removendo x3, pois esta recebeu maior p-valor
XOtimo = X[:,[0, 1, 2]]
regressor = sm.OLS(y, XOtimo.astype(float)).fit()
#examinamos o maior p-valor e se ele ultrapassar o limiar de 0.05, removemos
#print(regressor.summary())
#print(XOtimo[0,:])
#ajustamos o modelo removendo x3, pois esta recebeu maior p-valor
XOtimo = X[:,[1, 2]]
regressor = sm.OLS(y, XOtimo.astype(float)).fit()
#examinamos o maior p-valor e se ele ultrapassar o limiar de 0.05, removemos
print(regressor.summary())
print(XOtimo[0,:])
#https://medium.com/@manjabogicevic/multiple-linear-regression-using-python-b99754591ac0
def computeMultipleLinearRegressionModel(XTrain, yTrain, XTest, yTest):
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(XTrain, yTrain)
yPred = regressor.predict(XTest)
'''for i in range(0, yPred.shape[0]):
print(yPred[i], yTest[i], abs(yPred[i] - yTest[i]))
time.sleep(0.5)'''
def runMultipleLinearRegressionExample(filename):
start_time = time.time()
X, y = pre.loadDataset(filename)
elapsed_time = time.time() - start_time
print("Load Dataset: %.2f" % elapsed_time, "segundos.")
start_time = time.time()
X = pre.fillMissingData(X, 0, 2)
elapsed_time = time.time() - start_time
print("Fill Missing Data: %.2f" % elapsed_time, "segundos.")
start_time = time.time()
X = pre.computeCategorization(X, 3)
elapsed_time = time.time() - start_time
print("Compute Categorization: %.2f" % elapsed_time, "segundos.")
start_time = time.time()
XTrain, XTest, yTrain, yTest = pre.splitTrainTestSets(X, y, 0.8)
elapsed_time = time.time() - start_time
print("Split Train Test sets: %.2f" % elapsed_time, "segundos.")
start_time = time.time()
XTrain, XTest = computeAutomaticBackwardElimination(XTrain, yTrain, XTest, 0.05)
elapsed_time = time.time() - start_time
print("Compute Automatic Backward Elimination: %.2f" % elapsed_time, "segundos.")
start_time = time.time()
computeMultipleLinearRegressionModel(XTrain, yTrain, XTest, yTest)
elapsed_time = time.time() - start_time
print("Compute Multiple Linear Regression: %.2f" % elapsed_time, "segundos.")
'''start_time = time.time()
computeBackwardElimination(XTrain, yTrain)
elapsed_time = time.time() - start_time
print("Compute Backward Elimination: %.2f" % elapsed_time, "segundos.")
'''
if __name__ == "__main__":
runMultipleLinearRegressionExample("insurance.csv")