Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.sort

numpy.sort(a,axis=-1,kind='quicksort',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 beforesorting. The default is -1, which sorts along the last axis.

kind:{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional

Sorting algorithm. Default is ‘quicksort’.

order:str or list of str, optional

Whena is an array with fields defined, this argument specifieswhich fields to compare first, second, etc. A single field canbe specified as a string, and not all fields need be specified,but unspecified fields will still be used, in the order in whichthey come up in the dtype, to break ties.

Returns:
sorted_array:ndarray

Array of the same type and shape asa.

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. Astable sort keeps items with the same key in the same relativeorder. The three available algorithms have the followingproperties:

kindspeedworst casework spacestable
‘quicksort’1O(n^2)0no
‘mergesort’2O(n*log(n))~n/2yes
‘heapsort’3O(n*log(n))0no

All the sort algorithms make temporary copies of the data whensorting along any but the last axis. Consequently, sorting alongthe last axis is faster and uses less space than sorting alongany other axis.

The sort order for complex numbers is lexicographic. If both the realand imaginary parts are non-nan then the order is determined by thereal parts except when they are equal, in which case the order isdetermined by the imaginary parts.

Previous to numpy 1.4.0 sorting real and complex arrays containing nanvalues led to undefined behaviour. In numpy versions >= 1.4.0 nanvalues 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 nanplacements 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 an introsort which will switchheapsort when it does not make enough progress. This makes itsworst case O(n*log(n)).

‘stable’ automatically choses the best stable sorting algorithmfor the data type being sorted. It is currently mapped tomerge sort.

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]])

Use theorder 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')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')])

Previous topic

Sorting, searching, and counting

Next topic

numpy.lexsort

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp