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

Commit 93c2644

Browse files
committed
add op : append / fix op : concatenate (when axis == None)
1 parent a67dd6c commit 93c2644

File tree

9 files changed

+530
-173
lines changed

9 files changed

+530
-173
lines changed

python/mxnet/ndarray/numpy/_op.py

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,7 @@
3838
'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming', 'blackman', 'flip',
3939
'around', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take',
4040
'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal',
41-
'hsplit', 'rot90', 'einsum', 'true_divide', 'append']
42-
43-
44-
@set_module('mxnet.ndarray.numpy')
45-
def append(arr, values, axis=None):
46-
"""
47-
Append values to the end of an array.
48-
Parameters
49-
----------
50-
arr : ndarray
51-
Values are appended to a copy of this array.
52-
values : ndarray
53-
These values are appended to a copy of `arr`. It must be of the
54-
correct shape (the same shape as `arr`, excluding `axis`). If
55-
`axis` is not specified, `values` can be any shape and will be
56-
flattened before use.
57-
axis : int, optional
58-
The axis along which `values` are appended. If `axis` is not
59-
given, both `arr` and `values` are flattened before use.
60-
Returns
61-
-------
62-
append : ndarray
63-
A copy of `arr` with `values` appended to `axis`. Note that
64-
`append` does not occur in-place: a new array is allocated and
65-
filled. If `axis` is None, `out` is a flattened array.
66-
See Also
67-
--------
68-
insert : Insert elements into an array.
69-
delete : Delete elements from an array.
70-
Examples
71-
--------
72-
>>> np.append(np.array([1, 2, 3]), np.array([[4, 5, 6],[7, 8, 9]]))
73-
array([1., 2., 3., 4., 5., 6., 7., 8., 9.])
74-
When `axis` is specified, `values` must have the correct shape.
75-
>>> np.append(np.array([[1, 2, 3], [4, 5, 6]]), np.array([[7, 8, 9]]), axis=0)
76-
array([[1., 2., 3.],
77-
[4., 5., 6.],
78-
[7., 8., 9.]])
79-
"""
80-
if axis is None:
81-
arr = arr.flatten()
82-
values = values.flatten()
83-
axis = 0
84-
return concatenate((arr, values), axis=axis)
41+
'hsplit', 'rot90', 'einsum', 'true_divide']
8542

8643

8744
@set_module('mxnet.ndarray.numpy')
@@ -2963,8 +2920,22 @@ def concatenate(seq, axis=0, out=None):
29632920
-------
29642921
res : ndarray
29652922
The concatenated array.
2966-
"""
2967-
return _npi.concatenate(*seq, dim=axis, out=out)
2923+
2924+
Examples
2925+
-------
2926+
>>> a = np.array([[1, 2], [3, 4]])
2927+
>>> b = np.array([[5, 6]])
2928+
>>> np.concatenate((a, b), axis = 0)
2929+
>>> array([[1., 2.],
2930+
[3., 4.],
2931+
[5., 6.]])
2932+
>>> np.concatenate((a, b), axis = None)
2933+
>>> array([1., 2., 3., 4., 5., 6.])
2934+
>>> np.concatenate((a, b.T), axis = 1)
2935+
>>> array([[1., 2., 5.],
2936+
[3., 4., 6.]])
2937+
"""
2938+
return _npi.concatenate(*seq, axis=axis, out=out)
29682939

29692940

29702941
@set_module('mxnet.ndarray.numpy')

python/mxnet/numpy/multiarray.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,6 +4533,7 @@ def vsplit(ary, indices_or_sections):
45334533
@set_module('mxnet.numpy')
45344534
def concatenate(seq, axis=0, out=None):
45354535
"""Join a sequence of arrays along an existing axis.
4536+
45364537
Parameters
45374538
----------
45384539
a1, a2, ... : sequence of array_like
@@ -4545,10 +4546,25 @@ def concatenate(seq, axis=0, out=None):
45454546
If provided, the destination to place the result. The shape must be
45464547
correct, matching that of what concatenate would have returned if no
45474548
out argument were specified.
4549+
45484550
Returns
45494551
-------
45504552
res : ndarray
45514553
The concatenated array.
4554+
4555+
Examples
4556+
-------
4557+
>>> a = np.array([[1, 2], [3, 4]])
4558+
>>> b = np.array([[5, 6]])
4559+
>>> np.concatenate((a, b), axis = 0)
4560+
>>> array([[1., 2.],
4561+
[3., 4.],
4562+
[5., 6.]])
4563+
>>> np.concatenate((a, b), axis = None)
4564+
>>> array([1., 2., 3., 4., 5., 6.])
4565+
>>> np.concatenate((a, b.T), axis = 1)
4566+
>>> array([[1., 2., 5.],
4567+
[3., 4., 6.]])
45524568
"""
45534569
return _mx_nd_np.concatenate(seq, axis=axis, out=out)
45544570

@@ -6423,8 +6439,9 @@ def einsum(*operands, **kwargs):
64236439

