Pythonic for partial max in a numpy array

I am wondering if there is a quick way to calculate the partial maximum in a numpy array. Example:

a = [1,2,3,4,5,6,7,8,9]

If we fix the part size to 3, I would like the answer to be:

b = [3,6,9]

where 3 is the maximum of the first part a [1,2,3], 6 is the maximum of the second part [4,5,6], etc.

+4
source share
2 answers

Try changing, and then take the maximum:

import numpy as np

a = np.array([1,2,3,4,5,6,7,8,9])
>>> a.reshape((len(a) / 3, 3)).max(axis=1)
array([3, 6, 9])

If the length of the array is not divisible by a number, your question requires a larger definition in order to say what you mean in this case.

Edit @ajcr in the comments below shows a much better way to change:

>>> a.reshape((-1, 3)).max(axis=1)
+3
source

numpy, , 2d, max/column max.

Edit1: ajcr .

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
a = a.reshape((-1, 3))
print a.max(1)

:

[ 3  6  9 12]
+3

Source: https://habr.com/ru/post/1627662/


All Articles