numpy矢量化和原子矢量 [英] Numpy vectorize and atomic vectors
本文介绍了numpy矢量化和原子矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想像预期的那样在数组上实现一个类似于numpy.sum函数的功能,例如np.sum([2,3],1)= [3,4]和np.sum([1,2],[3,4])= [4,6].
I would like to implement a function that works like the numpy.sum function on arrays as on expects, e.g. np.sum([2,3],1) = [3,4] and np.sum([1,2],[3,4]) = [4,6].
一个简单的测试实现却表现得有些尴尬:
Yet a trivial test implementation already behaves somehow awkward:
import numpy as np
def triv(a, b): return a, b
triv_vec = np.vectorize(fun, otypes = [np.int])
triv_vec([1,2],[3,4])
结果:
array([0, 0])
而不是期望的结果:
array([[1,3], [2,4]])
任何想法,这是怎么回事?谢谢
Any ideas, what is going on here? Thx
推荐答案
您需要otypes=[np.int,np.int]
:
triv_vec = np.vectorize(triv, otypes=[np.int,np.int])
print triv_vec([1,2],[3,4])
(array([1, 2]), array([3, 4]))
otypes:str或dtypes的列表,可选
输出数据类型.必须将其指定为一串类型代码字符或一列数据类型说明符. 每个输出应该有一个数据类型说明符.
这篇关于numpy矢量化和原子矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文