当前位置:网站首页>numpy.sort
numpy.sort
2022-07-26 02:18:00 【Wanderer001】
Reference resources https://cloud.tencent.com/developer/article/1525073
numpy.sort(a, axis=-1, kind=None, order=None)[source]
Return a sorted copy of an array.
Parameters:
a:array_like
Array to be sorted.
axis:int or None, optional
Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.
kind:{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional
Sorting algorithm. The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort or radix sort under the covers and, in general, the actual implementation will vary with data type. The ‘mergesort’ option is retained for backwards compatibility.
Changed in version 1.15.0.: The ‘stable’ option was added.
order:str or list of str, optional
When 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.
Returns
sorted_array:ndarray
Array of the same type and shape as a.
See also
Method to sort an array in-place.
Indirect sort.
Indirect stable sort on multiple keys.
Find elements in a sorted array.
Partial sort.
Notes:
The various sorting algorithms are characterized by their average speed, worst case performance, work space size, and whether they are stable. A stable sort keeps items with the same key in the same relative order. The four algorithms implemented in NumPy have the following properties:
kind | speed | worst case | work space | stable |
|---|---|---|---|---|
‘quicksort’ | 1 | O(n^2) | 0 | no |
‘heapsort’ | 3 | O(n*log(n)) | 0 | no |
‘mergesort’ | 2 | O(n*log(n)) | ~n/2 | yes |
‘timsort’ | 2 | O(n*log(n)) | ~n/2 | yes |
Note
The datatype determines which of ‘mergesort’ or ‘timsort’ is actually used, even if ‘mergesort’ is specified. User selection at a finer scale is not currently available.
All the sort algorithms make temporary copies of the data when sorting along any but the last axis. Consequently, sorting along the last axis is faster and uses less space than sorting along any other axis.
The sort order for complex numbers is lexicographic. If both the real and imaginary parts are non-nan then the order is determined by the real parts except when they are equal, in which case the order is determined by the imaginary parts.
Previous to numpy 1.4.0 sorting real and complex arrays containing nan values led to undefined behaviour. In numpy versions >= 1.4.0 nan values are sorted to the end. The extended sort order is:
Real: [R, nan]
Complex: [R + Rj, R + nanj, nan + Rj, nan + nanj]
where R is a non-nan real value. Complex values with the same nan placements are sorted according to the non-nan part if it exists. Non-nan values are sorted as before.
New in version 1.12.0.
quicksort has been changed to introsort. When sorting does not make enough progress it switches to heapsort. This implementation makes quicksort O(n*log(n)) in the worst case.
‘stable’ automatically chooses the best stable sorting algorithm for the data type being sorted. It, along with ‘mergesort’ is currently mapped to timsort or radix sort depending on the data type. API forward compatibility currently limits the ability to select the implementation and it is hardwired for the different data types.
New in version 1.17.0.
Timsort is added for better performance on already or nearly sorted data. On random data timsort is almost identical to mergesort. It is now used for stable sort while quicksort is still the default sort if none is chosen. For timsort details, refer to CPython listsort.txt. ‘mergesort’ and ‘stable’ are mapped to radix sort for integer data types. Radix sort is an O(n) sort instead of O(n log n).
Examples
>>> a = np.array([[1,4],[3,1]])
>>> np.sort(a) # sort along the last axis
array([[1, 4],
[1, 3]])
>>> np.sort(a, axis=None) # sort the flattened array
array([1, 1, 3, 4])
>>> np.sort(a, axis=0) # sort along the first axis
array([[1, 1],
[3, 4]])Use the order keyword to specify a field to use when sorting a structured array:
>>> dtype = [('name', 'S10'), ('height', float), ('age', int)]
>>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),
... ('Galahad', 1.7, 38)]
>>> a = np.array(values, dtype=dtype) # create a structured array
>>> np.sort(a, order='height')
array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
('Lancelot', 1.8999999999999999, 38)],
dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])Sort by age, then height if ages are equal:
>>> np.sort(a, order=['age', 'height'])
array([('Galahad', 1.7, 38), ('Lancelot', 1.8999999999999999, 38),
('Arthur', 1.8, 41)],
dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])边栏推荐
- [2021] [paper notes] biological effects of cell membrane under infrared and THz - effect is a phenomenon, action is a mechanism - the benefits of THz to medicine
- Dqn pytoch example
- Business Intelligence BI full analysis, explore the essence and development trend of Bi
- Advantages of composition API
- I.MX6UL核心模块使用连载-nand flash读写测试 (三)
- The third question of leetcode 302 weekly Games -- query the number with the smallest k after cutting the number
- 由一个数据增量处理问题看到技术人员的意识差距
- From a data incremental processing problem, we can see the consciousness gap of technicians
- Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)
- Prometheus+blackbox exporter+grafana monitoring server port and URL address
猜你喜欢

Prometheus + process exporter + grafana monitor the resource usage of the process

Advantages of composition API

prometheus+redis-exporter+grafana 监控redis服务

Wechat applet - get user location (longitude and latitude + city)

Ti AM335X工控模块使用beaglebone(bbb)的Debian系统

I.MX6UL核心模块使用连载-nand flash读写测试 (三)

Sqlyog data import and export graphic tutorial

CD from grabbing the track to building a streaming media server -- a case study of "moon in the hometown of sleep"

一款可插拔的AM335X工控模块板载wifi模块

(Dynamic Programming Series) sword finger offer 48. the longest substring without repeated characters
随机推荐
Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
信息系统项目管理师---第十章沟通管理和干系人管理历年考题
How to install opengauss manually (non om mode)
18.删除链表的倒数第n个节点
Bitmap这个“内存刺客”你要小心~
scipy.sparse.vstack
Illustration of the insertion process of b+ tree
Be careful about bitmap, the "memory Assassin"~
19_请求表单与文件
I.MX6UL核心模块使用连载-USB接口测试 (六)
Pytorch的API总览
(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
I.MX6UL核心模块使用连载-CAN、蜂鸣器测试 (十一)
1. Mx6ul core module uses serial EMMC read / write test (IV)
1. Mx6ul core module use serial TF card read / write test (V)
Niuke net question brushing training (I)
[2021] [paper notes] 6G technology vision - otfs modulation technology
LeetCode_动态规划_中等_264.丑数 II
1. Mx6ul core module uses serial NAND FLASH read / write test (III)
scipy.sparse.csr_matrix