Can't explain this weird numpy vectorize result on an array

Viewed 29
first = np.random.rand(3,5)

def myfunc(z):
    if  z > 0.5:
        val =  z
    else:
        val =  0
    return val

second = np.vectorize(myfunc)(first)

def myfunc2(z):
    if  z < 0.5:
        val =  z
    else:
        val =  0
    return val

third = np.vectorize(myfunc2)(first)
print("first")
print(first)
print("second")
print(second)
print("third")
print(third)

Results are as follows :

first
[[0.35583844 0.89866138 0.06983657 0.04930049 0.3802954 ]
 [0.58969783 0.57097607 0.97938735 0.96245392 0.42167587]
 [0.17149216 0.6038677  0.56824784 0.84236898 0.5252295 ]]
second
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
third
[[0.35583844 0.         0.06983657 0.04930049 0.3802954 ]
 [0.         0.         0.         0.         0.42167587]
 [0.17149216 0.         0.         0.         0.        ]]

Why is the second array all zero? I am scratching my head over this unexpected result. I am not sure if I am doing something wrong or totally misunderstanding the vectorize function.

0 Answers
Related