-1

I have an array with elements, and I want to sum up the accuracy. I want to sum the arrays that have the elements in the same order. I rather not be writing a for loop going through each element with zip and summing them up, is there an easier way to do this?

The two arrays are as follows, and currently my code is below for calculating the sum.

yp = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
y = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]

sums = np.sum(yp == y)

I am getting an accuracy of zero.

Jam1
  • 629
  • 12
  • 25

1 Answers1

0

Using your example:

yp = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
y = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]

# First make the two arrays in question numpy arrays.
yp = np.array(yp)
y = np.array(y)
array_length = y.shape[1] # store length of sub arrays

equal_elements = np.array(yp) == np.array(y)  # check all equal elements
sums = np.sum(equal_elements, 1) # sum the number of equal elements in each sub array, use axis 1 as each array/sample is axis 0
equal_arrays = np.where(sums==array_length)[0] # returns a tuple, so index first element immediately

number_equal_arrays = equal_arrays.shape[0]  # What elements are equal
print('Number of equal arrays %d' % number_equal_arrays)
print('Accuracy %0.2f' % (number_equal_arrays/yp.shape[0]))

prints

Number of equal arrays 6

Accuracy 1.00

Community
  • 1
  • 1
pseudogram
  • 301
  • 1
  • 2
  • 8