Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
support to insert a sigle number
Browse files Browse the repository at this point in the history
  • Loading branch information
JiangZhaoh committed Dec 24, 2019
1 parent 7649a6e commit 3c2c5a7
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 298 deletions.
30 changes: 21 additions & 9 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,15 @@ def insert(arr, obj, values, axis=None):
----------
arr : ndarray
Input array.
obj : int, slice or ndarray of ints
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`.
The type of `values` should equal to the type of `arr`.
`values` should be shaped so that ``arr[...,obj,...] = values``
is legal.
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.
Expand All @@ -584,9 +583,10 @@ def insert(arr, obj, values, axis=None):
Notes
-----
Note that for higher dimensional inserts `obj=0` behaves very different
- 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
--------
Expand All @@ -604,7 +604,7 @@ def insert(arr, obj, values, axis=None):
Difference between sequence and scalars:
>>> np.insert(a, np.array([1], dtype=np.int32), np.array([[1],[2],[3]]), axis=1)
>>> 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.]])
Expand All @@ -622,15 +622,27 @@ def insert(arr, obj, values, axis=None):
>>> np.insert(b, slice(2, 4), np.array([5, 6]))
array([1., 1., 5., 2., 6., 2., 3., 3.])
>>> np.insert(b, np.array([2, 2], dtype=np.int32), np.array([7.13, False]))
array([1. , 1. , 7.13, 0. , 2. , 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.int32)
>>> 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.]])
"""
if isinstance(values, numeric_types):
if isinstance(obj, slice):
start = obj.start
stop = obj.stop
step = 1 if obj.step is None else obj.step
return _npi.insert(arr, val=values, start=start, stop=stop, step=step, axis=axis)
elif isinstance(obj, integer_types):
return _npi.insert(arr, val=values, int_ind=obj, axis=axis)
elif isinstance(obj, NDArray):
return _npi.insert(arr, obj, val=values, axis=axis)

if not isinstance(arr, NDArray):
raise TypeError("'arr' can not support type {}".format(str(type(arr))))
if not isinstance(values, NDArray):
Expand Down
19 changes: 10 additions & 9 deletions python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7426,16 +7426,15 @@ def insert(arr, obj, values, axis=None):
----------
arr : ndarray
Input array.
obj : int, slice or ndarray of ints
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`.
The type of `values` should equal to the type of `arr`.
`values` should be shaped so that ``arr[...,obj,...] = values``
is legal.
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.
Expand All @@ -7449,9 +7448,10 @@ def insert(arr, obj, values, axis=None):
Notes
-----
Note that for higher dimensional inserts `obj=0` behaves very different
- 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
--------
Expand All @@ -7469,7 +7469,7 @@ def insert(arr, obj, values, axis=None):
Difference between sequence and scalars:
>>> np.insert(a, np.array([1], dtype=np.int32), np.array([[1],[2],[3]]), axis=1)
>>> 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.]])
Expand All @@ -7487,11 +7487,12 @@ def insert(arr, obj, values, axis=None):
>>> np.insert(b, slice(2, 4), np.array([5, 6]))
array([1., 1., 5., 2., 6., 2., 3., 3.])
>>> np.insert(b, np.array([2, 2], dtype=np.int32), np.array([7.13, False]))
array([1. , 1. , 7.13, 0. , 2. , 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.int32)
>>> 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.]])
Expand Down
22 changes: 16 additions & 6 deletions python/mxnet/symbol/numpy/_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2871,16 +2871,15 @@ def insert(arr, obj, values, axis=None):
----------
arr : _Symbol
Input array.
obj : int, slice or _Symbol of ints
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 : _Symbol
Values to insert into `arr`.
The type of `values` should equal to the type of `arr`.
`values` should be shaped so that ``arr[...,obj,...] = values``
is legal.
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.
Expand All @@ -2894,10 +2893,21 @@ def insert(arr, obj, values, axis=None):
Notes
-----
Note that for higher dimensional inserts `obj=0` behaves very different
- 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
"""
if isinstance(values, numeric_types):
if isinstance(obj, slice):
start = obj.start
stop = obj.stop
step = 1 if obj.step is None else obj.step
return _npi.insert(arr, val=values, start=start, stop=stop, step=step, axis=axis)
elif isinstance(obj, integer_types):
return _npi.insert(arr, val=values, int_ind=obj, axis=axis)
elif isinstance(obj, NDArray):
return _npi.insert(arr, obj, val=values, axis=axis)
if not isinstance(arr, ndarray): # pylint: disable= undefined-variable
raise TypeError("'arr' can not support type {}".format(str(type(arr))))
if not isinstance(values, ndarray): # pylint: disable= undefined-variable
Expand Down
Loading

0 comments on commit 3c2c5a7

Please sign in to comment.