因为
otypes在向量化函数时未指定(输出数据类型),所以NumPy假定您要返回
int32值数组。
当给定
x的向量化函数
vfunz第一次看到时
-10.,返回整数
0,因此确定
dtype返回数组的应当为
int32。
要解决此问题,请指定
otypes为
np.float值:
vfunz = np.vectorize(c_inf_comp, otypes=[np.float])
然后,您将获得预期的结果:
>>> vfunz(x)array([ 0. , 0.99004983])
(或者,可以通过在的
else条件下返回浮点值来解决该问题
c_inf_comp,即
return0.0,这样,
np.vectorize(c_inf_comp)即使第一次看到负数,by生成的函数也将返回浮点值数组。)



