Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report