bayespy.utils.misc.put

bayespy.utils.misc.put(x, indices, y, axis=-1, ufunc=<ufunc 'add'>)[source]

A kind of inverse mapping of np.take

In a simple, the operation can be thought as:

x[indices] += y

with the exception that all entries of y are used instead of just the first occurence corresponding to a particular element. That is, the results are accumulated, and the accumulation function can be changed by providing ufunc. For instance, np.multiply corresponds to:

x[indices] *= y

Whereas np.take picks indices along an axis and returns the resulting array, put similarly picks indices along an axis but accumulates the given values to those entries.

Example

>>> x = np.zeros(3)
>>> put(x, [2, 2, 0, 2, 2], 1)
array([1., 0., 4.])

y must broadcast to the shape of np.take(x, indices):

>>> x = np.zeros((3,4))
>>> put(x, [[2, 2, 0, 2, 2], [1, 2, 1, 2, 1]], np.ones((2,1,4)), axis=0)
array([[1., 1., 1., 1.],
       [3., 3., 3., 3.],
       [6., 6., 6., 6.]])