Advanced 15th Course Python Module NumPy

Source: Internet
Author: User
Tags natural logarithm sorts

Turn

From where a great God turned, let's study together. The sample code will be incrementally added in the future.

NumPy NumPy is the foundation package for high performance scientific computing and data analysis. Some of the functions are as follows:
      • Ndarray, a fast and space-saving multidimensional array with vector arithmetic operations and complex broadcast capabilities.
      • A standard mathematical function for fast operation of the entire set of data (without writing loops).
      • Tools for reading and writing disk data, and tools for manipulating memory-mapped files.
      • Linear algebra, random number generation, and Fourier transform functions.
      • Tools for integrating code written in C, C + +, Fortran, and other languages.

First, import the NumPy library: import NumPy as NP

A NumPy functions and properties:
Type Type code Description
int8, Uint8 I1, U1 Signed and unsigned 8-bit integer (1 bytes)
Int16, UInt16 I2, U2 Signed and unsigned 16-bit integer (2 bytes)
Int32, UInt32 I4, U4 Signed and unsigned 32-bit integer (4 bytes)
Int64, UInt64 i8, U8 Signed and unsigned 64-bit integer (8 bytes)
Float16 F2 Semi-precision floating-point number
Float32 F4, F Single-precision floating-point number
Float64 F8, D Double-precision floating-point number
float128 F16, G Extended precision floating point number
Complex64 C8 Plural with two 32-bit representations, respectively
complex128 C16 Plural with two 64-bit representations, respectively
complex256 C32 Plural with two 128-bit representations, respectively
bool ? Boolean type
Object O Python Object
String Sn Fixed-length strings, 1 bytes per character, such as S10
Unicode Un Fixed-length Unicode, the number of bytes determined by the system, such as U10

Table 2.1.a.1 NumPy Types

Generating functions Role

Np.array (x)

Np.array (x, Dtype)

Convert input data to a Ndarray

Convert input data to a type of Ndarray

Np.asarray (Array) Convert input data to a new (copy) Ndarray

Np.ones (N)

Np.ones (N, Dtype)

Np.ones_like (Ndarray)

Generates an n-length one-dimensional all-one ndarray

Generating an N-length type is a one-dimensional all-one ndarray of Dtype

Create a ndarray with the same shape as the parameter

Np.zeros (N)

Np.zeros (N, Dtype)

Np.zeros_like (Ndarray)

Generates an n-length one-dimensional full 0 Ndarray

Generates an n-length type bit dtype one-dimensional full 0 Ndarray

Similar to Np.ones_like (Ndarray)

Np.empty (N)

Np.empty (N, Dtype)

Np.empty (Ndarray)

Generates an uninitialized one-dimensional ndarray of N-length

Generates an n-length type that is an uninitialized one-dimensional ndarray of Dtype

Similar to Np.ones_like (Ndarray)

Np.eye (N)

Np.identity (N)

Create an n * N unit Matrix (diagonal is 1, the remainder is 0)

Np.arange (num)

Np.arange (begin, end)

Np.arange (begin, end, step)

Generate a one-dimensional ndarray from 0 to num-1 steps 1

Generate a one-dimensional ndarray from begin to end-1 steps 1

Build a one-dimensional ndarray that steps from begin to End-step are step

Np.mershgrid (Ndarray, Ndarray,...)

Generate a Ndarray * Ndarray * ... The Multidimensional Ndarray

Np.where (Cond, Ndarray1, Ndarray2)

Depending on the condition cond, select Ndarray1 or Ndarray2 to return a new Ndarray

NP.IN1D (Ndarray, [x, y,...])

Checks if the elements in the Ndarray are equal to [x, y,...] Returns a bool array in one of the

Matrix functions Description

Np.diag (Ndarray)

Np.diag ([x, y,...])

Returns a diagonal (or non-diagonal) element of a square matrix as a one-dimensional array

Converts a one-dimensional array to a square (non-diagonal element is 0)

Np.dot (Ndarray, Ndarray) Matrix multiplication
Np.trace (Ndarray) Computes the and of the diagonal elements

Sort function

Description

Np.sort (Ndarray)

Sort, return copy

Np.unique (Ndarray)

Returns the elements in Ndarray, excludes duplicate elements, and sorts

