python - 计算没有for循环的numpy数组部分的平均值
问题描述
我有一个 NumPy 数组:
a.shape:
(24, 124, 117, 1)
如果我取两个内部数组的 10 x 10 子集
b = a[:,34:44,100:110,0]
b.shape:
(24, 10, 10)
要获取每个 10 x 10 数组中值的平均值列表:
the_list = []
for i in b:
the_list.append(np.nanmean(i))
print(the_list)
[1.3890099800025781, 1.4469500441974863, 1.3629133841057566, 1.415756533959169, 1.4254133193054614, 1.5217992758345105, 1.6735920775699795, 1.968677630129286, 2.097451850128511, 2.1386300417406416, 2.536669441756218, 2.3351648894197727, 2.794611866191022, 2.524879009293537, 3.1834270587157953, 4.049452682004729, 3.1542556793742387, 3.498059377421739, 4.439820758482223, 3.446379020262416, 4.8295208426437535, 3.7539767849251833, 5.08349934334655, 4.448043719645083]
for
有什么比循环更好的方法来做到这一点?
解决方案
np.nanmean(a[:,34:44,100:110,0], axis=(-1, -2)
感谢 Nils 的评论。
推荐阅读
- php - 与3表的雄辩关系
- c# - Parallel.Invoke 等到 HttpClient 重新登录 c#
- mysql - 如果 postid 不存在,MySQL 将记录插入一个表到另一个表
- c++ - 使用 c++ 开发时,宏 '__TIME__' 和 '__TIMESTAMP__' 无法刷新
- android - 使用搜索视图过滤 gridview
- python - Python 依赖地狱:virtualenv 和全局依赖之间的妥协?
- ios - 斯威夫特 | UIBarButton 不工作/不去行动目标
- android - 在 Google Play 上更新应用程序时出现错误 DF-DFERH-01
- sql - 如何在 SQL Server 中找到滚动季度收入的总和?
- javascript - 波浪js背景颜色