numpy np.sort()函数(指定对某一轴进行排序,返回数组的排序副本)(成对数组不要用这个排,用哪个啥lexsort()或argsort()都行)
生活随笔
收集整理的這篇文章主要介紹了
numpy np.sort()函数(指定对某一轴进行排序,返回数组的排序副本)(成对数组不要用这个排,用哪个啥lexsort()或argsort()都行)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
指定對哪一層進行排序,如果需排序的是多維數組,特別是那種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_likeArray to be sorted.axis : int or None, optionalAxis 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'}, optionalSorting 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, optionalWhen `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 : ndarrayArray 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.0quicksort 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.0Timsort 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.0NaT 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 axisarray([[1, 4],[1, 3]])>>> np.sort(a, axis=None) # sort the flattened arrayarray([1, 1, 3, 4])>>> np.sort(a, axis=0) # sort along the first axisarray([[1, 1],[3, 4]])(axis=0,就是一排一排縱向比較)Use the `order` keyword to specify a field to use when sorting astructured 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: +SKIParray([('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: +SKIParray([('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 axisa = asanyarray(a).flatten()axis = -1else: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]會發現,本應成對的數組都串了
總結
以上是生活随笔為你收集整理的numpy np.sort()函数(指定对某一轴进行排序,返回数组的排序副本)(成对数组不要用这个排,用哪个啥lexsort()或argsort()都行)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 稳定排序与不稳定排序的定义
- 下一篇: python matplotlib.py