On several sorts of numpy array _python

Source: Internet
Author: User
Tags iterable sorts
This article mainly introduces several sorts of numpy arrays, involving the simple introduction of numpy and the way to create arrays, with a certain reference value, to numpy interested friends can refer to.

Simple Introduction

The NumPy system is an open-source array computing extension of Python. This tool can be used to store and manipulate large matrices, which is much more efficient than Python's own nested list (nested list structure) structure, which is also useful for representing matrices (matrix).

Create an array

To create a 1-D array:

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

View array Dimensions

data.shape

View Array types

data.dtype

Get or modify an array element by index

data[1] 获取元素
data[1] = 'a' 修改元素 
 

Creating a two-dimensional array

data = np.array([[1,2,3],[4,5,6]])Two elements are list <br>2.data = Np.arange (10) As with Python's range, Range returns a list, Arange returns an array of type array <br>3.data2 = Data.reshape (2,5) returns an array of 2*5, he is not a copy array is a reference, just returns the different views of the array, and the data change data2 will also change

Creating a special array

data = np.zeros((2,2)) 创建2*2全为0的2维数组
data = np.ones((2,3,3,)) 创建全为1的三维数组
data = np.eye(4) 创建4*4的对角数组,对角元素为1,其它都为0

Array conversions

data = np.arange(16).reshape(4,4) 将0-16的移位数组转换为4*4的数组  

Sorting method

Note: It is often necessary to sort the array or list, Python provides several sorts of functions, the following describes the characteristics;

Two-dimensional array A:

1 43 1

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

How to use: A.sort

Parameter description:

Axis: Sort along the direction of the array, 0 means by row, 1 for column

Kind: Sorting algorithm that provides quick-row, mixed-row, heap-row

Order: Do not refer to the sequence, and then use it later to analyze this

Effect: Sort the array A and change the a directly after sorting

For example:

>>a.sort (Axis=1) >>print A

1 41 3

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

How to use:numpy.sort(a)

Parameter description:

A: To sort the array, the other same 1

Effect: Sort the array A, return a sorted list (same dimension as a), a unchanged

For example:

>>print Numpy.sort (A,axis=1) 1 3>>print A1 43 1

3, Numpy.argsort (a,axis=-1,kind= ' quicksort ', Order=none)

How to use: Numpy.argsort (a)

Parameter description: Same 2

Effect: Sorts an array A, returns a sorted index, a unchanged

For example:

>>print Numpy.argsort (a,axis=1) 0 11 0

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

Description: Built-in sorting function, for list, dictionary, etc. can be used

Iterable: is an iterative type;

CMP: A function For comparison, comparing what is determined by key, having a default value, iterating over an item in the set;

Key: A property and function of a list element as a keyword, with a default value, an item in the iteration set;

Reverse: Collation. reverse=true or Reverse=false, default False (small to large).

Return value: is a sort of iterated type, as iterable;

For example: B is a dictionary

B:

{' A ': 2, ' C ': 1, ' B ': 3}

To sort B:

>>c=sorted (B.iteritems (), Key=operator.itemgetter (1), reverse=false) >>print c[(' C ', 1), (' A ', 2), (' B ', 3) ]

Visible: A list is returned

Summarize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.