Some sorting methods of numpy array are discussed
- 2020-06-19 10:34:42
- OfStack
A simple introduction
NumPy is an open source array computation extension of Python. This tool can be used to store and process large matrices much more efficiently than Python's own nested list structure (nested list structure), which can also be used to represent matrices (matrix).
Create an array
Create a 1-dimensional array:
data = np.array([1,3,4,8])
View array dimensions
data.shape
View array types
data.dtype
Gets or modifies array elements by index
data[1] 获取元素
data[1] = 'a' 修改元素
Create a 2-dimensional array
data = np.array([[1,2,3],[4,5,6]])
Both elements are lists
<
br
>
data = ES45en.arange (10) like python's range1, range returns a list and arange returns an array of type array
<
br
>
3.data2 = data.reshape (2,5) returns a 2*5 array. It is not a copy of the array but a reference
Creating a special array
data = np.zeros((2,2)) 创建2*2全为0的2维数组
data = np.ones((2,3,3,)) 创建全为1的3维数组
data = np.eye(4) 创建4*4的对角数组,对角元素为1,其它都为0
An array of conversion
data = np.arange(16).reshape(4,4) 将0-16的移位数组转换为4*4的数组
The sorting way
Description: often need to sort array or list, python provides several sort functions, the following characteristics;
2-dimensional array a:
1 4
3 1
1.
ndarray.sort(axis=-1,kind='quicksort',order=None)
Usage: ES86en. sort
Parameter description:
axis: Sort along the direction of the array, 0 for rows, 1 for columns
kind: Sorting algorithm, provides fast sorting, mixed sorting, heap sorting
order: Not the order. I will analyze this later
Effect: sort the array a, then change a
Such as:
>>a.sort(axis=1)
>>print a
1 4
1 3
2,
numpy.sort(a,axis=-1,kind='quicksort',order=None)
Usage:
numpy.sort(a)
Parameter description:
a: Array to sort, other same as 1
Effect: sort the array a, returning 1 sorted array (same dimension as a), a unchanged
Such as:
>>print numpy.sort(a,axis=1)
1 4
1 3
>>print a
1 4
3 1
3, numpy. argsort (a, axis = 1, kind = 'quicksort', order = None)
Usage: ES138en. argsort(a)
Parameter description: same as 2
Effect: sort the array a, returning 1 sorted index, a unchanged
Such as:
>>print numpy.argsort(a,axis=1)
0 1
1 0
4,
data.shape
0
Built-in sort function for list, dictionaries, etc
iterable: Iterative type;
cmp: Function for comparison, what to compare is determined by key, with default value, 1 item in iteration set;
key: Use an attribute and function of the list element as the key, with default value, and iterate over 1 item in the collection;
reverse: Collation.reverse=True or reverse=False, default False (from small to large).
Return value: is a sorted iteratable type, like iterable1;
b, for example, is a dictionary
b:
{'a':2,'c':1,'b':3}
Sort b:
>>c=sorted(b.iteritems(),key=operator.itemgetter(1),reverse=False)
>>print c[('c', 1), ('a', 2), ('b', 3)]
You can see: 1 list is returned
conclusion
The above is all about the sorting methods of numpy array in this paper, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!