You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In np.reduce, np.mean, etc., the WHERE argument allows for selective reduction of only certain elements. It performs a one-way broadcast of WHERE to ARRAY:
WHERE: A boolean array which is broadcasted to match the dimensions of [array], and selects elements to include in the reduction.
But sometimes you'd like the broadcast to go in the other direction. For example, suppose you want to compute the average row number in a 2D array where some condition is true:
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (5,10) and requested shape (5,1)
because the broadcast is tried in one direction only (from WHERE -> ARRAY).
If ARRAY would otherwise be a repeated set of values (e.g. a column vector of row indices), it seems sensible to allow broadcasting between WHERE and ARRAY in either direction. In this example, ARRAY -> WHERE broadcasting would occur. Otherwise, you must waste memory "filling out" ARRAY:
I have mixed feelings about the suggestion. In most cases NumPy always broadcasts, but there are exceptions where it isn't clear that the arguments are on a similar standing (one example is that out=some_array will never broadcast, but of course that is an extreme case).
So it seems fine to me, but I am also unsure that it is wouldn't cause unintended broadcasts, when manual broadcasting via a = np.broadcast_to(a, ...) is pretty simple.
Proposed new feature or change:
In
np.reduce
,np.mean
, etc., theWHERE
argument allows for selective reduction of only certain elements. It performs a one-way broadcast ofWHERE
toARRAY:
But sometimes you'd like the broadcast to go in the other direction. For example, suppose you want to compute the average row number in a 2D array where some condition is true:
This raises:
because the broadcast is tried in one direction only (from
WHERE
->ARRAY).
If
ARRAY
would otherwise be a repeated set of values (e.g. a column vector of row indices), it seems sensible to allow broadcasting betweenWHERE
andARRAY
in either direction. In this example,ARRAY
->WHERE
broadcasting would occur. Otherwise, you must waste memory "filling out"ARRAY:
The text was updated successfully, but these errors were encountered: