MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / accuracy

Function accuracy

machine_learning/linear_discriminant_analysis.py:220–248  ·  view source on GitHub ↗

Calculate the value of accuracy based-on predictions :param actual_y:a list containing initial Y values generated by 'y_generator' function :param predicted_y: a list containing predicted Y values generated by 'predict_y_values' function :return: percentage of accura

(actual_y: list, predicted_y: list)

Source from the content-addressed store, hash-verified

218
219# Calculating Accuracy
220def accuracy(actual_y: list, predicted_y: list) -> float:
221 """
222 Calculate the value of accuracy based-on predictions
223 :param actual_y:a list containing initial Y values generated by 'y_generator'
224 function
225 :param predicted_y: a list containing predicted Y values generated by
226 'predict_y_values' function
227 :return: percentage of accuracy
228
229 >>> actual_y = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
230 ... 1, 1 ,1 ,1 ,1 ,1 ,1]
231 >>> predicted_y = [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0,
232 ... 0, 0, 1, 1, 1, 0, 1, 1, 1]
233 >>> accuracy(actual_y, predicted_y)
234 50.0
235
236 >>> actual_y = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
237 ... 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
238 >>> predicted_y = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
239 ... 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
240 >>> accuracy(actual_y, predicted_y)
241 100.0
242 """
243 # iterate over one element of each list at a time (zip mode)
244 # prediction is correct if actual Y value equals to predicted Y value
245 correct = sum(1 for i, j in zip(actual_y, predicted_y) if i == j)
246 # percentage of accuracy equals to number of correct predictions divided by number
247 # of all data and multiplied by 100
248 return (correct / len(actual_y)) * 100
249
250
251def valid_input[num](

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected