3

I have 2 np.array() as below. When I compare the two using "==", I get an output but with a deprecation warning. There is no warning when comparing 2 arrays with a same matrix.

What's the workaround to get still the same result but with no warning?

Thank you so much!

x = np.array([[0,1,2],[3,4,5]])
x

Out: array([[0, 1, 2],
       [3, 4, 5]])

y = np.array([[6,7],[8,9],[10,11]])
y

Out: array([[ 6,  7],
       [ 8,  9],
       [10, 11]])

x == y

Out: False

**C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
  """Entry point for launching an IPython kernel.**

Screenshot:

yatu
  • 86,083
  • 12
  • 84
  • 139
foto_coder
  • 31
  • 2

3 Answers3

2

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:

x==y

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
yatu
  • 86,083
  • 12
  • 84
  • 139
1

Have a look at this link:

np.array_equal(x,y)  # test if same shape, same elements values

np.array_equiv(x,y)  # test if broadcastable shape, same elements values

and also this link can be useful.

Alex
  • 3,923
  • 3
  • 25
  • 43
1

x and y have different shapes.

You can compare two Numpy arrays with the same shape element-wise way.

This answer maybe helpful.

Alex
  • 3,923
  • 3
  • 25
  • 43
Ausrada404
  • 499
  • 1
  • 7
  • 17