python - numpy - 对数组的每一行应用聚合

标签 python arrays numpy

我想在 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) 的数组。

最佳答案

您输入的是 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]])

关于python - numpy - 对数组的每一行应用聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36204407/

相关文章:

java - java加密解密中如何将String转换为byte[]?

python - 从通过 ctypes 获取的 C-ptr 创建 numpy 数组

python - 如何将用户在 Matplotlib 中输入的点传递到 np.array?

python - 如何从 numpy 二维数组中提取子数组?

arrays - EXCEL 公式返回不同日期范围(在 ROW 中)和不同产品(在 COLUMN 中)的 SUM

c++ - 数组和动态数组有什么区别?

python - Python 正则表达式中的上一组匹配

python - 有没有办法用gym的OpenAI来减慢游戏环境的速度?

python - 派趋势 : How to specify a word as a Topic instead of a Search Term?

python - latin-1 到 ascii