NumPy Library
Official English Document: https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
Array objects in the NumPy library: n-dimensional array type: Ndarray
1) The role of Ndarray:
A) The array object can eliminate the loops required for inter-element operations, making one-dimensional vectors more like a single piece of data.
b) Set up specialized array objects, which are optimized to increase the computational speed of such applications.
2) Ndarray is a multidimensional array object that consists of two parts:
The actual data and the metadata that describes the data (data dimensions, data types, etc.)
Ndarray arrays generally require all element types to be the same (homogeneous), array subscripts start from zero
3) Ndarray The properties of the instance object:
. Ndim: Rank, that is, the number of axes or the number of dimensions
The scale of the. Shape:ndarray object, for matrices, n rows M column
. Size:ndarray the number of object elements, equivalent to the n*m in. Shape
The element type of the. Dtype:ndarray Object
The size, in bytes, of each element in the. Itemsize:ndarray Object
4) Element type of Ndarray:
Data type |
Description |
bool |
Boolean type, True or False |
Intc |
Int32 or int6 consistent with the int type in the C language |
Intp |
The integer used for the index, consistent with ssize_t in C, Int2 or Int64 |
int8 |
8 byte length integer, Value [-128,127] |
Int16/int32/int64 |
Similar int8 |
Uint8 |
8-bit no positive number, value [0,255] |
Uint16/uint32/uint64 |
Similar uint8 |
Float16 |
16-bit semi-precision floating-point number: 1-bit sign bit, 5-bit exponent (10^ index), 10-bit mantissa |
Float32 |
Similar to float16;1 for sign bit, 8-bit exponent, 23-bit mantissa |
Float64 |
Similar to float16;1 for sign bit, 11-bit exponent, 52-bit Mantissa |
Complex64 |
Complex types, both real and imaginary are 32-bit floating-point numbers |
complex128 |
Complex types, both real and imaginary are 64-bit floating-point numbers |
Comparison: Python syntax supports only integers, floating-point numbers and 3 types of complex numbers
The scientific calculation has higher requirements for the type and accuracy of the data.
Note: Non-homogeneous ndarray can not effectively play the numpy advantage, try to avoid using
5) How to create an Ndarray array:
A) Create a Ndarray array from a list, tuple, and other types in Python
X=numpy.array (List/tuple)
X=numpy.array (List/tuple, Dtype=np.int64)
Do not specify Dtype,numpy will associate a dtype based on data conditions
b) Create an Ndarray array using the NumPy function, such as: Arange,ones,zeros, etc.
Function |
Description |
Numpy.arange (N) |
Similar to the range () function, returns the Ndarray type, with elements from 0 to n-1 |
Numpy.ones (Shape) |
Generates a full array of shapes based on shape, which is a tuple type |
Numpy.zeros (Shape) |
Generates a full zero group based on shape, which is a tuple type |
Numpy.full (Shape,val) |
An array is generated from shape, and each element value is Val |
Numpy.eye (N) |
Generating n-Order unit matrices |
Numpy.ones_like (a) |
Generates a full 1 array based on the shape of the array a |
Numpy.zeros_like (a) |
Generates a full 0 array based on the shape of the array a |
Numpy.full_like (A,val) |
Generates an array based on the shape of the array A, and each element value is Val |
Numpy.linspace () |
Fills data, such as starting and ending data, to form an array |
Numpy.concatenate () |
Combine two or more arrays into a new array |
c) Creating an Ndarray array from the byte stream (raw bytes)
d) read a specific format from a file and create an Ndarray array
6) Transformation of the Ndarray array
A) Dimension transformations for ndarray arrays (e.g. X=numpy.eye (n))
Function |
Description |
X.reshape (Shape) |
Does not change the array element, returns an array of shape shapes, the original array does not change |
X.resize (Shape) |
Consistent with the. Reshape () function, but modifies the original array |
X.swapaxes (AX1,AX2) |
Swap two dimensions in an array of n dimensions |
X.flatten () |
Descending dimension of the array, returning the collapsed one-dimensional array, the original array unchanged |
b) Other transformations of the Ndarray array
Function |
Description |
X.astype (New_type) |
Type transformation: Create a new Array (a copy of the original data) even if two data types are consistent |
X.tolist () |
Transform to List |
7) operation of the Ndarray array:
Index of the array: Gets the specific element in the array. For example
Slice of array: The process of getting a subset of array elements.
A) index and slice of one-dimensional array: Similar to Python's list
b) Index of multidimensional arrays:
The index values for each dimension are separated by commas, and one dimension is selected with: (colon), and each dimension slice method is the same as a one-dimensional array.
For example:
8) operation of the Ndarray array:
An operation between an array and a scalar:
An operation between an array and a scalar acts on each element of the array
The unary function of the numpy:
Function |
Description |
Numpy.abs (x)/.fabs (x) |
Calculates the absolute value of each element in an array |
NUMPY.SQRT () |
Computes the square root of each element in an array |
Numpy.square (x) |
Computes the square of each element in an array |
Numpy.log (x)/.log10 (x)/log2 (x) |
Calculates the natural logarithm, 10 logarithm, and 2 logarithm of each element of an array |
Numpy.ceil (x)/.floor (x) |
Computes the ceilling value or floor value of each element of an array |
Numpy.rint (x) |
Calculate rounding values for each element of an array |
NUMPY.MODF (x) |
Returns the fractional and integer portions of each element of an array as two independent arrays |
Numpy.cos (x)/.cosh (x) Numpy.sin (x)/.sinh (x) Numpy.tan (x)/.tanh (x) |
Compute normal and hyperbolic trigonometric functions for each element of an array |
Numpy.sign (x) |
Calculate the symbol values for each element of an array |
Numpy.exp (x) |
Computes the exponential value of each element of an array |
NumPy Two-dollar function:
Function |
Description |
+ - * / ** |
The corresponding operation of the elements of the two arrays |
Numpy.maximum (x)/.fmax (x) Numpy.minimum (x)/.fmin () |
The maximum value of the element level |
Numpy.mod (x, y) |
Modulo operations at the element level |
Numpy.copysign (x, y) |
Assigns the symbol of each element in the array y to the array x corresponding element |
> < >= = = =! |
Arithmetic comparison operators, producing Boolean types |
Python--numpy Library