-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccuracy.py
47 lines (42 loc) · 2.16 KB
/
accuracy.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
from math import sqrt
from leastSquaresPrediction import getData, getColumnValues
from trainedGradientDescentPrediction import getTrainedCoeffiecients, predict
from utils import normalizeElemt, denormalizeElem, printList
from trainModel import parseCsvFile
def addPredictions(coeff,minMaxX,minMaxY,km):
predictedPrice = list()
for row in km:
predictedPrice.append(float(predict(coeff,minMaxX,minMaxY, int(row))))
return predictedPrice
# RMSE is calculated as the square root of the mean of the squared differences between actual outcomes and predictions.
# Squaring each error forces the values to be positive,
# and the square root of the mean squared error returns the error metric back to the original units for comparison.
def calculateRMSEAccuracy(actualPrices, predictedPrices):
sum_error = 0.0
for i in range(len(actualPrices)):
prediction_error = predictedPrices[i] - actualPrices[i]
sum_error += (prediction_error ** 2)
mean_error = sum_error / float(len(predictedPrices))
return sqrt(mean_error)
# A quick way to evaluate a set of predictions on a classification problem is by using accuracy.
# Classification accuracy is a ratio of the number of correct predictions out of all predictions that were made.
# It is often presented as a percentage between 0% for the worst possible accuracy and 100% for the best possible accuracy.
# This model is only suitable if you deal with binary outcomes (0, 1), or True/False
def calculateClassificationAccuracy(actualPrices, predictedPrices):
correct = 0
for i in range(len(actualPrices)):
if (actualPrices[i] == predictedPrices[i]):
correct += 1
classificationAccuracy = correct / float(len(actualPrices)) * 100
return classificationAccuracy
def main():
data = getData()
km, actualPrices = getColumnValues(data)
coeff,minMaxX,minMaxY = getTrainedCoeffiecients()
predictedPrices = addPredictions(coeff,minMaxX,minMaxY,km)
classificationAccuracy = calculateClassificationAccuracy(actualPrices, predictedPrices)
rootMeanSquareError = calculateRMSEAccuracy(actualPrices, predictedPrices)
print('classificationAccuracy', classificationAccuracy)
print('RMSE: %.3f' % (rootMeanSquareError))
if __name__ == "__main__":
main()