64246440
@set_module('mxnet.numpy')
64256441
def append(arr, values, axis=None):
6426-
"""
6442+
r"""
64276443
Append values to the end of an array.
6444+
64286445
Parameters
64296446
----------
64306447
arr : ndarray
@@ -6437,16 +6454,14 @@ def append(arr, values, axis=None):
64376454
axis : int, optional
64386455
The axis along which `values` are appended. If `axis` is not
64396456
given, both `arr` and `values` are flattened before use.
6457+
64406458
Returns
64416459
-------
64426460
append : ndarray
64436461
A copy of `arr` with `values` appended to `axis`. Note that
64446462
`append` does not occur in-place: a new array is allocated and
64456463
filled. If `axis` is None, `out` is a flattened array.
6446-
See Also
6447-
--------
6448-
insert : Insert elements into an array.
6449-
delete : Delete elements from an array.
6464+
64506465
Examples
64516466
--------
64526467
>>> np.append(np.array([1, 2, 3]), np.array([[4, 5, 6],[7, 8, 9]]))
@@ -6457,9 +6472,4 @@ def append(arr, values, axis=None):
64576472
[4., 5., 6.],
64586473
[7., 8., 9.]])
64596474
"""
6460-
if axis is None:
6461-
arr = arr.flatten()
6462-
values = values.flatten()
6463-
axis = 0
6464-
return concatenate((arr, values), axis=axis)
6465-
6475+
return _mx_nd_np.append(arr, values, axis=axis)

python/mxnet/numpy_dispatch_protocol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def _run_with_array_ufunc_proto(*args, **kwargs):
8585
_NUMPY_ARRAY_FUNCTION_LIST = [
8686
'argmax',
8787
'around',
88+
'append',
8889
'broadcast_arrays',
8990
'broadcast_to',
9091
'clip',

python/mxnet/symbol/numpy/_symbol.py

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming', 'blackman', 'flip',
4141
'around', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take',
4242
'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal',
43-
'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'append']
43+
'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide']
4444

4545

4646
def _num_outputs(sym):
@@ -2995,6 +2995,7 @@ def vsplit(ary, indices_or_sections):
29952995
@set_module('mxnet.symbol.numpy')
29962996
def concatenate(seq, axis=0, out=None):
29972997
"""Join a sequence of arrays along an existing axis.
2998+
29982999
Parameters
29993000
----------
30003001
a1, a2, ... : sequence of array_like
@@ -3007,12 +3008,27 @@ def concatenate(seq, axis=0, out=None):
30073008
If provided, the destination to place the result. The shape must be
30083009
correct, matching that of what concatenate would have returned if no
30093010
out argument were specified.
3011+
30103012
Returns
30113013
-------
30123014
res : ndarray
30133015
The concatenated array.
3016+
3017+
Examples
3018+
-------
3019+
>>> a = np.array([[1, 2], [3, 4]])
3020+
>>> b = np.array([[5, 6]])
3021+
>>> np.concatenate((a, b), axis = 0)
3022+
>>> array([[1., 2.],
3023+
[3., 4.],
3024+
[5., 6.]])
3025+
>>> np.concatenate((a, b), axis = None)
3026+
>>> array([1., 2., 3., 4., 5., 6.])
3027+
>>> np.concatenate((a, b.T), axis = 1)
3028+
>>> array([[1., 2., 5.],
3029+
[3., 4., 6.]])
30143030
"""
3015-
return _npi.concatenate(*seq, dim=axis, out=out)
3031+
return _npi.concatenate(*seq, axis=axis, out=out)
30163032

30173033

30183034
@set_module('mxnet.symbol.numpy')
@@ -4555,47 +4571,4 @@ def einsum(*operands, **kwargs):
45554571
return _npi.einsum(*operands, subscripts=subscripts, out=out, optimize=int(optimize_arg))
45564572

45574573

4558-
@set_module('mxnet.symbol.numpy')
4559-
def append(arr, values, axis=None):
4560-
"""
4561-
Append values to the end of an array.
4562-
Parameters
4563-
----------
4564-
arr : ndarray
4565-
Values are appended to a copy of this array.
4566-
values : ndarray
4567-
These values are appended to a copy of `arr`. It must be of the
4568-
correct shape (the same shape as `arr`, excluding `axis`). If
4569-
`axis` is not specified, `values` can be any shape and will be
4570-
flattened before use.
4571-
axis : int, optional
4572-
The axis along which `values` are appended. If `axis` is not
4573-
given, both `arr` and `values` are flattened before use.
4574-
Returns
4575-
-------
4576-
append : ndarray
4577-
A copy of `arr` with `values` appended to `axis`. Note that
4578-
`append` does not occur in-place: a new array is allocated and
4579-
filled. If `axis` is None, `out` is a flattened array.
4580-
See Also
4581-
--------
4582-
insert : Insert elements into an array.
4583-
delete : Delete elements from an array.
4584-
Examples
4585-
--------
4586-
>>> np.append(np.array([1, 2, 3]), np.array([[4, 5, 6],[7, 8, 9]]))
4587-
array([1., 2., 3., 4., 5., 6., 7., 8., 9.])
4588-
When `axis` is specified, `values` must have the correct shape.
4589-
>>> np.append(np.array([[1, 2, 3], [4, 5, 6]]), np.array([[7, 8, 9]]), axis=0)
4590-
array([[1., 2., 3.],
4591-
[4., 5., 6.],
4592-
[7., 8., 9.]])
4593-
"""
4594-
if axis is None:
4595-
arr = arr.flatten()
4596-
values = values.flatten()
4597-
axis = 0
4598-
return concatenate((arr, values), axis=axis)
4599-
4600-
46014574
_set_np_symbol_class(_Symbol)

0 commit comments

Comments
 (0)