numpy - Why my mask failed in Python? -


my code:

#!/usr/bin/python  import numpy np  names = np.array(['bob', 'joe', 'will', 'bob', 'will', 'joe', 'joe']) data = np.random.randn(7, 4) + 0.8  print (data)  mask2= ((names != 'joe') == 7.0) d2 = data[mask2] print (d2)  d3 = data[names != 'joe'] = 7.0 print (d3) 

actually,my intention same solution both mask , other expression. have solved patric,s help

mask2= (names != 'joe') data[mask2] = 7.0 print (data) 

then have:

[[ 7.          7.          7.          7.        ]  [-0.73168514  2.26996071 -0.24892468  1.31421193]  [ 7.          7.          7.          7.        ]  [ 7.          7.          7.          7.        ]  [ 7.          7.          7.          7.        ]  [ 0.74771766  2.44888399  0.62641731 -0.12963696]  [ 0.08604169  2.25468039  2.1960925   0.88218726]] 

mask2 = ((names != 'joe') == 7.0)

why mask failed in python?

this mask doesn't make sense, expression, compared result of names != 'joe' 7.0

in [13]: names != 'joe' out[13]: array([ true, false,  true,  true,  true, false, false], dtype=bool) 

so it's natural false everywhere:

in [14]: ((names != 'joe') == 7.0) out[14]: array([false, false, false, false, false, false, false], dtype=bool) 

your other mask makes sense, in form:

x[mask] = value 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -