\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u83b7\u53d6numpy array\u524dN\u4e2a\u6700\u5927\u503c\u7684\u64cd\u4f5c\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u5982\u6709\u9519\u8bef\u6216\u672a\u8003\u8651\u5b8c\u5168\u7684\u5730\u65b9\uff0c\u671b\u4e0d\u541d\u8d50\u6559<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u4e3b\u8981\u5e94\u7528\u4e86argsort()\u51fd\u6570\uff0c\u51fd\u6570\u539f\u578b\uff1a<\/p>\n
\r\nnumpy.argsort(a, axis=-1, kind='quicksort', order=None)\r\n'''\r\nReturns the indices that would sort an array.\r\nPerform 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.\r\n'''\r\nParameters: \r\na : array_like\r\nArray to sort.\r\n \r\naxis : int or None, optional\r\nAxis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.\r\n \r\nkind : {\u2018quicksort', \u2018mergesort', \u2018heapsort', \u2018stable'}, optional\r\nSorting algorithm.\r\n \r\norder : str or list of str, optional\r\nWhen a 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.\r\n \r\nReturns: \r\nindex_array : ndarray, int\r\nArray of indices that sort a along the specified axis. If a 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.<\/pre>\n\u793a\u4f8b\uff1a<\/strong><\/span><\/div>\n\r\nimport numpy as np\r\ntop_k=3\r\narr = np.array([1, 3, 2, 4, 5])\r\ntop_k_idx=arr.argsort()[::-1][0:top_k]\r\nprint(top_k_idx)\r\n#[4 3 1]<\/pre>\n\u8865\u5145\uff1apython topN \/ topK \u53d6 \u6700\u5927\u7684N\u4e2a\u6570 \u6216 \u6700\u5c0f\u7684N\u4e2a\u6570<\/strong><\/span><\/div>\nimport numpy as np\r\na = np.array([1,4,3,5,2])\r\nb = np.argsort(a)\r\nprint(b)\r\nprint\u7ed3\u679c[0 4 2 1 3]<\/pre>\n\u8bf4\u660ea[0]\u6700\u5c0f\uff0ca[3]\u6700\u5927<\/p>\n
\u8865\u5145\uff1a\u5229\u7528Python\u83b7\u53d6\u6570\u7ec4\u6216\u5217\u8868\u4e2d\u6700\u5927\u7684N\u4e2a\u6570\u53ca\u5176\u7d22\u5f15<\/strong><\/span><\/div>\n\r\nimport heapq \r\na=[43,5,65,4,5,8,87]\r\nre1 = heapq.nlargest(3, a) #\u6c42\u6700\u5927\u7684\u4e09\u4e2a\u5143\u7d20\uff0c\u5e76\u6392\u5e8f\r\nre2 = map(a.index, heapq.nlargest(3, a)) #\u6c42\u6700\u5927\u7684\u4e09\u4e2a\u7d22\u5f15 nsmallest\u4e0enlargest\u76f8\u53cd\uff0c\u6c42\u6700\u5c0f\r\nprint(re1)\r\nprint(list(re2)) #\u56e0\u4e3are2\u7531map()\u751f\u6210\u7684\u4e0d\u662flist\uff0c\u76f4\u63a5print\u4e0d\u51fa\u6765\uff0c\u6dfb\u52a0list()\u5c31\u884c\u4e86<\/pre>\n\u7ed3\u679c\uff1a<\/p>\n
re1\uff1a[87, 65, 43]\r\n\r\nre2\uff1a[6, 2, 0]<\/pre>\n\u4ee5\u4e0a\u4e3a\u4e2a\u4eba\u7ecf\u9a8c\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u4e00\u4e2a\u53c2\u8003\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u4e3b\u8981\u5e94\u7528\u4e86argsort()\u51fd\u6570\uff0c\u51fd\u6570\u539f\u578b\uff1a numpy.argsort(a, axis=-1, kind=’ […]<\/p>\n","protected":false},"author":1482,"featured_media":39023,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-218628","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=218628"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218628\/revisions"}],"predecessor-version":[{"id":218635,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218628\/revisions\/218635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/39023"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=218628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=218628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=218628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}