maximusdooku
maximusdooku

Reputation: 5542

How can I find maximum along multiple axes of a multidimensional numpy array?

I have a numpy array of these dimensions

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

Now, I want to find the maximum of the 14 days for all events for each of the 11 models. But I am not sure how to do it in the numpy way. I am not sure if this is correct.

modelmax = []
nmodels = 11

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

As an example, for the 100 events:

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  ]

I have also tried

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

But these multidimensional indices are leaving me confused.

Upvotes: 3

Views: 6353

Answers (1)

Tomi Aarnio
Tomi Aarnio

Reputation: 2527

If you want the maximum over days for each combination of (category, model, type, event), for a resulting shape of (10, 11, 50, 100, 1):

np.max(data, axis=4)

If you want the maximum over days and types for each combination of (category, model, event), for a resulting shape of (10, 11, 100, 1):

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

Upvotes: 10

Related Questions