如何在Python中找到平均值并重新塑造数组

huangapple go评论67阅读模式
英文:

How to find the mean and reshape an array in python

问题

我有一个形状为(901, 201, 3, 20)的数组。请问,如何找到第三个矩阵的平均值,以得到(901, 201, 20)(901, 201, 1, 20)

英文:

I have an array of the shape (901, 201, 3, 20).

Please, how can I find the mean of the third matrix to give me (901, 201, 20) or (901, 201, 1, 20)?

答案1

得分: 1

使用numpy时,可以很容易地使用numpy.mean。第一个参数是数组,第二个参数是指定计算平均值的轴。

import numpy as np

a = np.zeros((901, 201, 3, 20))
b = np.mean(a, axis=2)

print(b.shape) # (901, 201, 20)

我建议您查阅文档。您还可以使用keepdims参数保留轴的维度,从而得到形状为(901, 201, 1, 20)的结果。希望对您有帮助。

英文:

Assuming you are using numpy, this is quite easy using numpy.mean. The first argument would be the array, and as second argument you specify the axis over which to take the mean.

import numpy as np

a = np.zeros((901, 201, 3, 20))
b = np.mean(a, axis=2)

print(b.shape) # (901, 201, 20)

I recommend you to take a look at the documentation. You could for example also use the keepdims argument to keep the dimension of the axis, resulting in a shape of (901, 201, 1, 20). Hope this helps

huangapple
  • 本文由 发表于 2023年5月10日 22:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定