python - How to compare a numpy array with a scalar? -
this question may seem basic it's generating big mess.
i try compare numpy.array
scalar as:
a=numpy.array([0.,1.,2.,-1.,-4.]) if a.any()>0.: print 'a:',a
as expected get:
a: [ 0. 1. 2. -1. -4.]
now if same find negative values
a=numpy.array([0.,1.,2.,-1.,-4.]) if a.any()<0.: print 'a:',a
i don't means values greater 0.
its because of a.any
returns true (it returns true of 1 of elements meets condition , false in otherwise).and since true , 1 same objects in python (true==1) ,your condition interpreted 1<0
python, false!
>>> true<0 false >>> a.any()<0. false
and instead of need (a<0).any()
>>> (a<0).any() true
Comments
Post a Comment