24

I came across a problem with comparing the predictions of my model with the labels of training set. The arrays I'm using have shapes:

Training set (200000, 28, 28) (200000,)
Validation set (10000, 28, 28) (10000,)
Test set (10000, 28, 28) (10000,)

However, when checking the accuracy with the function:

def accuracy(predictions, labels):
    return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
          / predictions.shape[0])

It's giving me:

C:\Users\Arslan\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future. """

And it gives the accuracy as 0% for all datasets.

I think we cannot compare the arrays using '=='. How could I compare the arrays in the right way instead?

Arslan
  • 400
  • 1
  • 3
  • 10

3 Answers3

28

I assume the error occurs in this expression:

np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))

can you tell us something about the 2 arrays, predictions, labels? The usual stuff - dtype, shape, some sample values. Maybe go the extra step and show the np.argmax(...) for each.

In numpy you can compare arrays of the same size, but it has become pickier about comparing arrays that don't match in size:

In [522]: np.arange(10)==np.arange(5,15)
Out[522]: array([False, False, False, False, False, False, False, False, False, False], dtype=bool)
In [523]: np.arange(10)==np.arange(5,14)
/usr/local/bin/ipython3:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
  #!/usr/bin/python3
Out[523]: False
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Thanks. dimensions of the two matrices were messed up. It went smooth after rearranging the elements in the matrices. – Arslan Jun 19 '17 at 19:09
17

This error is telling you that the comparisson you're performing doesn't really make sense, since both arrays have different shapes, hence it can't perform elementwise comparisson. Here's an example:

x = np.random.randint(0,5,(3,2))
y = np.random.randint(0,5,(5,7))

Where attempting to do x==y will yield:

DeprecationWarning: elementwise comparison failed; this will raise an error in the future. x==y

The right way to do this, would be to use np.array_equal, which checks equality of both shape and elements:

np.array_equal(x,y)
# False

In the case of floats, np.allclose is more suited, since it allows to control both the relative and absolute tolerance of the comparisson result. Here's an example:

x = np.random.random((400,34))
y = x.round(6)

np.array_equal(x,y)
# False
np.allclose(x,y)
# False
np.allclose(x,y, atol=1e-05)
# True
yatu
  • 86,083
  • 12
  • 84
  • 139
-7

I solved this by upgrading python to 3.6.4 (latest)

conda update python
  • 16
    This did not help me. – ProfVersaggi Jun 07 '18 at 17:22
  • I think the point here is this error/behavior could be related to a Python or package version. – wordsforthewise Mar 16 '22 at 15:16
  • yeah, in some cases this might help, but downgrading a package version is often not the desired solution (I entered here just to find how to use the last method to do it, not to start a dependency hell), many times even not possible (imagine a team project in which you cannot just switch library versions) – xCovelus Nov 27 '22 at 19:35