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

add np.amin op #17538

Merged
merged 1 commit into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 150 additions & 32 deletions python/mxnet/_numpy_op_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _np_all(a, axis=None, keepdims=False, out=None):

Returns
--------
all : ndarray, bool
all : ndarray, bool
A new boolean or array is returned unless out is specified,
in which case a reference to out is returned.

Expand Down Expand Up @@ -680,7 +680,7 @@ def _np_reshape(a, newshape, order='C', out=None):
def _np_roll(a, shift, axis=None):
"""
Roll array elements along a given axis.

Elements that roll beyond the last position are re-introduced at
the first.

Expand Down Expand Up @@ -841,7 +841,7 @@ def _np_squeeze(a, axis=None, out=None):
def _np_max(a, axis=None, keepdims=False, out=None):
"""
Return the maximum of an array or maximum along an axis.

Parameters
----------
a : ndarray
Expand All @@ -856,14 +856,14 @@ def _np_max(a, axis=None, keepdims=False, out=None):
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original `arr`.

Returns
-------
max : ndarray
max : ndarray
Maximum of `a`. If `axis` is None, the result is an array of dimension 1.
If `axis` is given, the result is an array of dimension
``a.ndim - 1``.

See Also
--------
min :
Expand All @@ -872,28 +872,28 @@ def _np_max(a, axis=None, keepdims=False, out=None):
Element-wise maximum of two arrays, ignoring any nan.
argmax :
Return the indices of the maximum values.

Notes
-----
NaN in the orginal `numpy` is denoted as nan and will be ignored.

Don't use `max` for element-wise comparison of 2 arrays; when
``a.shape[0]`` is 2, ``maximum(a[0], a[1])`` is faster than
``max(a, axis=0)``.

Examples
--------
>>> a = np.arange(4).reshape((2,2))
>>> a
array([[0., 1.],
[2., 3.]])
[2., 3.]])
>>> np.max(a) # Maximum of the flattened array
array(3.)
>>> np.max(a, axis=0) # Maxima along the first axis
array([2., 3.])
>>> np.max(a, axis=1) # Maxima along the second axis
array([1., 3.])

>>> b = np.arange(5, dtype=np.float32)
>>> b[2] = np.nan
>>> np.max(b)
Expand All @@ -904,15 +904,72 @@ def _np_max(a, axis=None, keepdims=False, out=None):

def _np_amax(a, axis=None, keepdims=False, out=None):
"""
Refer to _np_max
Return the maximum of an array or maximum along an axis.

Parameters
----------
a : ndarray
Input data.
axis : int, optional
Axis along which to operate. By default, flattened input is used.
out : ndarray, optional
Alternative output array in which to place the result. Must
be of the same shape and buffer length as the expected output.
See `doc.ufuncs` (Section "Output arguments") for more details.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original `arr`.

Returns
-------
amax : ndarray
Maximum of `a`. If `axis` is None, the result is an array of dimension 1.
If `axis` is given, the result is an array of dimension
``a.ndim - 1``.

See Also
--------
min :
The minimum value of an array along a given axis, ignoring any nan.
maximum :
Element-wise maximum of two arrays, ignoring any nan.
argmax :
Return the indices of the maximum values.

Notes
-----
NaN in the orginal `numpy` is denoted as nan and will be ignored.

Don't use `amax` for element-wise comparison of 2 arrays; when
``a.shape[0]`` is 2, ``maximum(a[0], a[1])`` is faster than
``amax(a, axis=0)``.

Examples
--------
>>> a = np.arange(4).reshape((2,2))
>>> a
array([[0., 1.],
[2., 3.]])
>>> np.amax(a) # Maximum of the flattened array
array(3.)
>>> np.amax(a, axis=0) # Maxima along the first axis
array([2., 3.])
>>> np.amax(a, axis=1) # Maxima along the second axis
array([1., 3.])

>>> b = np.arange(5, dtype=np.float32)
>>> b[2] = np.nan
>>> np.amax(b)
array(4.)
"""
pass


def _np_min(a, axis=None, keepdims=False, out=None):
"""
Return the minimum of an array or minimum along an axis.

Parameters
----------
a : ndarray
Expand All @@ -927,25 +984,25 @@ def _np_min(a, axis=None, keepdims=False, out=None):
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original `arr`.

Returns
-------
min : ndarray
Minimum of `a`. If `axis` is None, the result is an array of dimension 1.
If `axis` is given, the result is an array of dimension
``a.ndim - 1``.

See Also
--------
max :
The maximum value of an array along a given axis, ignoring any nan.
minimum :
Element-wise minimum of two arrays, ignoring any nan.

Notes
-----
NaN in the orginal `numpy` is denoted as nan and will be ignored.

Don't use `min` for element-wise comparison of 2 arrays; when
``a.shape[0]`` is 2, ``minimum(a[0], a[1])`` is faster than
``min(a, axis=0)``.
Expand All @@ -966,14 +1023,75 @@ def _np_min(a, axis=None, keepdims=False, out=None):
>>> b[2] = np.nan
>>> np.min(b)
array(0.) # nan will be ignored
"""
"""
pass


def _np_amin(a, axis=None, keepdims=False, out=None):
"""
Return the minimum of an array or minimum along an axis.

