NumPy sorting, searching, and counting functions

Source: Internet
Author: User

These sorting functions implement different sorting algorithms, and each sorting algorithm is characterized by execution speed, worst case performance, required workspace, and algorithm stability. The following table shows a comparison of three sorting algorithms.

type Speed Worst Case Scenario working Space Stability
‘quicksort‘(Quick Sort) 1 O(n^2) 0 Whether
‘mergesort‘(merge Sort) 2 O(n*log(n)) ~n/2 Is
‘heapsort‘(Heap sort) 3 O(n*log(n)) 0 Whether

(1) Np.sort ()--function returns a sorted copy of the input array

A=np.array ([[[1,8],[9,7]])print (a)print('----------')  print (np.sort (a))print('-----------')  )print(Np.sort (a,axis=0))

Output:

[[1 8] [9 7]]----------[[1 8] [7 9]]-----------[[1 7] [ 9 8]]

In the sort order that contains the string:

ImportNumPy as NP DT= Np.dtype ([('name','U5'),(' Age', int)]) A= Np.array ([('Raju', 21), ('Anil', 25), ('Ravi', 17), ('Amar', +)], Dtype =DT)Print(a)Print('---------------------')  Print(Np.sort (A, order =' Age'))

Output:

[('Raju', 21) ('Anil', 25) ('Ravi', 17) ('Amar', 27)]---------------------[('Ravi', 17) ('Raju', 21) ('Anil', 25) ('Amar', 27)]

(2) the Np.argsort ()-----function performs an indirect sort on the input array along a given axis and returns an indexed array of data using the specified sort type. This indexed array is used to construct the sorted array. That is, the index in the original array where the element is to be sorted.

x = Np.array ([3,  1,  2= np.argsort (x)  print  (y)print (X[y])  for inch y:       Print (X[i])

Output:

[1 2 0][1 2 3]123

(3) Np.lexsort ()--functions use key sequences to perform indirect ordering. Keys can be thought of as a column in a spreadsheet. The function returns an indexed array that can be used to get the sorted data. Note that the last key happens to be the primary key of sort.

NM = ('Raju','Anil','Ravi','Amar') DV=  ('a.y.','f.y.','c.y.','b.y.') Ind=Np.lexsort ((dv,nm))Print(Ind)Print([Nm[i] +", "+ Dv[i] forIinchIND])

Output:

[3 1 0 2] ['Amar, B.y. ' ' Anil, F.y. ' ' Raju, A.y. ' ' Ravi, C.y. ']

(4) The NumPy module has some functions for searching within an array. Provides functions for finding the maximum, minimum, and element that satisfies a given condition.

Np.argmin () and Np.argmax ()-----These two functions return the index of the smallest and largest elements, respectively, along a given axis.

(5) Np.nonzero ()--function returns the index of the non-0 element in the input array.

A = Np.array ([[30,40,0],[0,20,10],[50,0,60print(a)print(Np.nonzero (a))

Output:

[[  0] [  1, 1, 2, 2]), array ([0, 1, 1, 2, 0, 2]))

(6) Np.where ()----function returns the index of the element in the input array that satisfies the given condition, first axis=0, in Axis=1

x = Np.arange (9.). Reshape (3,  3)   print= np.where (x >  3)  print   Print  (' Use these indexes to get the element that satisfies the criteria:'  )Print (X[y])

Output:

[0.  1.  23.  4.  56.  7.  8.] (Array ([1, 1, 2, 2, 2]), array ([1, 2, 0, 1, 24.  ) 5.  6.  7.  8.]

(7) Np.extract ()----function returns the element that satisfies any condition, mod () is the remainder function

x = Np.arange (9.). Reshape (3,  3)    print#condition = np.mod (x,2)  = =   0  print  (' by element's condition value:'  )Print  (condition)print  (' extract element using condition:'  )  Print (np.extract (condition, x))

Output:

[0.  1.  23.  4.  56.  7.  8.] By element's condition value: [[True false  true] [false  true] [true false  true]] [use condition to extract element: [0  ]. 2.  4.  6.  8.]

NumPy sorting, searching, and counting functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.