Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python Data Analysis ---- summary of common usages in the Numpy library


May 30, 2021 Article blog


Table of contents


preface

Before you introduce numpy, let's talk about the basic concepts of Python.

Python is an advanced, dynamic, multi-form programming language. P ython code often looks like pseudo-code, so you can implement a very powerful idea with a few lines of highly readable code.

Numpy is simple, a library of scientific calculations for Python that provides the functionality of matrix computing, which is typically used with Scipy and matplotlib. I n fact, list already provides a matrix-like representation, but numpy gives us more functions.

The editor-in-chief here summarizes some of numpy's common operations.

First, introduce the numpy package:

import numpy as np

First, in numpy N dimension array object is called ndarrey, some operations about it:

Properties of 1.ndarry objects:

attribute illustrate
.ndim Rank, that is, the number of axes or dimensions
.shape The scale of the ndarray object, for the matrix, n row m column
.size The number of elements of the ndarray object, equivalent to the value of n*m in .shape
.dtype The type of element of the ndarray object
.itemsize The size of each element in the ndarray object, in bytes

2.ndarry element type (1):

data type illustrate
bool Boolean type, True or False
intc Consistent with the type of int in C, it is usually int32 or int64
intp An integer used for indexing, consistent with ssize_t in C, int32 or int64
int8 Integer of byte length, value: .128, 127
int16 Integer of 16-bit length, value: s32768, 32767
int32 Integer of 32-bit length, value: .231, 231-1
int64 Integer of 64-bit length, value: .263, 263-1

2.ndarry element type (1):

data type illustrate
bool Boolean type, True or False
intc Consistent with the type of int in C, it is usually int32 or int64
intp An integer used for indexing, consistent with ssize_t in C, int32 or int64
int8 Integer of byte length, value: .128, 127
int16 Integer of 16-bit length, value: s32768, 32767
int32 Integer of 32-bit length, value: .231, 231-1
int64 Integer of 64-bit length, value: .263, 263-1

3.ndarry element type (2):

data type illustrate
uint8 8-bit unsigned integer, value: s0, 255
uint16 16-bit unsigned integer, value: s0, 65535
uint32 32-bit unsigned integer, value: s0, 232-1
uint64 32-bit unsigned integer, value: s0, 264-1
float16 16-bit semi-precision floating points: 1-digit sign bit, 5-bit index, 10-bit tail
float32 32-bit semi-precision floating points: 1-digit sign bit, 8-bit index, 23-bit tail
float64 64-bit semi-precision floating points: 1-digit symbol bit, 11-bit index, 52-bit tail
complex64 Plural types, real and imaginary are 32-bit floating points
complex128 The number of complex numbers, real and imaginary parts are 64-bit floating point numbers.

Summary of common functions in 4.ndarry:

function illustrate
np.arange(n) Similar to the range() function, which returns the ndarray type, the elements range from 0 to n-1
np.ones(shape) Based on the shape, a full 1 array is generated, shape is the tuple type np.zeros (shape) generates a full 0 array based on shape, and shape is the tuple type
np.full(shape,val) An array is generated from the shape, and each element value is val
np.eye(n) Create a square n-unit matrix with 1 diagonal and the rest 0
np.ones_like(a) An all-1 array is generated based on the shape of array a
np.zeros_like(a) An all-0 array is generated based on the shape of array a
np.full_like(a,val) An array is generated from the shape of array a, and each element value is val
np.linspace() Fill the data with equal spacing based on the start and end data to form an array
np.concatenate() Combine two or more numbers into a new array
.reshape(shape) Without changing the array elements, an array of shape shapes is returned, and the original array remains unchanged
.resize(shape) Consistent with the .reshape() function, but modifying the original array
.swapaxes(ax1,ax2) Swap two dimensions in the array n dimensions
.flatten() The array is degraded and returned to the collapsed one-dimensional array, which remains unchanged
np.abs(x) np.fabs(x) Calculate the absolute value of each element of the array
np.sqrt(x) Calculate the square root of each element of the array
np.square(x) Calculate the squares of each element of the array
np.log(x) np.log10(x) np.log2(x) Calculate the natural icings, 10-bottom icings, and 2-bottom icings of elements in an array
np.ceil(x) np.floor(x) Calculate the ceiling or floor values for each element of the array
np.rint(x) Calculates the rounding value of each element of the array
np.modf(x) Returns the decimal and integer parts of each element of the array as two separate arrays
np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) Calculate the normal and hyperbolic trigonometry functions of each element of the array
np.exp(x) Calculates the exponential values of the elements of the array
np.sign(x) Calculate the symbolic values of each element of the array, 1 (-), 0, -1 (-)
+ ‐* / ** The elements of the two arrays perform corresponding operations
np.maximum(x,y) np.fmax() np.minimum(x,y)np.fmin() The maximum/minimum value at the element level is calculated
np.mod(x,y) Element-level modal operations
np.copysign(x,y) Assign the symbols of each element value in array y to the array x corresponding element
> < >= <= == != Arithmetic comparisons produce boolean arrays

Second, in numpy random function library function summary:

1.np.random random function (1):

function illustrate
rand(d0,d1,..,dn) Create a random array based on d0-dn, floating points, s0,1), evenly distributed
randn(d0,d1,..,dn) Create a random array based on d0-dn, with a standard normal distribution
randint(low[,high,shape]) Create a random integer or array of integers based on shape, with a range of low, high
seed(s) Random number of seeds, s is the given seed value

2.np.random random function 2:

function illustrate
shuffle(a) Change array x according to the 1st axis of array a
permutation(a) A new disordered array is generated according to the 1st axis of array a, without changing array x
choice(a[,size,replace,p]) Extracting elements from one-dimensional array a with probability p to form a new array of size shapes indicates whether elements can be reused, defaulting to False

3. Np.random's random function (3):

function illustrate
uniform(low,high,size) Produces an array with a uniform distribution, low start value, high end value, size shape
normal(loc,scale,size) Produces an array with a normal distribution, loc mean, scale standard deviation, size shape
poisson(lam,size) Produces an array with Poisson distribution, lam random event rate, size shape

Third, in numpy statistical function summary:

function illustrate
sum(a, axis=None) Calculates the sum of array a-related elements, axis integers, or tuples, based on a given axis axis
mean(a, axis=None) Calculates the expectations of array a-related elements, axis integers or tuples based on a given axis
average(a,axis=None,weights=None) Calculate the weighted average of array a-related elements based on a given axis axis axis
std(a, axis=None) Calculate the standard deviation of array a-related elements based on a given axis axis axis
var(a, axis=None) Calculate the variance of array a-related elements based on the given axis axis axis axis
min(a) max(a) Calculate the minimum and maximum values of the elements in array a
argmin(a) argmax(a) Calculate the minimum and maximum values of the elements in array a and lower the scale by one dimension
unravel_index(index, shape) Convert the one-dimensional subscript to a multidimensional subscript according to shape
ptp(a) Calculates the difference between the maximum and minimum values of the elements in array a
median(a) Calculate the median (median) of an element in array a
np.gradient(f) Calculates the gradient of the elements in array f and returns each dimension gradient when f is multidimensional