-1

I would like to compare numpy arrays of equal size in terms of greater/smaller relations. Specifically, I have

>>> import numpy as np
>>> A = np.array([0.5, 2., 0.1, 12.])
>>> B = np.ones(len(A))
>>> A<B
array([ True, False,  True, False], dtype=bool)
>>> C = np.array([0.5, 2., 0.1, 12., 0.8])
>>> D = np.ones(len(C))
>>> C<D
array([ True, False,  True, False, False], dtype=bool)

The last element is False although 0.8 is less than 1.0. This seems to happen for uneven array lengths. Am I missing something here?

I'm using Python 2.7.6 with NumPy 1.8.0.

However, it works with NumPy 1.8.2.

ProtonK
  • 11
  • 3

1 Answers1

1

I think you mixed something up in your test. This is always the danger when working in an interactive session. For reproducing things, you should always use a self-contained minimal working example. I guess you cannot provide this to us.

Concluding that something fails for "uneven" lengths is a bit harsh, from just one simple test :-). Also, as DSM has pointed out, in your first snippet the output does not fit the input (length-wise). You should not copy/paste unrelated input and output, just to make it look like being related, even if you think it was related. Let the computer tell you.

For me, things work as expected:

>>> A = np.array([0.5, 2., 0.1, 12., 0.8])
>>> B = np.ones(len(A))
>>> A<B
array([ True, False,  True, False,  True], dtype=bool)
Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
  • Thanks for the reply! I copied your code, still same problem. Which versions? – ProtonK Jan 23 '15 at 12:54
  • Start off with a fresh Python process, `import numpy as np`, and run the three lines. Still "invalid" output? – Dr. Jan-Philip Gehrcke Jan 23 '15 at 12:59
  • No worries, I've tried it several times, still same problem. However, it works perfectly with NumPy 1.8.2 on a different machine. – ProtonK Jan 23 '15 at 13:05
  • Still, you should fix the first snippet in your question. It makes people doubt your results, for a reason. I have skimmed through 1.8.1 release notes, if "your bug" was fixed. Don't know exactly, have a look yourself: http://docs.scipy.org/doc/numpy/release.html#numpy-1-8-1-release-notes – Dr. Jan-Philip Gehrcke Jan 23 '15 at 13:09
  • Snippet fixed. I couldn't find anything in the release notes. – ProtonK Jan 23 '15 at 13:24
  • Then now you need to test with numpy 1.8.2 and, if the problem persists, open a bug report. – Dr. Jan-Philip Gehrcke Jan 23 '15 at 13:29
  • Okay lets leave it here. Something seems really messed up with my NumPy installation. I just tried with ancient 2.6.8 and NumPy 1.3.0. It works perfectly. Same with 2.7.6 and NumPy 1.8.2. – ProtonK Jan 23 '15 at 13:41