Common NumPy Usage Detailed introduction

Source: Internet
Author: User
Tags sin

NumPy Introduction

The presence of NumPy makes Python a powerful matrix computing power, no less than MATLAB.
Official documents (https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)

Introduction to various usages

The first is the data type in NumPy, the Ndarray type, not the same as Array.array in the standard library.

Some properties of Ndarray

Ndarray.ndim
The number of axes (dimensions) of the array. In the Python world, the number of dimensions was referred to as rank.
Ndarray.shape
The dimensions of the array. This is a tuple of integers indicating, the size of the array in each dimension. For a matrix with n rows and m columns, shape'll Be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, Ndim.
ndarray.size
The total number of elements of the array. This is equal to the product of the elements of shape.
Ndarray.dtype
An object describing the type of the elements in the array. One can create or specify Dtype ' s using standard Python types. Additionally NumPy provides types of its own. Numpy.int32, Numpy.int16, and Numpy.float64 are some examples.
ndarray.itemsize
The size in bytes of each element of the array. For example, an array of elements of type float64 have itemsize 8 (=64/8), while one of the type complex32 has itemsize 4 (=32/ 8). It is equivalent to Ndarray.dtype.itemsize.
Ndarray.data
The buffer containing the actual elements of the array. Normally, we won ' t need to use this attribute because we'll access the elements in an array using indexing facilities.

Creation of Ndarray

>>> Import NumPy as np>>> a = Np.array ([2,3,4]) >>> Aarray ([2, 3, 4]) >>> A.dtypedtype ( ' Int64 ') >>> B = Np.array ([1.2, 3.5, 5.1]) >>> b.dtypedtype (' float64 ')

Two-dimensional arrays

>>> B = Np.array ([(1.5,2,3), (4,5,6)]) >>> Barray ([[1.5,  2.,  3.],       [4.,  5.,  6.] ])

Specify type at creation time

>>> C = Np.array ([[Up], [3,4]], Dtype=complex) >>> CArray ([[1.+0.J,  2.+0.J],       [3.+0.J,  4.+0.J]])

Create a few special matrices