Parameters
----------
a : ndarray
Input data.
axis : int, optional
Axis along which to operate. By default, flattened input is used.
out : ndarray, optional
Alternative output array in which to place the result. Must
be of the same shape and buffer length as the expected output.
See `doc.ufuncs` (Section "Output arguments") for more details.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original `arr`.

Returns
-------
amin : ndarray
Minimum of `a`. If `axis` is None, the result is an array of dimension 1.
If `axis` is given, the result is an array of dimension
``a.ndim - 1``.

See Also
--------
max :
The maximum value of an array along a given axis, ignoring any nan.
minimum :
Element-wise minimum of two arrays, ignoring any nan.

Notes
-----
NaN in the orginal `numpy` is denoted as nan and will be ignored.

Don't use `amin` for element-wise comparison of 2 arrays; when
``a.shape[0]`` is 2, ``minimum(a[0], a[1])`` is faster than
``amin(a, axis=0)``.

Examples
--------
>>> a = np.arange(4).reshape((2,2))
>>> a
array([[0., 1.],
[2., 3.]])
>>> np.amin(a) # Minimum of the flattened array
array(0.)
>>> np.amin(a, axis=0) # Minima along the first axis
array([0., 1.])
>>> np.amin(a, axis=1) # Minima along the second axis
array([0., 2.])
>>> b = np.arange(5, dtype=np.float32)
>>> b[2] = np.nan
>>> np.amin(b)
array(0.) # nan will be ignored
"""
pass


def _np_prod(a, axis=None, dtype=None, out=None, keepdims=False):
"""
Return the product of array elements over a given axis.

Parameters
----------
a : ndarray
Expand All @@ -999,53 +1117,53 @@ def _np_prod(a, axis=None, dtype=None, out=None, keepdims=False):
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original `arr`.

Returns
-------
product_along_axis : ndarray, see `dtype` parameter above.
An array shaped as `a` but with the specified axis removed.
Returns a reference to `out` if specified.

See Also
--------
ndarray.prod : equivalent method

Notes
-----
Arithmetic is modular when using integer types, and no error is
raised on overflow. That means that, on a 32-bit platform:

>>> x = np.array([536870910, 536870910, 536870910, 536870910])
>>> np.prod(x) #random
array(8.307675e+34)

Examples
--------
By default, calculate the product of all elements:

>>> np.prod(np.array([1.,2.]))
array(2.)

Even when the input array is two-dimensional:

>>> np.prod(np.array([1.,2.,3.,4.]).reshape((2,2)))
array(24.)

But we can also specify the axis over which to multiply:

>>> np.prod(np.array([1.,2.,3.,4.]).reshape((2,2)), axis=1)
array([ 2., 12.])

If the type of `x` is unsigned, then the output type is
the unsigned platform integer:

>>> x = np.array([1, 2, 3], dtype=np.uint8)
>>> np.prod(x).dtype == np.uint8
True

If `x` is of a signed integer type, then the output type
is the default platform integer:

>>> x = np.array([1, 2, 3], dtype=np.int8)
>>> np.prod(x).dtype == np.int8
True
Expand Down Expand Up @@ -1139,7 +1257,7 @@ def _npx_constraint_check(x, msg):
In order to evaluate this operator, one should multiply the origin tensor by the return value
of this operator to force this operator become part of the computation graph,
otherwise the check would not be working under symoblic mode.

Parameters
----------
x : ndarray
Expand Down
1 change: 1 addition & 0 deletions python/mxnet/numpy_dispatch_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def _run_with_array_ufunc_proto(*args, **kwargs):
'amax',
'mean',
'min',
'amin',
'nonzero',
'ones_like',
'atleast_1d',
Expand Down
1 change: 1 addition & 0 deletions src/operator/numpy/np_broadcast_reduce_op_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ NNVM_REGISTER_OP(_backward_np_max)
.set_attr<FCompute>("FCompute<cpu>", NumpyReduceAxesNoDTypeBackward<cpu, mshadow_op::eq>);

NNVM_REGISTER_OP(_np_min)
.add_alias("_np_amin")
.describe(R"code()code" ADD_FILELINE)
.set_num_inputs(1)
.set_num_outputs(1)
Expand Down
Loading