Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating percentage error by comparing two arrays

I have some data in two numpy arrays.

a = [1, 2, 3, 4, 5, 6, 7]
b = [1, 2, 3, 5, 5, 6, 7]

I say array a is my calculated result and array b are the true result values. I want to calculate the error percentage in my result. Now I can loop through the two arrays and compare them 0 if the values match and 1 for a mismatch then add them up, divide by the total values and calculate percentage error.

Is there any possible faster and elegant method for doing this ?

like image 856
Ada Xu Avatar asked Dec 05 '13 14:12

Ada Xu


1 Answers

First calculate the positions where a and b differ using a != b, then find the mean of those values:

>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5, 6, 7])
>>> b = np.array([1, 2, 3, 5, 5, 6, 7])
>>> error = np.mean( a != b )
>>> error
0.14285714285714285
like image 61
mdml Avatar answered Oct 24 '22 21:10

mdml