>>> Np.zeros ((3,4)) array ([[0.,  0.,  0.,  0.],       [0.,  0.,  0.,  0.],       [0.,  0.,  0.,  0.]) >>> Np.ones ((2,3,4), dtype=np.int16)                # dtype can also be specifiedarray ([[[[[1], 1, 1, 1],        [1, 1, 1, 1 ],        [1, 1, 1, 1]],       [[1, 1, 1, 1],        [1, 1, 1, 1],        [1, 1, 1, 1]], dtype=int16) >>> Np.empty ( (2,3))                                 # Uninitialized, output may varyarray ([[  3.73603959e-262,   6.02658058e-154,   6.55490914E-260],       [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])

Create a number of matrices with specific rules

>>> Np.arange (2, 5), Array ([ten, 0, +]) >>> Np.arange (+/-)                 # accepts float Argum Entsarray ([0.,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8]) >>> from numpy import pi>>> Np.linspace (0, 2, 9)                 # 9 numbers from 0 to 2array ([0.  ,  0.25,  0.5,  0.75,  1.  ,  1.2 5,  1.5,  1.75,  2.  ]) >>> x = np.linspace (0, 2*pi, +)        # useful to evaluate function at lots of points>>> f = np.sin (x )

Some of the basic operations

Subtraction Trigonometric Logic Operations

>>> a = Np.array ([20,30,40,50]) >>> B = Np.arange (4) >>> Barray ([0, 1, 2, 3]) >>> C =  A-b>>> CArray ([0, 1, 4, 9]) >>> 10*np.sin (a), array ([9.12945251, -9.88031624,  7.4511316, -2.62374854]) >>> A<35array ([True, True, False, false], Dtype=bool)

Matrix operations
MATLAB has. *,./etc.
However, in NumPy, if the use of +,-,x,/precedence is the subtraction method between the various points
If two matrices (squares) can be executed between elements and perform matrix operations, the operations between elements will be prioritized

>>> Import NumPy as np>>> a = Np.arange (10,20) >>> B = Np.arange (20,30) >>> A + Barray ([ ---------------------------------------] >>> A * Barray ([231, 299, 336, 375, 416, 459, 504, 551]) >> ;> A/barray ([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) >>> b/aarray ([2, 1, 1, 1, 1, 1, 1, 1, 1, 1])

If you need to perform matrix operations, it is generally the multiplication of matrices

>>> A = Np.array ([1,1,1,1]) >>> B = Np.array ([2,2,2,2]) >>> a.reshape (2,2) Array ([[1, 1],       [1, 1]]) >>> B.reshape (2,2) Array ([[[2, 2],       [2, 2]]) >>> A * Barray ([2, 2, 2, 2]) >>> Np.dot (A, b) 8> >> A.dot (B) 8

Some of the common global functions

>>> B = Np.arange (3) >>> Barray ([0, 1, 2]) >>> Np.exp (b) Array ([1.        ,  2.71828183,  7.3890561]) >>> np.sqrt (B) array ([0.        ,  1.        ,  1.41421356]) >>> C = Np.array ([2.,-1., 4.]) >>> Np.add (B, C) Array ([2.,  0.,  6.])

Index Shard Traversal of matrices

>>> a = Np.arange (Ten) **3>>> aarray ([  0,   1,   8,  64,  125, 216, 343, 512, 729])  >>> a[2]8>>> A[2:5]array ([8, +, 1000]) >>> a[:6:2] = -1000    # equivalent to a[0:6:2] =; From start to position 6, exclusive, set every 2nd element to-1000>>> Aarray ([ -1000,     1, -1000,    27,-100 0,   216,   343,   729]) >>> a[:: -1]                                 # reversed Aarray ([  729,   343,   216,   -1000,    -1000,     1, -1000]) >>> for i in a: ...     Print (i** (1/3.)) ... nan1.0nan3.0nan5.06.07.08.09.0

Traversal of matrices

>>> import NumPy as np>>> B = np.arange (+). Reshape (4, 4) >>> for row in B: ...  Print (Row) ... [0 1 2 3] [4 5 6 7] [8  9 11][12 15]>>> for node in B.flat: ...  Print (node) ... 0123456789101112131415

Special operations of matrices

Change matrix shape--reshape

>>> a = Np.floor (Ten * Np.random.random ((3,4))) >>> Aarray ([[6.,  5.,  1.,  5.],       [5.,  5.,  8.,  9.],       [5.,  5.,  9.,  7.]) >>> a.ravel () array ([6.,  5.,  1.,  5.,  5.,  5.,  8.,  9.,  5  ., 5.,  9.,  7.]) >>> Aarray ([[6.,  5.,  1.,  5.],       [5.,  5.,  8.,  9.],       [5.,  5.,  9.,  7.])

The difference between resize and reshape
Resize will change the original matrix, reshape will not

>>> Aarray ([[6.,  5.,  1.,  5.],       [5.,  5.,  8.,  9.],       [5.,  5.,  9.,  7.]) >>> A.reshape (2,-1) Array ([[6.,  5.,  1.,  5.,  5.,  5.],       [8.,  9  ., 5.,  5.,  9.,  7.]) >>> Aarray ([[6.,  5.,  1.,  5.],       [5.,  5.,  8.,  9.],       [5.,  5.,  9.,  7]]) >>> a.resize (2,6) >>> Aarray ([[6.,  5.,  1.,  5.,  5., 5.  ],       [8.,  9.,  5.,  5.,  9.,  7.])

Merging of matrices

>>> a = Np.floor (10*np.random.random ((2,2)) >>> Aarray ([[8.,  8.],       [0.,  0.]) >>> B = Np.floor (10*np.random.random ((2,2))) >>> Barray ([[1.,  8.],       [0.,  4.]) >>> Np.vstack ((b)) array ([[8.,  8.],       [0.,  0.],       [1.,  8.],       [0.,  4.]] >>> Np.hstack ((b)) array ([[8.,  8.,  1.,  8.],       [0.,  0.,  0.,  4.])

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.