Hallgeir Wilhelmsen
Hallgeir Wilhelmsen

Reputation: 1134

Opposite of numpy.broadcast_to

Is there an opposite of numpy.broadcast_to, e.g. numpy.project_to(array, function, axis)?

np.project_to(
  np.array([[1,1,1], [2,1,3], [2,2,2]], dtype=float),
  lambda x, y: x/y,
  axis=1)

which would return

array([ 1., 0.66666667, 0.5])

Upvotes: 0

Views: 455

Answers (1)

jakevdp
jakevdp

Reputation: 86433

It looks like what you're asking for is a reduction along a particular axis: this can be done using the reduce method of any binary ufunc. For example:

>>> x = np.array([[1,1,1], [2,1,3], [2,2,2]], dtype=float)

>>> np.divide.reduce(x, axis=1)
array([ 1.        ,  0.66666667,  0.5       ])

Upvotes: 2

Related Questions