“This is the 18th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.”

### 1. Ndarray: Multi-dimensional array object

Each array has an attribute:

- Shape: The number of dimensions in an array;
- Dtype: the data type of the array;
- Ndim: indicates the dimension.

#### 1.1 generate ndarray

**Array generator function**

The function name | describe |
---|---|

array | Convert input data (which can be lists, tuples, arrays, and other sequences) to NDARRay. If the data type is not explicitly specified, it will be inferred automatically. All input data is copied by default |

asarray | Converts input to NDARray, but does not copy if the input is already NDARray |

arrange | The array version of the Python built-in function range, which returns an array |

ones | Generates an all-1 array based on the given shape and data type |

ones_like | Generates an array of all ones of the same shape from the given array |

zeros | Generates an array of all zeros based on the given shape and data type |

zeros_like | Generates an array of all zeros of the same shape from the given array |

empty | Generates an empty array with no initialized values based on the given shape |

empty_like | Generates an empty array with the same shape but no initialized value from the given array |

full | Generates an array of specified values based on the given shape and data type |

full_like | Generates an array of the same shape but with the specified value from the given array |

eye,identity | Generate an NXN eigenmatrix (1 for all diagonal positions, 0 for the rest) |

#### 1.2 Data types of NDARray

**Numpy data type**

type | The type of code | describe |
---|---|---|

int8, uint8 | i1, u1 | Signed and unsigned 8-bit integers |

int16, uint16 | i2, u2 | Signed and unsigned 16-bit integers |

int32, uint32 | i4, u4 | Signed and unsigned 32-bit integers |

int64, uint64 | i8, u8 | Signed and unsigned 64-bit integers |

float16 | f2 | Semi-precision floating point number |

float32 | F4 or f | Standard single-precision floating point numbers; Compatible with C language float |

float64 | F8 or d | Standard double precision floating point number; Compatible with C double and Python float |

float28 | F16 or g | Expand precision floating point numbers |

complex64, complex128, complex256 | c8, c16, c32 | Complex numbers based on 32-bit, 64-bit, and 128-bit floating-point numbers, respectively |

bool | ? | Boolean value that stores True or False |

object | O | Python object type |

String_ | S | Corrected ASCII string type; For example, to generate a string type of length 10, use ‘S10’ |

unicode_ | U | Modified Unicode type to generate a Unicode type of length 10, using ‘U10’ |

- You can explicitly convert the data type of an array using astype;
- Always generate a new array with Astype, even if you pass in the same DType as before.

#### 1.3 Basic index and Slice

- Unlike Python’s built-in list, a slice of an array is a view of the original array. This means that the data is not copied, and any changes to the view are reflected in the original array;
- [:] without slice values will refer to all values of the array;
- If you want a copy of a group rather than a view, you need to explicitly copy the array, for example arr[5:8].copy();
- When slicing, a single colon selects the array along the entire axis. For example, for a 2-dimensional array arr, arr[:, :1] represents the first column of all rows.

#### 1.4 Boolean index

- The length of the Boolean array must match the length of the array axis index;
- If the length of the array is incorrect, the method of selecting data by Boolean values does not give an error.
- When selecting data using a Boolean index, a copy of the data is always generated, and the array returned in a timely manner remains unchanged.

#### 1.5 Array transpose and transpose

**Transpose is a special form of data reorganization that returns a view of the underlying data without copying anything.**

- Transpose: T;
- Inner product of matrix: Np. dot;
- For high-dimensional arrays, the transpose method accepts tuples containing axis numbers to replace axes;
- Transpose with dot T is a special case of changing an axis. Ndarray has a swapaxes method that takes a pair of axis numbers as parameters and adjusts the axes to recombine the data.

### 2. Generic functions: fast element-by-element array functions

**A generic function, also known as uFUNc, is a function that performs element-by-element operations on NDARray data.**

#### 2.1 Unary general function

The function name | describe |
---|---|

Abs, fabs | Evaluates the absolute value of an integer, floating-point, or complex number element by element |

sqrt | Calculate the square root of each element (equal to ARr ** 0.5) |

square | Compute the square of each element (equal to arr ** 2) |

exp | Compute the natural exponential value of each element$e^x$ |

Log, log10, log2, log1p | Corresponding to: natural logarithm (e is base), logarithm base 10, logarithm base 2, log(1+x) |

sign | Calculate the sign value of each element: 1 (integer), 0 (0), -1 (negative) |

ceil | Computes the highest integer value for each element (that is, the smallest integer greater than or equal to the given value) |

floor | Computes the minimum integer value for each element (that is, the largest integer less than or equal to the given value) |

rint | Keep the element to integer bits and hold dTYPE |

modf | Returns the decimal and integer parts of the array as arrays, respectively |

isnan | Returns whether the element in the array is a NaN (not a number) in the form of a Boolean array of values |

Isfinite, isinf | Returns whether the elements in an array are finite (not INF, not NaN), and whether they are infinite, in the form of a Boolean array |

Cosine, cosh, sin, sinh, tan, tanh | Conventional hyperbolic trig functions |

Arccosine, arccosh, arcsin, arcsinh, arctan, arctanh | Inverse trigonometric function |

logical_not | Bitwise invert the elements of the data (same as ~arr) |

#### 2.2 Binary general function

The function name | describe |
---|---|

add | Add the corresponding elements of the array |

