mxnet.np.insert

insert(arr, obj, values, axis=None)

Insert values along the given axis before the given indices.

Parameters
  • arr (ndarray) – Input array.

  • obj (int, slice or ndarray of int64) – Object that defines the index or indices before which values is inserted. Support for multiple insertions when obj is a single scalar or a sequence with one element (only support int32 and int64 element).

  • values (ndarray) – Values to insert into arr. If the type of values is different from that of arr, values is converted to the type of arr.

  • axis (int, optional) – Axis along which to insert values. If axis is None then arr is flattened first.

Returns

  • out (ndarray) – A copy of arr with values inserted. Note that insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array.

  • .. note::

    • Note that for higher dimensional inserts obj=0 behaves very different from obj=[0] just like arr[:,0,:] = values is different from arr[:,[0],:] = values.

    • If obj is a ndarray, it’s dtype only supports int64

Examples

>>> a = np.array([[1, 1], [2, 2], [3, 3]])
>>> a
array([[1., 1.],
       [2., 2.],
       [3., 3.]])
>>> np.insert(a, 1, np.array(5))
array([1., 5., 1., 2., 2., 3., 3.])
>>> np.insert(a, 1, np.array(5), axis=1)
array([[1., 5., 1.],
       [2., 5., 2.],
       [3., 5., 3.]])

Difference between sequence and scalars:

>>> np.insert(a, np.array([1], dtype=np.int64), np.array([[1],[2],[3]]), axis=1)
array([[1., 1., 1.],
       [2., 2., 2.],
       [3., 3., 3.]])
>>> np.insert(a, 1, np.array([1, 2, 3]), axis=1)
array([[1., 1., 1.],
       [2., 2., 2.],
       [3., 3., 3.]])
>>> b = a.flatten()
>>> b
array([1., 1., 2., 2., 3., 3.])
>>> np.insert(b, np.array([2, 2], dtype=np.int64), np.array([5, 6]))
array([1., 1., 5., 6., 2., 2., 3., 3.])
>>> np.insert(b, slice(2, 4), np.array([5, 6]))
array([1., 1., 5., 2., 6., 2., 3., 3.])

# type casting >>> np.insert(b.astype(np.int32), np.array([2, 2],dtype=’int64’), np.array([7.13, False])) array([1, 1, 7, 0, 2, 2, 3, 3], dtype=int32)

>>> x = np.arange(8).reshape(2, 4)
>>> idx = np.array([1, 3], dtype=np.int64)
>>> np.insert(x, idx, np.array([999]), axis=1)
array([[  0., 999.,   1.,   2., 999.,   3.],
       [  4., 999.,   5.,   6., 999.,   7.]])