NumPy Sorting and SearchingNumpy provides a variety of functions for sorting and searching. There are various sorting algorithms like quicksort, merge sort and heapsort which is implemented using the numpy.sort() function. The kind of the sorting algorithm to be used in the sort operation must be mentioned in the function call. Let's discuss the sorting algorithm which is implemented in numpy.sort()
The syntax to use the numpy.sort() function is given below. It accepts the following parameters.
Consider the following example. ExampleOutput: Sorting along the columns: [[ 2 3 10] [ 4 5 6] [ 7 8 9]] Sorting along the rows: [[ 4 2 3] [ 7 5 6] [10 8 9]] Sorting data ordered by name [(b'John', 251) (b'Mukesh', 200)] numpy.argsort() functionThis function is used to perform an indirect sort on an input array that is, it returns an array of indices of data which is used to construct the array of sorted data. Consider the following example. ExampleOutput: Original array: [90 29 89 12] Printing indices of sorted data [3 1 2 0] printing sorted array 12 29 89 90 numpy.lexsort() functionThis function is used to sort the array using the sequence of keys indirectly. This function performs similarly to the numpy.argsort() which returns the array of indices of sorted data. Consider the following example. ExampleOutput: printing indices of sorted data [0 3 1 4 2] using the indices to sort the array a 12 d 12 b 90 e 211 c 380 numpy.nonzero() functionThis function is used to find the location of the non-zero elements from the array. Consider the following example. ExampleOutput: printing original array [ 12 90 380 12 211] printing location of the non-zero elements (array([0, 1, 2, 3, 4]),) numpy.where() functionThis function is used to return the indices of all the elements which satisfies a particular condition. Consider the following example. ExampleOutput: (array([1, 2, 4]),) (array([0, 1, 1]), array([1, 0, 1])) Next TopicNumPy Copies and Views |