Python Extension Library 1-numpy

Source: Internet
Author: User


1 Array objects

Create an array

Import= Np.arange (2,10) step is 1#[0,10] A total of 20 #  Creating an array with List/tuple

Fast generation of X*y full zero groups

A = Np.zeros ((3,4))
Random number of 0~1
A = Np.random.rand (5)

Converting one-dimensional arrays to two-dimensional arrays

A = Np.arange (= A.reshape (4,5)

PS: Using reshape ( -1,5) to get the same result, the row is automatically adapted according to the column

Constructs a higher-dimensional

A = A.reshape (2,2,5)

Converting two-dimensional arrays into one-dimensional arrays

A = Np.array ([[1,2,3],[4,5,6= Np.ravel (a)

View Array Properties

A.ndim view dimensions, A.shape view the dimensions of each dimension, a.size view the number of elements, A.dtype view element types

Array cutting Vsplit () and Hsplit ()

Vsplit () to carry out branches, while Hsplit, Np.vsplit (arr,indices)

A = Np.arange (+). Reshape ( -1,3)print  aprint np.vsplit (a,3)    Print np.hsplit (a,3)

Output to

Array ([[[0, 1, 2],       [ 3, 4, 5],       [ 6, 7, 8],       [ 9, 10, 11],       [12, 13, 14],       [15, 16, 17]]) [Array ([[0],1, 2],       [3, 4, 5]]), Array ([[[6], 7, 8],       [ 9, ten, one]]), Array ([[[12, 13, 14],       [15, 16, 17]])][array ([[0], [3],       [ 6],       [ 9],       [12],       []), Array ([[1]],       [ 4],       [ 7],       [10],       [13],       []), Array ([[2]],       [ 5],       [ 8],       [11],       [14],       [17]])

Slice by specified position

Change the second parameter to list to specify the location of the slice

Print # slicing on lines 1th and 4th

Array manipulation

1) ' + ', '-', ' * ', '/' subtraction

2) Open square root, exponential operation

A = Np.array ([up])print  np.array (a)print  np.sqrt (a)print Np.exp (a) Print Np.square (a) print np.power (a,5)print a**5  # and Np.power (a,5) Effect

3) Maximum minimum value

a.min () A.max () a.sum () a.min (Axis#minimun element in each column  #  Minimun elementin each row A.max (axis=0) A.max (axis=1)

4) array mean, median

Np.mean (a) Np.median (a)

Array values

1) You can directly use the subscript value, directly assigned to a shallow copy (B=a, B points to a memory address), to really copy, using copy

A = Np.array ([[1,2],[3,4== a.copy ()print a[0][0], b[0][0], c[0][0]  #  1 1 1b[0][0] = 5print a[0][0], b[0][0], c[0][0]  #  5 5 1

2) Use ': ' To access all data of a given dimension

A = Np.arange. Reshape (4, 5)print#取出 the 2nd to 4th element of each row of a

Array stitching

A =np.array ([Np.array] ([4,5,6#] [1, 2, 3, 4, 5, 6]#[[1, 2, 3],                                 #  [4, 5, 6]


Two Matrix objects

Create a matrix

The matrix is two-dimensional, and the array can be any positive integer dimension

A = Np.arange (5#[[0, 1, 2, 3, 4]]B = Np.mat ('1 2;3 4'# [1, 2],         # [3, 4]]

Matrix multiplication

The Matrix's ' * ' operator is a matrix multiplication, multiplication sign the matrix column on the left and the Matrix line to the right of multiplication sign are equal, whereas in the array the ' * ' operator is the corresponding multiplication of each element, and each dimension of the array on both sides of the multiplication sign needs to be consistent

Matrix Transpose

A = Np.array ([[1, 2, 3], [4, 5, 6]])print np.transpose (a)   # array with transpose  Print Np.matrix (a). T  # matrix with T

Matrix inversion

A = Np.mat ('1.0 2.0;3.0 4.0'= Nlg.inv (a)print#[[ 1.0000000e+00 0.0000000e+00]    #[8.8817842e-16 1.0000000e+00]]

Eigenvalues and eigenvectors

Eig_value, Eig_vector = Nlg.eig (a)

3 other

Missing value handling

Use Nan as the missing value, determine with isNaN

A = Np.random.rand (2,21] = Np.nanprint#[[False  True]# [FALSE]]

Python Extension Library 1-numpy

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.