我有这个函数来矢量化:
if x >= y, then x*y
else x/y
我的代码是:
def vector_function(x, y):
if y >= x:
return x*y
else:
return x/y
vfunc = np.vectorize(vector_function)
return vfunc
raise NotImplementedError
但我得到了一个错误:
'>=' not supported between instances of 'int' and 'list'
有人能帮忙吗
Tags:
纯“矢量化”版本是:
根据数组的大小,这比Stefans answer快2到10倍
我选择这种{}方法是因为它是最简单、最紧凑的{}广播方式。它可能不是最快的,这取决于
/
和*
的“成本”问题是函数内部的
vectorize
-call但是,请注意,numpy.vectorize:提供
vectorize
函数主要是为了方便,而不是为了性能。该实现本质上是一个for循环相关问题 更多 >
编程相关推荐