Skip to content

Commit ee3ee30

Browse files
committed
Adding accuracy function
1 parent fb390e3 commit ee3ee30

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

machine_learning/linear discriminant analysis.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,23 @@ def predict(x_items: list, means: list, variance: float, probabilities: list) ->
162162
# discriminant value is taken as the prediction, than we try to get index of that.
163163
predicted_index.append(l.index(max(l)))
164164
return predicted_index
165+
166+
167+
# Calculating Accuracy
168+
def accuracy(actual_y: list, predicted_y: list) -> float:
169+
""" This function calculates the value of accuracy based-on predictions
170+
:param actual_y:a list containing initial Y values generated by 'Y_gen' function
171+
:param predicted_y: a list containing predicted Y values generated by 'predict' function
172+
:return: percentage of accuracy
173+
"""
174+
correct = 0 # initial value for number of correct predictions
175+
# for loop iterates over one element of each list at a time (zip mode)
176+
for i, j in zip(actual_y, predicted_y):
177+
# if actual Y value equals to predicted Y value
178+
if i == j:
179+
# prediction is correct
180+
correct += 1
181+
# percentage of accuracy equals to number of correct predictions divided by number of
182+
# all data and multiplied by 100
183+
percentage = (correct / len(actual_y)) * 100
184+
return percentage

0 commit comments

Comments
 (0)