Summary introduction using the NumPy method

Source: Internet
Author: User
NumPy is an open-source numerical 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). NumPy (Numeric Python) offers a number of advanced numerical programming tools such as matrix data types, vector processing, and sophisticated operations libraries. Designed for rigorous digital processing. Many of the major financial companies used, as well as the core scientific computing organizations such as: Lawrence Livermore,nasa use it to deal with some of the original use of C++,fortran or MATLAB and other tasks.

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

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.