1. Sort function
Sort function |
illustrate |
np.sort( ndarray) |
sort, return a copy |
np.unique(ndarray) |
Returns the elements in the ndarray, excluding duplicate elements, and sorted |
np.intersect1d( ndarray1, ndarray2) e.g.union1d (ndarray1, ndarray2) e.g. setdiff1d (ndarray1, ndarray2) e.g. setxor1d (ndarray1, ndarray2) |
Returns the intersection of the two and sorts. Returns the union of the two and sorts. Returns the difference between the two. Returns the symmetric difference between the two |
2. Example of the use of the function
np.sort
See also the official manual: https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html
np.sort(a, axis=-1, kind='quicksort', order=None)
# -*- coding: utf-8 -*- """ @author: tom Talk is cheap, show me the code Aim: numpy sort function example """ import numpy as np #sort, return copy #np.sort(ndarray, axis=-1, kind='quicksort', order=None) a = np.array([[1,9,2], [7,5,6], [4,8,3]]) The axis where the single element element in #a is located is 0, such as 1, 7, 4 and 9, 5, 8, and 2, 6, 3 are all on axis=0 The axis of the innermost array in #a is all 1, such as [1,9,2] and [7,5,6] and [4,8,3] are all on axis=1 #Description: a is a 2-dimensional array, the maximum axis is 1, and the minimum axis is 0 ret = np.sort(a) #sort along the last axis ''' [[1 2 9] [5 6 7] [3 4 8]] ''' ret = np.sort(a, axis=0) #sort along the first axis ''' [[1 5 2] [4 8 3] [7 9 6]] ''' ret = np.sort(a, axis=None) #sort the flattened array ''' [1 2 3 4 5 6 7 8 9] ''' # Sort by a property in a given element sampleType = [('name', 'S10'), ('height', float), ('age', int)] #custom element structure samples = [('Tom', 1.8, 36), ('Kitty', 1.9, 35), ('John', 1.9, 39), ('Anna', 1.7, 32)] a = np.array(samples, dtype=sampleType) ret = np.sort(a, order='height') #sort by height ''' [(b'Anna', 1.7, 32) (b'Tom', 1.8, 36) (b'John', 1.9, 39) (b'Kitty', 1.9, 35)] ''' ret = np.sort(a, order=['height','age']) #Sort by height, and then sort by age when the heights are equal ''' [(b'Anna', 1.7, 32) (b'Tom', 1.8, 36) (b'Kitty', 1.9, 35) (b'John', 1.9, 39)] ''' print(id(a),id(ret)) #184520576 184520416
np.unique
#Return the elements in the ndarray, excluding duplicate elements, and sort #np.unique(ar, return_index=False, return_inverse=False, return_counts=False) ret = np.unique([1, 1, 2, 2, 3, 3]) #[1 2 3] ret = np.unique([3, 3, 2, 2, 1, 1]) #[1 2 3] ret = np.unique([[1,1],[2,2],[3,3]]) #[1 2 3] ret = np.unique([[3,3],[2,2],[1,1]]) #[1 2 3] a = np.array([1, 1, 2, 2, 3, 3]) u,indices = np.unique(a, return_index=True)#u=a[indices] print (u) # [1 2 3] print(indices) #[0 2 4] print(a[indices]) #[1 2 3] u,indices = np.unique([1, 1, 2, 2, 3, 3], return_inverse=True)#a=u[indices] print (u) # [1 2 3] print(indices) #[0 0 1 1 2 2] print (u [indices]) # [1 1 2 2 3 3] = a
np.intersect1d
#Return the intersection of the two and sort. #np.intersect1d( ndarray1, ndarray2, assume_unique=False) a = np.array([1,2,3,4,5]) b = np.array([4,5,6,7,8]) c = np.array([4,5,6,7,8,9,10,11]) d = np.array([[4,5,6,7],[8,9,10,11]]) ret = np.intersect1d(a, b)#[4 5] ret = np.intersect1d(a, c)#[4 5] ret = np.intersect1d(a, d)#[4 5]
e.g.union1d
#Return the union of two sequences and sort. # e.g.union1d (ndarray1, ndarray2) a = [0, 1, 2] b = [1, 2, 3] c = [3, 4, 5] ret = np.union1d(a, b) #[0 1 2 3] ret = np.union1d(b, c) #[1 2 3 4 5] #Return the union of multiple sequences and sort, you can use the functools.reduce function at the same time from functools import reduce ret = reduce(np.union1d, (a, b, c)) #[0 1 2 3 4 5]
e.g. setdiff1d
#Return the difference between the two. # e.g. setdiff1d (ndarray1, ndarray2) #Return the sorted, unique values in ar1 that are not in ar2. a = np.array([1,2,3,4,5]) b = np.array([4,5,6,7,8]) c = np.array([[4,5,6,7,8],[9,10,11,12,13]]) ret = np.setdiff1d (a, b) # [1 2 3] ret = np.setdiff1d (a, c) # [1 2 3]
e.g. setxor1d
#Return the symmetric difference between the two (unique & sorted of ar1 and ar2's own unique set of elements) #Find the set exclusive-or of two arrays. #Return the sorted, unique values that are in only one (not both) of the input arrays. # e.g. setxor1d (ndarray1, ndarray2) a = np.array([1,2,3,4,5]) b = np.array([4,5,6,7,8]) ret = np.setxor1d (a, b) # [1 2 3 6 7 8]
(end)