-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainedGradientDescentPrediction.py
68 lines (63 loc) · 1.82 KB
/
trainedGradientDescentPrediction.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
import sys
from csv import reader
from utils import normalizeElemt, denormalizeElem
# Get user input
def getUserInput():
while 1:
print("Please enter a value for the prediction")
try:
predictionValue = input()
if (int(predictionValue) >= 0):
break
else:
print('Not a valid value for the prediction model. Needs to be >= 0')
except ValueError:
print('Not a valid value for the prediction model. Needs to be >= 0')
except EOFError:
sys.exit('Error on Input. Exit..')
except:
sys.exit('Error on Input. Exit...')
return (float(predictionValue))
# Here we are loading the latest value for the coeffiecients beta0 and beta1
# which have been trained with trainModel.py
def getTrainedCoeffiecients():
coeff = list()
minMaxX = list()
minMaxY = list()
try:
file = open('./coefficients/b0b1.csv', "r")
with file:
lines = reader(file)
for idx, row in enumerate(lines):
if not row:
continue
if (idx == 0):
coeff.append(row)
elif (idx == 1):
minMaxX.append(row)
elif (idx == 2):
minMaxY.append(row)
except OSError:
sys.exit('Could not load latest beta0, beta1 values. Exit...')
return (coeff,minMaxX,minMaxY)
# Simple linear regression equation
def predict(coeff,minMaxX,minMaxY, inputValue):
b0 = coeff[0][0]
b1 = coeff[0][1]
minX = minMaxX[0][0]
minY = minMaxY[0][0]
maxX = minMaxX[0][1]
maxY = minMaxY[0][1]
predictedPrice = float(b0) + float(b1)*normalizeElemt(minX, maxX, inputValue)
return denormalizeElem(minY, maxY, predictedPrice)
def main():
inputValue = getUserInput()
coeff = list
minMaxX = list
minMaxY = list
coeff,minMaxX,minMaxY = getTrainedCoeffiecients()
if (coeff and minMaxX and minMaxY):
print('Estimated price: ', int(predict(coeff, minMaxX, minMaxY, inputValue)))
sys.exit('Estimated price: {0}'.format(0))
if __name__ == "__main__":
main()