Home > Net >  why np.vectorize(max) does not work as expected
why np.vectorize(max) does not work as expected

Time:04-13

I have the following code

import numpy as np
f = np.vectorize(lambda x: max(x, 0))
print(f(-0.3))
print(f(1.8))
print(f([1.8, -0.3]))
print(f([-0.3, 1.8]))

I have the output:

0
1.8
[1.8 0. ]
[0 1]

Could any one explain why I get [0 1] for f([-0.3, 1.8])

CodePudding user response:

As stated in the docs:

The data type of the output of vectorized is determined by calling the function with the first element of the input. This can be avoided by specifying the otypes argument.

So since the output of the first is a int object, it infers numpy.int64 as the output dtype.

More importantly, though, you probably should just not be using numpy.vectorize here, it doesn't really make any sense. Why are you using it, if I might ask? Keep in mind another important part of the docs:

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.

CodePudding user response:

Not sure exactly why, but I think vectorize isn't too bright about distinguishing data types. Change "0" to "0.0" and you may get better results.

CodePudding user response:

It's dangerous to use np.vectorize without reading full docs - or at least annoying.

The return dtype is determined by the first test case:

In [52]: np.vectorize(lambda x: max(x,0))(np.linspace(-1,1,6))
Out[52]: array([0, 0, 0, 0, 0, 1])

Note the integer values. With a float value:

In [53]: np.vectorize(lambda x: max(x,0.0))(np.linspace(-1,1,6))
Out[53]: array([0. , 0. , 0. , 0.2, 0.6, 1. ])

Or setting it explicitly:

In [54]: np.vectorize(lambda x: max(x,0),otypes=['f'])(np.linspace(-1,1,6))
Out[54]: array([0. , 0. , 0. , 0.2, 0.6, 1. ], dtype=float32)

But for a function like this, skip vectorize entirely:

In [55]: np.maximum(np.linspace(-1,1,6),0)
Out[55]: array([0. , 0. , 0. , 0.2, 0.6, 1. ])
  • Related