numpy数组方向均值,不降低维数 [英] numpy array directional mean without dimension reduction

查看:61
本文介绍了numpy数组方向均值,不降低维数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何执行以下操作:
对于3D numpy数组,我想在一个维度上取均值,然后将值分配回具有相同形状的3D数组,并在均值的推导方向上重复这些值...
我正在努力制定3D的示例,但在2D(4x4)中,我想看起来像这样

How would I do the following:
With a 3D numpy array I want to take the mean in one dimension and assign the values back to a 3D array with the same shape, with duplicate values of the means in the direction they were derived...
I'm struggling to work out an example in 3D but in 2D (4x4) it would look a bit like this I guess

array[[1, 1, 2, 2]     
      [2, 2, 1, 0]  
      [1, 1, 2, 2]  
      [4, 8, 3, 0]] 

成为

array[[2, 3, 2, 1]     
      [2, 3, 2, 1]  
      [2, 3, 2, 1]  
      [2, 3, 2, 1]]   

我正在为np.mean和平均时的尺寸损失而苦苦挣扎.

I'm struggling with the np.mean and the loss of dimensions when take an average.

推荐答案

您可以在取均值之后重新调整数组的大小:

You can resize the array after taking the mean:

In [24]: a = np.array([[1, 1, 2, 2],
[2, 2, 1, 0],
[2, 3, 2, 1],
[4, 8, 3, 0]])
In [25]: np.resize(a.mean(axis=0).astype(int), a.shape)
Out[25]: 
array([[2, 3, 2, 0],
       [2, 3, 2, 0],
       [2, 3, 2, 0],
       [2, 3, 2, 0]])

这篇关于numpy数组方向均值,不降低维数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