Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the first N maximum values in the array of numpy

Shulou Source: shulou.com Published: 2022-06-01 12:04:47 10月15日 Update

In this article, the editor introduces in detail "how to obtain the top N maximum values in numpy array" with detailed content, clear steps and proper handling of details. I hope that this article "how to get the top N maximum values in numpy array" can help you solve your doubts.

The main application of the argsort () function, function prototype: numpy.argsort (a, axis=-1, kind='quicksort', order=None)''Returns the indices that would sort an array.Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.'''Parameters: a: array_likeArray to sort. Axis: int or None, optionalAxis along which to sort. The default is-1 (the last axis). If None, the flattened array is used. Kind: {'quicksort',' mergesort', 'heapsort',' stable'}, optionalSorting algorithm. Order: str or list of str, optionalWhen an is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties. Returns: index_array: ndarray, intArray of indices that sort an along the specified axis. If an is one-dimensional, a [index _ array] yields a sorted a. More generally, np.take_along_axis (a, index_array, axis=a) always yields the sorted a, irrespective of dimensionality. Example: import numpy as nptop_k=3arr = np.array ([1,3,2,4,5]) top_k_idx=arr.argsort () [::-1] [0:top_k] print (top_k_idx) # [4 31]

Supplement: python topN / topK takes the maximum N number or the minimum N number

Import numpy as npa = np.array ([1, 4, 3, 3, 5, 2]) b = np.argsort (a) print (b)

Print result [0 4 2 1 3]

It shows that a [0] is the smallest and a [3] is the largest.

A [0]

Tags: Maximum maximum minimum number article three content function index result proper code element prototype idea array new knowledge more step knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple OPPO Reno Shulou Technology vpn MySQL