#!/usr/bin/env python # coding: utf-8 # In[1]: # Sorting Arrays # Fast Sorting in NumPy: np.sort and np.argsort import numpy as np a = np.array([1, 9, 0, 7, 3, 5]) sort = np.sort(a) print(sort) # In[3]: # If you want the sorted index then use np.argsort index = np.argsort(a) print(index) # In[11]: # Sorting along rows or columns X = rand.randint(0, 10, (4, 6)) print(X) print("-----------------------------------") # sort each column of X a = np.sort(X, axis=0) print(a) print("-----------------------------------") # sort each row of X b = np.sort(X, axis =1) print(b) # In[13]: # Partial Sorts: Partitioning x = np.array([7, 2, 3, 1, 6, 5, 4]) print(x) print("------------------------------") a = np.partition(x, 3) print(a) # NumPy provides this in the np.partition function. # np.partition takes an array and a number K; the result is a new array with the smallest K values to the left of the partition, # and the remaining values to the right, in arbitrary order: # In[14]: b = np.partition(X, 2, axis=1) print(b)