我想在numpy数组上应用一些聚合。
x = np.array([ ([[ 1.87918162, 1.12919822, -1.63856741],\
[ 0.40560484, 0.96425656, 0.7847214 ],\
[-0.83472207, 0.88918246, -0.83298299],\
[-1.29211004, 0.71730071, -2.09109609],\
[-1.65800248, 0.49154087, 0.14932455]]),\
([[ 1.87918162, 1.12919822, -1.63856741],\
[-0.21786626, -0.23561859, -0.19750753],\
[-0.83472207, 0.88918246, -0.83298299],\
[-0.34967282, 0.51348973, -0.30882943],\
[ 0.35654636, -0.64453956, -1.3066075 ],\
[ 0.187328 , -1.32496725, -0.05783984]])])
print type(x)
print x[0]
print np.mean(x[0], axis=0)
print np.mean(x, axis=0)
>>> <type 'numpy.ndarray'>
>>> [[1.87918162, 1.12919822, -1.63856741], [0.40560484, 0.96425656, 0.7847214], [-0.83472207, 0.88918246, -0.83298299], [-1.29211004, 0.71730071, -2.09109609], [-1.65800248, 0.49154087, 0.14932455]]
>>> [-0.30000963 0.83829576 -0.72572011]
错误是:
TypeError: unsupported operand type(s) for /: 'list' and 'long'
我不明白为什么它在一行而不是在整个阵列上工作。我怀疑阵列形状的不规则性会导致问题 但是如果不在数组上使用for循环迭代并在一个数组中连接所有结果,我怎么能处理呢?
编辑:
预期结果是垂直每行的总和。所以结果应该是一个维度数组(2,3)。
答案 0 :(得分:2)
您输入的是datatype = Object
的NumPy数组,并且数据格式不规则,因此您无法使用np.mean(x, axis=0)
之类的内容。相反,对于这种情况,您可以垂直堆叠这些行,然后使用np.add.reduceat
执行总和减少,直到x
中axis=0
沿x
的每个元素的长度结束。因此,我们将采用几乎矢量化的方法(几乎因为我们通过循环理解获得lens = np.array([len(i) for i in x])
cut_idx = np.append(0,lens[:-1]).cumsum()
out = np.add.reduceat(np.vstack(x),cut_idx,axis=0)/lens[:,None]
的每个元素的长度,但这不是计算密集型的),如此 -
In [89]: x = np.array([ ([[ 1.87918162, 1.12919822, -1.63856741],\
...: [ 0.40560484, 0.96425656, 0.7847214 ],\
...: [-0.83472207, 0.88918246, -0.83298299],\
...: [-1.29211004, 0.71730071, -2.09109609],\
...: [-1.65800248, 0.49154087, 0.14932455]]),\
...: ([[ 1.87918162, 1.12919822, -1.63856741],\
...: [-0.21786626, -0.23561859, -0.19750753],\
...: [-0.83472207, 0.88918246, -0.83298299],\
...: [-0.34967282, 0.51348973, -0.30882943],\
...: [ 0.35654636, -0.64453956, -1.3066075 ],\
...: [ 0.187328 , -1.32496725, -0.05783984]]),\
...: ([[ 1.87918162, 1.12919822, -1.63856741],\
...: [-1.29211004, 0.71730071, -2.09109609],\
...: [-1.65800248, 0.49154087, 0.14932455]])
...: ])
In [90]: np.mean(x[0], axis=0)
Out[90]: array([-0.30000963, 0.83829576, -0.72572011])
In [91]: np.mean(x[1], axis=0)
Out[91]: array([ 0.17013247, 0.0544575 , -0.72372245])
In [92]: np.mean(x[2], axis=0)
Out[92]: array([-0.35697697, 0.7793466 , -1.19344632])
In [93]: out
Out[93]:
array([[-0.30000963, 0.83829576, -0.72572011],
[ 0.17013247, 0.0544575 , -0.72372245],
[-0.35697697, 0.7793466 , -1.19344632]])
这是针对问题中列出的示例输入的扩展版本的示例运行 -
{{1}}