NP.INTERSECT1D (Ndarray1, Ndarray2)

NP.UNION1D (Ndarray1, Ndarray2)

NP.SETDIFF1D (Ndarray1, Ndarray2)

NP.SETXOR1D (Ndarray1, Ndarray2)

Returns the intersection of the two and sorts.

Returns the combination of the two and sorts.

Returns the difference between the two.

Returns the symmetry difference between the two

Unary calculation function Description

Np.abs (Ndarray)

Np.fabs (Ndarray)

Calculate absolute Value

Calculate absolute value (non-plural)

Np.mean (Ndarray)

Averaging

NP.SQRT (Ndarray)

Calculate x^0.5

Np.square (Ndarray)

Calculate x^2

Np.exp (Ndarray)

Calculate e^x

Log, log10, log2, log1p

Log with natural logarithm, bottom 10 log, bottom 2 log, bottom (1+x)

Np.sign (Ndarray)

Calculate positive and negative numbers: 1 (positive), 0 (0), 1 (negative)

Np.ceil (Ndarray)

Np.floor (Ndarray)

Np.rint (Ndarray)

Calculates the smallest integer greater than or equal to the value

Calculates the largest integer less than or equal to the value

Rounding to the nearest integer, preserving Dtype

NP.MODF (Ndarray)

Returns the decimal and integer parts of an array as two independent arrays

Np.isnan (Ndarray)

Returns a bool array that determines whether it is Nan

Np.isfinite (Ndarray)

Np.isinf (Ndarray)

Returns a Boolean array that determines whether it is a poor (non-INF, non-Nan)

Returns an array of type bool that determines whether it is infinite

Cos, cosh, sin, sinh, tan, Tanh

General type and hyperbolic trigonometric functions

Arccos, Arccosh, Arcsin, Arcsinh, Arctan, Arctanh

Inverse trigonometric function and hyperbolic type inverse trigonometric function

Np.logical_not (Ndarray)

Calculates the true value of not x for each element, equivalent to-ndarray

Multivariate computational functions

Description

Np.add (Ndarray, Ndarray)

Np.subtract (Ndarray, Ndarray)

Np.multiply (Ndarray, Ndarray)

Np.divide (Ndarray, Ndarray)

Np.floor_divide (Ndarray, Ndarray)

Np.power (Ndarray, Ndarray)

Np.mod (Ndarray, Ndarray)

Add

Subtraction

Multiplication

Division

Circle Divide Method (discard remainder)

Sub-party

Die-finding

Np.maximum (Ndarray, Ndarray)

Np.fmax (Ndarray, Ndarray)

Np.minimun (Ndarray, Ndarray)

Np.fmin (Ndarray, Ndarray)

To find the maximum value

Maximum value (Ignore Nan)

To find the minimum value

Minimum value (Ignore Nan)

Np.copysign (Ndarray, Ndarray)

Assigning the symbol in Parameter 2 to parameter 1

Np.greater (Ndarray, Ndarray)

Np.greater_equal (Ndarray, Ndarray)

Np.less (Ndarray, Ndarray)

Np.less_equal (Ndarray, Ndarray)

Np.equal (Ndarray, Ndarray)

Np.not_equal (Ndarray, Ndarray)

>

>=

<

<=

==

!=

Logical_and (Ndarray, Ndarray)

Logical_or (Ndarray, Ndarray)

Logical_xor (Ndarray, Ndarray)

&

|

^