subtract | In the second array, the elements contained in the first array are removed |

multiply | Multiply the corresponding elements of the array |

Divide, floor_divide | In addition to or divisible |

power | Take the elements of the second array as powers of the corresponding elements of the first array |

Maximum, fmax | Computes the maximum value element by element, and fmax ignores NaN |

Minimum, fmin | Evaluates the minimum element by element, and fmin ignores NaN |

mod | Calculate by modulo of an element (the remainder of a division) |

copysign | Change the sign value of the first array to the sign value of the second array |

Greater, greater_equal, less, LESS_equal, equal, and not_equal | Performs an element-by-element comparison, returning an array of Boolean values |

Logical_and, logical_OR, logical_xor | Doing element-by-element logical operations (and or not) |

### 3. Array-oriented programming with arrays

Using NumPy arrays allows you to do a variety of data manipulation tasks with simple array expressions without having to write a lot of loops. This use of array expressions instead of explicit loops is called vectorization.

- The np. meshGrid function takes two one-dimensional arrays and generates a two-dimensional matrix based on all (x, y) pairs of the two arrays.

#### 3.1 Operate conditional logic as an array

**The numpy. Where function is the vectorized version of the ternary expression x if condition else y.**

- The second and third arguments to where do not need to be arrays (arrays of equal size), they can be scalars;
- A typical use of WHERE in data analysis is to generate a new array from an array.

#### 3.2 Mathematical and statistical methods

Many mathematical functions that compute entire array statistics or on axial data can be called as methods of array types. You can use aggregate functions (often called reduction functions), such as sum, mean, and STD (standard deviation), either to call the methods of an array instance directly or to use the top-level Numpy functions.

**Basic array statistics methods**

methods | describe |
---|---|

sum | Compute the tired sum of all elements along the axis, 0 length array, the tired sum is 0 |

mean | Mathematical average, the average of an array of length 0 is NaN |

STD, var | Standard deviation and variance, you can choose the degree of freedom adjustment (default denominator is n) |

Min, Max, | Minimum and maximum |

Argmin, argmax | The position of the minimum and maximum values |

cumsum | The sum of the elements starts at 0 |

cumprod | The sum of the elements starts at 1 |

#### 3.3 Boolean array methods

- Sum: Counts the number of True values in the Boolean array;
- All: checks whether each value is True;
- Any: Checks whether at least one of them is True.

#### 3.4 Unique values and other set logic

Numpy contains some basic set operations for one-dimensional NDARRay. A common method is np.unique, which returns an array of the unique values sorted.

**Collection operations on arrays**

methods | describe |
---|---|

unique(x) | Compute unique values of x and sort them |

intersect1d(x, y) | Compute the intersection of x and y and sort it |

union1d(x, y) | Compute the union of x and y and sort it |

in1d(x, y) | Computes whether elements in x are contained in y, returning an array of Boolean values |

setdiff1d(x, y) | The difference set, the element of x that is in x but is no longer in y |

setxor1d(x, y) | An xor set, in x or y, but not a member of the intersection of X and y |

### 4. Use arrays for file input and output

Numpy can store or load data on hard disk as text or binary files.

- Np. save and NP. load are two utility functions that make fun of reading hard disk data. Arrays are stored by default in an uncompressed format with the suffix.npy;
- You can use np.savez and pass an array as an argument to save multiple arrays in an uncompressed file;
- If the data has been compressed, run numpy.savez_compressed to save the data to the compressed file.

### 5. Linear algebra

- Dot: matrix product, x.dot(y) is equivalent to Np. dot(x, y);
- The special @ symbol is also used as the infix operator for dot matrix operations: x @np.ones (3);
- Numpy. Linalg has a standard set of functions for matrix factorization, as well as other common functions such as inverse and determinant solving.

**The numpy. Linalg function is commonly used**

function | describe |
---|---|

diag | Returns the diagonal (or off-diagonal) elements of a square matrix as a one-dimensional array, or converts the one-dimensional array to a square matrix with zeros on non-diagonal lines |

dot | Dot matrix |

trace | Compute the sum of diagonal elements |

det | Compute the determinant of the matrix |

eig | Compute the eigenvalues and eigenvectors of the square matrix |

inv | Compute the inverse of the square matrix |

pinv | Compute the Moore-Penrose pseudo-inverse of the matrix |

qr | Calculate QR decomposition |

svd | Computational Singular Value Decomposition (SVD) |

solve | A linear system for x Ax is equal to b. Where A is the square matrix |

lstsq | Compute the least squares solution to Ax is equal to b |

### 6. Pseudo-random number generation

**A partial list of functions in numpy.random**

function | describe |
---|---|

seed | Pass a random state seed to a random number generator |

permutation | Returns a random permutation of a sequence or an out-of-order sequence of integer ranges |

shuffle | Arrange a random sequence |

rand | Take a sample from a uniform distribution |

randint | Extract random integers based on a given range from low to high |

randn | Sample extraction from normal distribution of mean 0 and variance 1 (MATLAB-type interface) |

binomial | We’re taking samples from the binomial distribution |

normal | Sampling from a normal (Gaussian) distribution |

beta | Sample from beta distribution |

chisquare | Take a sample from the Chi-square distribution |

gamma | Take a sample from the gamma distribution |

uniform | Samples are extracted from the uniform [0, 1] distribution |