A brief discussion on several sorting methods of numpy array_python

韦小宝
Release: 2017-12-16 13:32:49
Original
3253 people have browsed it

This article mainly introduces several sorting methods of numpy arrays, including a brief introduction to numpy and how to create arrays. It has certain reference value. Friends who are interested in numpy can refer to it.

Brief introduction

The NumPy system is an open source array calculation extension for Python. This tool can be used to store and process large matrices much more efficiently than Python's own nested list structure (which can also be used to represent matrices).

Create an array

Create a 1-dimensional array:

data = np.array([1,3,4,8])  

View array dimensions

data.shape

View array type

data.dtype

Get or modify array elements by index

data[1] Get elements<br>data[1] = 'a' Modify Element 

Create a two-dimensional array

data = np.array([[1,2,3],[4, 5,6]]) Both elements are lists
2.data = np.arange(10) Like python’s range, range returns a list, and arange returns an array of array type
3.data2 = data.reshape(2,5) returns a 2*5 array. It does not copy the array but a reference. It just returns a different view of the array. If data changes, data2 will also change.

Create a special array

data = np.zeros((2,2)) Create a 2*2 2-dimensional array with all 0s<br>data = np.ones(( 2,3,3,)) Create a three-dimensional array with all 1s<br>data = np.eye(4) Create a 4*4 diagonal array, with diagonal elements being 1 and others being 0<br>

Array conversion

data = np.arange(16).reshape(4,4) Convert the shifted array of 0-16 to 4 *Array of 4

Sort method

Note: It is often necessary to sort arrays or lists, and python provides several Sorting function, the following describes the characteristics;

Two-dimensional array a:

1 4
3 1
Copy after login

1, ndarray.sort(axis= -1,kind='quicksort',order=None)

Usage method: a.sort

Parameter description:

axis: sort along the array Direction, 0 means by row, 1 means by column

kind: sorting algorithm, provides quick sort, mixed sort, heap sort

order: does not refer to the order, when used in the future Let’s analyze the effect of this

: Sort array a, and directly change a

after sorting. For example:

>>a.sort(axis=1)
>>print a
Copy after login

1 4
1 3
Copy after login

2、numpy.sort(a,axis=-1,kind='quicksort',order=None)

Usage :numpy.sort(a)

Parameter description:

a: Array to be sorted, other effects are the same as 1

: For array a Sort, return a sorted array (same dimension as a), a unchanged

For example:

>>print numpy.sort(a,axis=1)
1 4
1 3
>>print a
1 4
3 1
Copy after login

3, numpy.argsort (a,axis=-1,kind='quicksort',order=None)

Usage method: numpy.argsort(a)

Parameter description: Same as 2

Effect: Sort array a and return a sorted index, a remains unchanged

For example:

>>print numpy.argsort(a,axis=1)
0 1
1 0
Copy after login

4, sorted (iterable,cmp=None,key=None,reverse=False)

Description: The built-in sorting function can be used for lists, dictionaries, etc.

iterable: iterable Type;

cmp: Function used for comparison, what is compared is determined by key, has a default value, and iterates an item in the collection;

key: uses a certain attribute and function of the list element As a keyword, proceed has a default value and is an item in the iterative collection;

reverse: sorting rule.reverse=True or reverse=False, the default is False (from small to large).

Return value: It is a sorted iterable type, the same as iterable;

For example: b is a dictionary

b:

{' a':2,'c':1,'b':3}

Sort b:

>>c=sorted(b.iteritems(),key=operator.itemgetter(1),reverse=False)
>>print c[(&#39;c&#39;, 1), (&#39;a&#39;, 2), (&#39;b&#39;, 3)]
Copy after login

Visible: return is a list

Summary

The above is the entire content of this article about several sorting methods of numpy arrays. I hope it will be useful to everyone. helped. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point out. Thank you friends for supporting this site!

Related recommendations:

Python Scientific Computing - Quick Start with Numpy

Why is numpy array so fast?

Python NumPy library installation and usage notes

The above is the detailed content of A brief discussion on several sorting methods of numpy array_python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template