logo

Python의 numpy.sort

어떤 경우에는 계산을 위해 정렬된 배열이 필요합니다. 이를 위해 Python의 numpy 모듈은 다음과 같은 함수를 제공합니다. numpy.sort() . 이 함수는 소스 배열 또는 입력 배열의 정렬된 복사본을 제공합니다.

numpy 정렬

통사론:

 numpy.sort(a, axis=-1, kind='quicksort', order=None) 

매개변수:

x: array_like

이 매개변수는 정렬할 소스 배열을 정의합니다.

축: int 또는 None(선택 사항)

이 매개변수는 정렬이 수행되는 축을 정의합니다. 이 매개변수가 없음 를 사용하면 정렬 전에 배열이 평면화되며 기본적으로 이 매개변수는 -1로 설정되어 마지막 축을 따라 배열이 정렬됩니다.

종류: {quicksort, heapsort, mergesort}(선택 사항)

이 매개변수는 정렬 알고리즘을 정의하는 데 사용되며 기본적으로 정렬은 다음을 사용하여 수행됩니다. '퀵 정렬' .

이진 트리 중위 순회

순서: str 또는 str 목록(선택 사항)

배열이 필드로 정의되면 해당 순서는 첫 번째, 두 번째 등의 비교를 위한 필드를 정의합니다. 단일 필드만 문자열로 지정할 수 있으며 모든 필드에 대해 반드시 지정할 수 있는 것은 아닙니다. 그러나 지정되지 않은 필드는 dtype에 나타나는 순서대로 계속 사용되어 연결을 끊습니다.

보고:

이 함수는 소스 배열과 모양 및 유형이 동일한 소스 배열의 정렬된 복사본을 반환합니다.

예시 1:

 import numpy as np x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]]) x y=np.sort(x) y 

산출:

이중 연결리스트
 array([[ 1, 4, 2, 3], [ 9, 13, 61, 1], [43, 24, 88, 22]]) array([[ 1, 2, 3, 4], [ 1, 9, 13, 61], [22, 24, 43, 88]]) 

위의 코드에서

  • 별칭 이름이 np인 numpy를 가져왔습니다.
  • 다차원 배열을 만들었습니다. '엑스' 사용하여 np.배열() 기능.
  • 우리는 변수를 선언했습니다 '그리고' 반환된 값을 할당했습니다. np.정렬() 기능.
  • 입력 배열을 전달했습니다. '엑스' 기능에서.
  • 마지막으로 우리는 '그리고' .

출력에는 동일한 유형과 모양의 소스 배열의 정렬된 복사본이 표시됩니다.

예 2:

 import numpy as np x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]]) x y=np.sort(x, axis=None) y 

산출:

 array([[ 1, 4, 2, 3], [ 9, 13, 61, 1], [43, 24, 88, 22]]) array([ 1, 1, 2, 3, 4, 9, 13, 22, 24, 43, 61, 88]) 

예시 3:

 import numpy as np x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]]) x y=np.sort(x,axis=0) y z=np.sort(x,axis=1) z 

산출:

 array([[ 1, 4, 2, 1], [ 9, 13, 61, 3], [43, 24, 88, 22]]) array([[ 1, 2, 3, 4], [ 1, 9, 13, 61], [22, 24, 43, 88]]) 

예시 4:

 import numpy as np dtype = [('name', 'S10'), ('height', float), ('age', int),('gender','S10')] values = [('Shubham', 5.9, 23, 'M'), ('Arpita', 5.6, 23, 'F'),('Vaishali', 5.2, 30, 'F')] x=np.array(values, dtype=dtype) x y=np.sort(x, order='age') y z=np.sort(x, order=['age','height']) z 

산출:

 array([(&apos;Shubham&apos;, 5.9, 23, &apos;M&apos;), (&apos;Arpita&apos;, 5.6, 23, &apos;F&apos;), (&apos;Vaishali&apos;, 5.2, 30, &apos;F&apos;)],dtype=[(&apos;name&apos;, &apos;S10&apos;), (&apos;height&apos;, &apos;<f8'), ('age', ' <i4'), ('gender', 's10')]) array([('arpita', 5.6, 23, 'f'), ('shubham', 5.9, 'm'), ('vaishali', 5.2, 30, 'f')], dtype="[(&apos;name&apos;," 's10'), ('height', '<f8'), < pre> <p> <strong>In the above code</strong> </p> <ul> <li>We have imported numpy with alias name np.</li> <li>We have defined the fields and values for the structured array.</li> <li>We have created a structured array <strong>&apos;x&apos;</strong> by passing dtype and values in the <strong>np.array()</strong> function.</li> <li>We have declared the variables <strong>&apos;y&apos;</strong> and <strong>&apos;z&apos;</strong> , and assigned the returned value of <strong>np.sort()</strong> function.</li> <li>We have passed the input array <strong>&apos;x&apos;</strong> and order in the function.</li> <li>Lastly, we tried to print the value of <strong>&apos;y</strong> &apos; and <strong>&apos;z&apos;</strong> .</li> </ul> <p>In the output, it shows a sorted copy of the structured array with a defined order.</p> <hr></f8'),>