[Numpy-discussion] add axis to results of reduction (mean, min, ...)

josef.pktd@gmai... josef.pktd@gmai...
Thu Aug 6 10:55:58 CDT 2009


What's the best way of getting back the correct shape to be able to
broadcast, mean, min,.. to the original array, that works for
arbitrary dimension and axis?

I thought I have seen some helper functions, but I don't find them anymore?

Josef

>>> a
array([[1, 2, 3, 3, 0],
       [2, 2, 3, 2, 1]])
>>> a-a.max(0)
array([[-1,  0,  0,  0, -1],
       [ 0,  0,  0, -1,  0]])
>>> a-a.max(1)
Traceback (most recent call last):
  File "<pyshell#135>", line 1, in <module>
    a-a.max(1)
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>>> a-a.max(1)[:,None]
array([[-2, -1,  0,  0, -3],
       [-1, -1,  0, -1, -2]])


More information about the NumPy-Discussion mailing list