Python numpy Commonly used function summary

  • 2020-06-15 09:25:58
  • OfStack

What is Numpy

Before I introduce numpy to you, Let me give you the basic concept of python.

Python is a high-level, dynamic, multigeneric programming language. Many times the Python code looks like pseudocode 1, so you can implement a very powerful idea with very few lines of highly readable code.

numpy is very simple. Numpy is a scientific calculation library of Python, which provides the function of matrix operation. It is generally used since Scipy and matplotlib1. list already provides a matrixlike representation, but numpy gives us more functions.

An array of

Array common functions

1.where()
Returns the index value of the array, conditional
2.take(a,index)
Value from the array a by index index
3.linspace(a,b,N)
Returns an array evenly distributed in the range (a,b) with N elements
4.a.fill()
Populates all elements of an array with the specified value
5.diff(a)
Returns an array of the differences between the adjacent elements of the array a
6.sign(a)
Returns the plus or minus sign for each element of the array a
7.piecewise(a,[condlist],[funclist])
The array a returns the result of the corresponding element according to the Boolean condition condlist
8.a.argmax(),a.argmin()
Returns the index of the largest and smallest elements of a

Changing array dimensions

a.ravel(),a.flatten():
Flatten the array a to a 1-dimensional array
a.shape=(m,n),a.reshape(m,n):
Convert the array a to m*n
3.a.transpose,a.T
Transpose a

Array combination

1.hstack((a,b)),concatenate((a,b),axis=1)
Combine the arrays a and b horizontally
2.vstack((a,b)),concatenate((a,b),axis=0)
Combine the arrays a and b vertically
3.row_stack((a,b))
Combine the arrays a and b in the line direction
4.column_stack((a,b))
Combine the arrays a,b by column direction

An array of segmentation

1.split(a,n,axis=0),vsplit(a,n)
Divide the array a into n arrays vertically
2.split(a,n,axis=1),hsplit(a,n)
The a array is split horizontally into n arrays

Array pruning and compression

1.a.clip(m,n)
Set the array a to a range of (m,n), elements greater than n in the array to n, elements less than m to m
2.a.compress()
Returns an array filtered by a given condition

An array of attributes

1.a.dtype
The data type of the array a
2.a.shape
The dimensions of a array
3.a.ndim
The dimension of the array a
4.a.size
The total number of elements in the array a
5.a.itemsize
The number of bytes in memory of the elements of the array a
6.a.nbytes
The memory footprint of the entire array a
7.a.astype(int)
Convert the a array to type int

An array of computing

1.average(a,weights=v)
The array a is weighted by v
2.mean(a),max(a),min(a),middle(a),var(a),std(a)
The mean, maximum, minimum, median, variance, standard deviation of the array a
3.a.prod()
The product of all the elements of the array a
4.a.cumprod()
The cumulative product of the elements of the array a
5.cov(a,b),corrcoef(a,b)
The covariance and correlation coefficient of the arrays a and b
6.a.diagonal()
View the elements on the diagonal of the matrix a
7.a.trace()
Calculate the trace of a, the sum of the diagonal elements

conclusion


Related articles: