|
34 | 34 | 'log1p', 'rint', 'radians', 'reciprocal', 'square', 'negative', 'fix', 'ceil', 'floor',
|
35 | 35 | 'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot', 'histogram', 'eye',
|
36 | 36 | 'linspace', 'logspace', 'expand_dims', 'tile', 'arange', 'split', 'vsplit', 'concatenate',
|
37 |
| - 'stack', 'vstack', 'dstack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', |
| 37 | + 'stack', 'vstack', 'dstack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'argmin', |
38 | 38 | 'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming', 'blackman', 'flip',
|
39 | 39 | 'around', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take',
|
40 | 40 | 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal',
|
41 |
| - 'hsplit', 'rot90', 'einsum', 'true_divide'] |
| 41 | + 'hsplit', 'rot90', 'einsum', 'true_divide', 'nonzero'] |
42 | 42 |
|
43 | 43 |
|
44 | 44 | @set_module('mxnet.ndarray.numpy')
|
@@ -3165,8 +3165,6 @@ def clip(a, a_min, a_max, out=None):
|
3165 | 3165 | @set_module('mxnet.ndarray.numpy')
|
3166 | 3166 | def argmax(a, axis=None, out=None):
|
3167 | 3167 | r"""
|
3168 |
| - argmax(a, axis=None, out=None) |
3169 |
| -
|
3170 | 3168 | Returns the indices of the maximum values along an axis.
|
3171 | 3169 |
|
3172 | 3170 | Parameters
|
@@ -3234,6 +3232,75 @@ def argmax(a, axis=None, out=None):
|
3234 | 3232 | return _npi.argmax(a, axis=axis, keepdims=False, out=out)
|
3235 | 3233 |
|
3236 | 3234 |
|
| 3235 | +@set_module('mxnet.ndarray.numpy') |
| 3236 | +def argmin(a, axis=None, out=None): |
| 3237 | + r""" |
| 3238 | + Returns the indices of the maximum values along an axis. |
| 3239 | +
|
| 3240 | + Parameters |
| 3241 | + ---------- |
| 3242 | + a : ndarray |
| 3243 | + Input array. Only support ndarrays of dtype `float16`, `float32`, and `float64`. |
| 3244 | + axis : int, optional |
| 3245 | + By default, the index is into the flattened array, otherwise |
| 3246 | + along the specified axis. |
| 3247 | + out : ndarray or None, optional |
| 3248 | + If provided, the result will be inserted into this array. It should |
| 3249 | + be of the appropriate shape and dtype. |
| 3250 | +
|
| 3251 | + Returns |
| 3252 | + ------- |
| 3253 | + index_array : ndarray of indices whose dtype is same as the input ndarray. |
| 3254 | + Array of indices into the array. It has the same shape as `a.shape` |
| 3255 | + with the dimension along `axis` removed. |
| 3256 | +
|
| 3257 | + Notes |
| 3258 | + ----- |
| 3259 | + In case of multiple occurrences of the maximum values, the indices |
| 3260 | + corresponding to the first occurrence are returned. |
| 3261 | +
|
| 3262 | + This function differs from the original `numpy.argmax |
| 3263 | + <https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html>`_ in |
| 3264 | + the following aspects: |
| 3265 | +
|
| 3266 | + - Input type does not support Python native iterables(list, tuple, ...). |
| 3267 | + - Output has dtype that is same as the input ndarray. |
| 3268 | + - ``out`` param: cannot perform auto broadcasting. ``out`` ndarray's shape must be the same as the expected output. |
| 3269 | + - ``out`` param: cannot perform auto type cast. ``out`` ndarray's dtype must be the same as the expected output. |
| 3270 | + - ``out`` param does not support scalar input case. |
| 3271 | +
|
| 3272 | + Examples |
| 3273 | + -------- |
| 3274 | + >>> a = np.arange(6).reshape(2,3) + 10 |
| 3275 | + >>> a |
| 3276 | + array([[10., 11., 12.], |
| 3277 | + [13., 14., 15.]]) |
| 3278 | + >>> np.argmin(a) |
| 3279 | + array(0.) |
| 3280 | + >>> np.argmin(a, axis=0) |
| 3281 | + array([0., 0., 0.]) |
| 3282 | + >>> np.argmin(a, axis=1) |
| 3283 | + array([0., 0.]) |
| 3284 | +
|
| 3285 | + >>> b = np.arange(6) |
| 3286 | + >>> b[2] = 0 |
| 3287 | + >>> b |
| 3288 | + array([0., 1., 0., 3., 4., 5.]) |
| 3289 | + >>> np.argmax(b) # Only the first occurrence is returned. |
| 3290 | + array(0.) |
| 3291 | +
|
| 3292 | + Specify ``out`` ndarray: |
| 3293 | +
|
| 3294 | + >>> a = np.arange(6).reshape(2,3) + 10 |
| 3295 | + >>> b = np.zeros((2,)) |
| 3296 | + >>> np.argmin(a, axis=1, out=b) |
| 3297 | + array([0., 0.]) |
| 3298 | + >>> b |
| 3299 | + array([0., 0.]) |
| 3300 | + """ |
| 3301 | + return _npi.argmin(a, axis=axis, keepdims=False, out=out) |
| 3302 | + |
| 3303 | + |
3237 | 3304 | @set_module('mxnet.ndarray.numpy')
|
3238 | 3305 | def mean(a, axis=None, dtype=None, out=None, keepdims=False): # pylint: disable=arguments-differ
|
3239 | 3306 | """
|
@@ -4761,3 +4828,84 @@ def einsum(*operands, **kwargs):
|
4761 | 4828 | subscripts = operands[0]
|
4762 | 4829 | operands = operands[1:]
|
4763 | 4830 | return _npi.einsum(*operands, subscripts=subscripts, out=out, optimize=int(optimize_arg))
|
| 4831 | + |
| 4832 | + |
| 4833 | +@set_module('mxnet.ndarray.numpy') |
| 4834 | +def nonzero(a): |
| 4835 | + """ |
| 4836 | + Return the indices of the elements that are non-zero. |
| 4837 | +
|
| 4838 | + Returns a tuple of arrays, one for each dimension of `a`, |
| 4839 | + containing the indices of the non-zero elements in that |
| 4840 | + dimension. The values in `a` are always returned in |
| 4841 | + row-major, C-style order. |
| 4842 | +
|
| 4843 | + To group the indices by element, rather than dimension, use `argwhere`, |
| 4844 | + which returns a row for each non-zero element. |
| 4845 | +
|
| 4846 | + Parameters |
| 4847 | + ---------- |
| 4848 | + a : ndarray |
| 4849 | + Input array. |
| 4850 | +
|
| 4851 | + Returns |
| 4852 | + ------- |
| 4853 | + tuple_of_arrays : tuple |
| 4854 | + Indices of elements that are non-zero. |
| 4855 | +
|
| 4856 | + See Also |
| 4857 | + -------- |
| 4858 | + ndarray.nonzero : |
| 4859 | + Equivalent ndarray method. |
| 4860 | +
|
| 4861 | + Notes |
| 4862 | + ----- |
| 4863 | + While the nonzero values can be obtained with ``a[nonzero(a)]``, it is |
| 4864 | + recommended to use ``x[x.astype(bool)]`` or ``x[x != 0]`` instead, which |
| 4865 | + will correctly handle 0-d arrays. |
| 4866 | +
|
| 4867 | + Examples |
| 4868 | + -------- |
| 4869 | + >>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]]) |
| 4870 | + >>> x |
| 4871 | + array([[3, 0, 0], |
| 4872 | + [0, 4, 0], |
| 4873 | + [5, 6, 0]], dtype=int32) |
| 4874 | + >>> np.nonzero(x) |
| 4875 | + (array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64)) |
| 4876 | +
|
| 4877 | + >>> x[np.nonzero(x)] |
| 4878 | + array([3, 4, 5, 6]) |
| 4879 | + >>> np.transpose(np.stack(np.nonzero(x))) |
| 4880 | + array([[0, 0], |
| 4881 | + [1, 1], |
| 4882 | + [2, 0], |
| 4883 | + [2, 1]], dtype=int64) |
| 4884 | +
|
| 4885 | + A common use for ``nonzero`` is to find the indices of an array, where |
| 4886 | + a condition is True. Given an array `a`, the condition `a` > 3 is a |
| 4887 | + boolean array and since False is interpreted as 0, np.nonzero(a > 3) |
| 4888 | + yields the indices of the `a` where the condition is true. |
| 4889 | +
|
| 4890 | + >>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.int32) |
| 4891 | + >>> a > 3 |
| 4892 | + array([[False, False, False], |
| 4893 | + [ True, True, True], |
| 4894 | + [ True, True, True]]) |
| 4895 | + >>> np.nonzero(a > 3) |
| 4896 | + (array([1, 1, 1, 2, 2, 2], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64)) |
| 4897 | +
|
| 4898 | + Using this result to index `a` is equivalent to using the mask directly: |
| 4899 | +
|
| 4900 | + >>> a[np.nonzero(a > 3)] |
| 4901 | + array([4, 5, 6, 7, 8, 9], dtype=int32) |
| 4902 | + >>> a[a > 3] |
| 4903 | + array([4, 5, 6, 7, 8, 9], dtype=int32) |
| 4904 | +
|
| 4905 | + ``nonzero`` can also be called as a method of the array. |
| 4906 | +
|
| 4907 | + >>> (a > 3).nonzero() |
| 4908 | + (array([1, 1, 1, 2, 2, 2], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64)) |
| 4909 | + """ |
| 4910 | + out = _npi.nonzero(a).transpose() |
| 4911 | + return tuple([out[i] for i in range(len(out))]) |
0 commit comments