Np.dot (Ndarray, Ndarray) Calculating the matrix inner product of two Ndarray
Np.ix_ ([x,y,m,n],...) Generates an indexer for fancy indexing (fancy index)
File read/write Description
Np.save (String, Ndarray) Save Ndarray to file named [String].npy] (no compression)
Np.savez (String, Ndarray1, Ndarray2, ...) Save all Ndarray compression to a file named [String].npy]
Np.savetxt (sring, Ndarray, FMT, newline= ' \ n ') Write Ndarray to file in FMT format
Np.load (String) Reads the file contents of a filename string and translates it into a Ndarray object (or Dictionary object)
Np.loadtxt (String, delimiter) Reads the file contents of the filename string, converting it to ndarray with delimiter as a delimiter
table 2.1.a.2 NP common functionsB Numpy.ndarray functions and properties:
Ndarray.ndim Get the dimension of Ndarray
Ndarray.shape Get the length of each dimension of Ndarray
Ndarray.dtype Gets the data type of the element in the Ndarray
Ndarray. T Simple Transpose matrix Ndarray
table 2.1.b.1 Ndarray Properties
Function Description
Ndarray.astype (Dtype) The conversion type, which occurs if the conversion fails TypeError
Ndarray.copy () Copying a copy of Ndarray (new memory space)
Ndarray.reshape ((n,m,...)) Convert Ndarray to n*m* ... The multidimensional Ndarray (not copy)
Ndarray.transpose ((Xindex,yindex,...)) Xindex,yindex by Dimension Index ... Matrix transpose, dependent on shape, cannot be used for one-dimensional matrices (not copy)
Ndarray.swapaxes (Xindex,yindex) Swap dimensions (not copy)
Calculation function Description
Ndarray.mean (axis=0) Averaging
Ndarray.sum (axis= 0) Sum

Ndarray.cumsum (axis=0)

Ndarray.cumprod (axis=0)

Accumulation

Tired Ride

NDARRAY.STD ()

Ndarray.var ()

Variance

Standard deviation

Ndarray.max ()

Ndarray.min ()

Maximum Value

Minimum value

Ndarray.argmax ()

Ndarray.argmin ()

Index of Maximum value

Minimum value Index

Ndarray.any ()

Ndarray.all ()

If there is at least one true

Whether all is true

Ndarray.dot (Ndarray)

Calculating the inner product of matrices

Sort function

Description

Ndarray.sort (axis=0)

Sort, return source data

table 2.1.b.2 Ndarray functions
Ndarray[n] Select N+1 Element
NDARRAY[N:M] Select N+1 to M elements
ndarray[:] Select all elements
Ndarray[n:] Select N+1 to last element
NDARRAY[:N] Select No. 0 to nth element

ndarray[Bool_ndarray]

Note: Bool_ndarray represents a bool type of Ndarray

Select the element that is true

Ndarray[[x,y,m,n]] ...

Select the order and sequence of Ndarray for X, Y, M, n

NDARRAY[N,M]

NDARRAY[N][M]

Select the m+1 element of line n+1

Ndarray[n,m,...]

Ndarray[n][m] ....

Select N rows n columns .... The Elements
table 2.1.b.3 Ndarray index/Slice modeC Numpy.random functions and properties: with normal distribution (average 0, standard deviation 1)
function description

Seed ()

Seed (int)

Seed (ndarray)

determine random number generation seed

Permutation (int)

Permutation (Ndarray)

td>

Returns the random arrangement of a one-dimensional sequence from 0~9

Returns the random arrangement of a sequence

Shuffle (ndarray) for a sequence in-place random order

rand (int)

Randint (begin,end,num=1)

Produce an int uniformly distributed sample value

Randomly selects num integers from a given begin and end

randn (N, M, ...) Generate a n*m* ... Ndarray
Normal (size= (n,m,...)) Generate a n*m* ... Ndarray  of the Normal (Gaussian) distribution;
Beta (ndarray1,ndarray2) generates a sample value for the beta distribution, and the parameter must be greater than 0 
 chisquare () generates sample values for Chi-square distribution  
 gamma () produces a gamma distribution of sample values  
 uniform () produces sample values that are evenly distributed in [0,1]  

2.1.c.1 Random Common functions

D Numpy.linalg functions and properties:
Function Description
Det (Ndarray) Computed matrix column type
Eig (Ndarray) The eigenvalues and eigen vectors of the matrix are calculated

INV (Ndarray)

PINV (Ndarray)

Calculating the inverse of a square matrix

Moore-penrose pseudo-inverse of a square matrix

QR (Ndarray) calculate QR Decomposition
SVD (Ndarray) Compute singular value decomposition SVD
Solve (Ndarray) Solving a linear equation Group ax = b, where A is a phalanx
LSTSQ (Ndarray) The least squares solution for calculating ax=b

2.1.d.1 Linalg Common functions

Turn

54583415

https://zhuanlan.zhihu.com/p/26514493

Advanced 15th Course Python Module NumPy

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.