英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论