如何在多维numpy数组的多个轴上找到最大值?

时间:2018-11-07 22:01:35

标签: python numpy

我有这些尺寸的numpy数组

data.shape (categories, models, types, events, days) -> (10, 11, 50, 100, 14)

现在,我想为11个模型中的每一个查找所有事件的最长14天。但是我不确定如何以numpy的方式进行操作。我不确定这是否正确。

modelmax = []
nmodels = 11

for modelcount in range(nmodels):
    modelmax.append(np.max(data[0][modelcount][:], axis=2))

例如,对于100个事件:

np.max(data, axis=4)[0][0][0])

[ 3.9264417   3.3029506   3.0707457   3.6646023   1.7508441   3.1634364
  6.195052    1.5353022   1.8033538   1.4508389   1.3882699   2.0849068
  3.654939    6.6364765   3.92829     6.6467876   1.5442419   4.639682
  9.361191    5.261462    1.7438816   5.6970205   2.4356377   1.6073244
  2.6177561   6.886767    3.890399    2.8880894   1.9826577   1.0888597
  4.3763924   3.8597727   1.790302    1.0277777   6.270729    9.311213
  2.318774    2.9298437   1.139397    0.9598383   3.0489902   1.6736581
  1.3983868   2.0979824   4.169757    1.0739225   1.5311266   1.4676268
  1.726325    1.8057758   2.226462    2.6197987   4.49518     2.3042605
  5.7164993   1.182242    1.5107205   2.2920077   2.205539    1.4702082
  2.154468    2.0641963   4.9628353   1.9987459   2.1360166   1.7073958
  1.943267    7.5767093   1.3124634   2.2648168   1.1504744   3.210688
  2.6720855   2.998225    4.365262    3.5410352  10.765423    4.6292825
  3.1789696   0.92157686  1.663245    1.5835482   3.1070056   1.6918416
  8.086268    3.7994847   2.4314868   1.6471033   1.1688241   1.7820593
  3.3509188   1.3092748   3.7915008   1.018912    3.2404447   1.596657
  2.0869658   2.6753283   2.1096318   8.786542  ]

我也尝试过

np.max(dryflow[0][:], axis=3)

但是这些多维索引让我感到困惑。

1 个答案:

答案 0 :(得分:0)

如果您希望days的每个组合的最大值超过(category, model, type, event),则得到的形状为(10,11,50,100,1):

np.max(data, axis=4)

如果您希望days的每个组合的最大值超过types(category, model, event),则得到的形状为(10,11,100,1):

np.max(data, axis=(2,4))