指定对哪一层进行排序,如果需排序的是多维数组,特别是那种np.sort()貌似不太友好
from numpy\core\fromnumeric.py
@array_function_dispatch(_sort_dispatcher)
def sort(a, axis=-1, kind=None, order=None):
"""
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.
要排序的轴。 如果为None,则在排序之前将数组展平。 默认值为-1,它沿着最后一个轴排序。
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.
排序算法。 默认值为“快速排序”。 请注意,“稳定”和“合并排序”在后台都使用timsort或基数排序,并且通常,实际实现会随数据类型而变化。 保留'mergesort'选项是为了向后兼容。
.. versionchanged:: 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.
当“ a”是定义了字段的数组时,此参数指定要比较的字段的第一个,第二个等。单个字段可以指定为字符串,并且不需要指定所有字段,但仍将使用未指定的字段, 按照他们在dtype中出现的顺序,打破关系。
Returns
-------
sorted_array : ndarray
Array of the same type and shape as `a`.
类型和形状与“ a”相同的数组。
See Also
--------
ndarray.sort : Method to sort an array in-place.
就地排序数组的方法。
argsort : Indirect sort.
间接排序。
lexsort : Indirect stable sort on multiple keys.
对多个键进行间接稳定排序。
searchsorted : Find elements in a sorted array.
查找排序数组中的元素。
partition : 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:
各种分类算法的特征在于它们的平均速度,最坏情况下的性能,工作空间大小以及它们是否稳定。 稳定的排序使具有相同键的项以相同的相对顺序保持。 NumPy中实现的四种算法具有以下属性:
=========== ======= ============= ============ ========
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.
即使指定了“ mergesort”,数据类型也会确定实际使用的是“ mergesort”还是“ timsort”。 目前尚无法进行更精细的用户选择。
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.
复数的排序顺序为字典顺序。 如果实部和虚部都不是NAN,则顺序由实部确定,除非它们相等,在这种情况下,顺序由虚部确定。
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:
在numpy 1.4.0之前,对包含nan值的实数和复杂数组进行排序会导致未定义的行为。 在numpy版本中,> = 1.4.0会将nan值排序到末尾。 扩展的排序顺序为:
* 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.
其中R是非南实数值。 具有相同nan位置的复杂值(如果存在)将根据非nan部分进行排序。
Non-nan values are sorted as before.
非Nan值的排序方式与以前相同。
.. versionadded:: 1.12.0
quicksort has been changed to `introsort <https://en.wikipedia.org/wiki/Introsort>`_.
When sorting does not make enough progress it switches to `heapsort <https://en.wikipedia.org/wiki/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 <https://en.wikipedia.org/wiki/Timsort>`_ or `radix sort <https://en.wikipedia.org/wiki/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.
quicksort已更改为`introsort <https://en.wikipedia.org/wiki/Introsort>`_。
如果排序没有取得足够的进展,它将切换到“堆排序<https://en.wikipedia.org/wiki/Heapsort>`_”。
在最坏的情况下,此实现使快速排序O(n * log(n))成为可能。
“稳定”会自动为排序的数据类型选择最佳的稳定排序算法。
目前,它与“ mergesort”一起被映射到“ timsort <https://en.wikipedia.org/wiki/Timsort>” _或“基数排序<https://en.wikipedia.org/wiki/Radix_sort>” _取决于数据类型。
API前向兼容性当前限制了选择实现的能力,并且对不同数据类型进行了硬连线。
.. versionadded:: 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 <https://github.com/python/cpython/blob/3.7/Objects/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).
添加Timsort可以提高对已分类或已分类数据的性能。 在随机数据上,timsort与mergesort几乎相同。 现在,它用于稳定排序,而如果未选择,则快速排序仍是默认排序。 有关音符的详细信息,请参考`CPython listsort.txt <https://github.com/python/cpython/blob/3.7/Objects/listsort.txt>`_。 'mergesort'和'stable'映射为整数数据类型的基数排序。 基数排序是O(n)排序,而不是O(n log n)。
.. versionchanged:: 1.17.0
NaT now sorts to the end of arrays for consistency with NaN.
NaT现在会排序到数组的末尾,以与NaN保持一致。
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]])
(axis=0,就是一排一排纵向比较)
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') # doctest: +SKIP
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']) # doctest: +SKIP
array([('Galahad', 1.7, 38), ('Lancelot', 1.8999999999999999, 38),
('Arthur', 1.8, 41)],
dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
"""
if axis is None:
# flatten returns (1, N) for np.matrix, so always use the last axis
a = asanyarray(a).flatten()
axis = -1
else:
a = asanyarray(a).copy(order="K")
a.sort(axis=axis, kind=kind, order=order)
return a
示例
在进行成对数组想根据某个性质的排序时,不要使用这个np.sort()函数,如:
import numpy as np
# key为时间,value为(光照度,平均识别个数)
keyword = {'11:30.0': (50000, 13.96), '12:16.0': (54500, 13.20), '13:15.0': (47500, 12.48),
'14:22.0': (55450, 12.44), '14:35.0': (55430, 13.72), '17:03.0': (13990, 11.00),
'17:38.0': (9058, 11.60), '17:57.0': (5044, 12.46), '18:20.0': (1300, 13.80),
'18:25.0': (900, 13.90), '18:28.0': (700, 13.96), '18:31.0': (570, 13.90),
'18:33.0': (500, 13.94), '18:34.0': (450, 13.9), '18:35.0': (440, 13.88),
'18:36.0': (360, 13.60), '18:37.0': (300, 13.8), '18:39.0': (250, 13.4),
'18:40.0': (200, 13.34),
'18:42.0': (150, 13.10), '18:44.0': (100, 11.80), '18:44.2': (90, 11.34),
'18.44.4': (80, 11.38), '18:44.8': (70, 9.50), '18:45.0': (60, 9.20),
'18:46.0': (50, 11.9), '18:46.3': (40, 10.8), '18:46.6': (30, 9.20),
'18:49.0': (20, 9.70), '18:49.6': (15, 6.90), '18:50.3': (13, 4.70),
'18:50.9': (12, 3.80), '18:51.5': (11, 2.60), '18:52.2': (10, 1.70),
'18:52.9': (9, 1.00), '18:53.6': (8, 0.2), '18:54.3': (7, 0.06),
'18:55.0': (6, 0.02)}
data = []
for key in keyword:
data.append(keyword[key])
data = np.array(data)
data_sort=np.sort(data,axis=0)
for i in range(len(data)):
print(data[i],' ',data_sort[i])
结果:
[5.000e+04 1.396e+01] [6. 0.02]
[5.45e+04 1.32e+01] [7. 0.06]
[4.750e+04 1.248e+01] [8. 0.2]
[5.545e+04 1.244e+01] [9. 1.]
[5.543e+04 1.372e+01] [10. 1.7]
[1.399e+04 1.100e+01] [11. 2.6]
[9058. 11.6] [12. 3.8]
[5044. 12.46] [13. 4.7]
[1300. 13.8] [15. 6.9]
[900. 13.9] [20. 9.2]
[700. 13.96] [30. 9.2]
[570. 13.9] [40. 9.5]
[500. 13.94] [50. 9.7]
[450. 13.9] [60. 10.8]
[440. 13.88] [70. 11.]
[360. 13.6] [80. 11.34]
[300. 13.8] [90. 11.38]
[250. 13.4] [100. 11.6]
[200. 13.34] [150. 11.8]
[150. 13.1] [200. 11.9]
[100. 11.8] [250. 12.44]
[90. 11.34] [300. 12.46]
[80. 11.38] [360. 12.48]
[70. 9.5] [440. 13.1]
[60. 9.2] [450. 13.2]
[50. 11.9] [500. 13.34]
[40. 10.8] [570. 13.4]
[30. 9.2] [700. 13.6]
[20. 9.7] [900. 13.72]
[15. 6.9] [1300. 13.8]
[13. 4.7] [5044. 13.8]
[12. 3.8] [9058. 13.88]
[11. 2.6] [1.399e+04 1.390e+01]
[10. 1.7] [4.75e+04 1.39e+01]
[9. 1.] [5.00e+04 1.39e+01]
[8. 0.2] [5.450e+04 1.394e+01]
[7. 0.06] [5.543e+04 1.396e+01]
[6. 0.02] [5.545e+04 1.396e+01]
会发现,本应成对的数组都串了
来源:CSDN
作者:Dontla
链接:https://blog.csdn.net/Dontla/article/details/104479934