Introduction

In NumPy, in addition to basic arithmetic operations, multi-dimensional arrays also have some very useful functions built-in, which can speed up our scientific calculations.

Simple function

Let's take a look at the more common arithmetic functions. Before using, we first construct an array:

arr = np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Calculate the root of the elements in the array:

np.sqrt(arr)
array([0.    , 1.    , 1.4142, 1.7321, 2.    , 2.2361, 2.4495, 2.6458,
       2.8284, 3.    ])

Exponential function with natural constant e as the base:

np.exp(arr)
array([   1.    ,    2.7183,    7.3891,   20.0855,   54.5982,  148.4132,
        403.4288, 1096.6332, 2980.958 , 8103.0839])

Take the maximum of the two arrays to form a new array:

x = np.random.randn(8)
y = np.random.randn(8)
x,y
(array([-2.3594, -0.1995, -1.542 , -0.9707, -1.307 ,  0.2863,  0.378 ,
        -0.7539]),
 array([ 0.3313,  1.3497,  0.0699,  0.2467, -0.0119,  1.0048,  1.3272,
        -0.9193]))
np.maximum(x, y)
array([ 0.3313,  1.3497,  0.0699,  0.2467, -0.0119,  1.0048,  1.3272,
       -0.7539])

Return the decimal and integer parts of an array of floating-point numbers:

arr = np.random.randn(7) * 5
array([-7.7455,  0.1109,  3.7918, -3.3026,  4.3129, -0.0502,  0.25  ])
remainder, whole_part = np.modf(arr)
(array([-0.7455,  0.1109,  0.7918, -0.3026,  0.3129, -0.0502,  0.25  ]),
 array([-7.,  0.,  3., -3.,  4., -0.,  0.]))

Vectorized array operations

If you want to perform operations between arrays, the common method is to loop through them, but this efficiency will be relatively low. So Numpy provides a method for data processing between arrays.

Let me first explain the function np.meshgrid, which is used to quickly generate a grid point coordinate matrix.

First look at a piece of code for coordinate points:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[0, 1, 2], [0, 1, 2]])
y = np.array([[0, 0, 0], [1, 1, 1]])


plt.plot(x, y,
         color='green',
         marker='.',
         linestyle='')
plt.grid(True)
plt.show()

The X above is a two-dimensional array, which represents the position of the coordinate point on the X axis.

Y is also a two-dimensional array, which represents the position of the coordinate point on the Y axis.

Take a look at the image drawn:

The above drawing is the 6 coordinate points that are combined using X and Y matrices.

The above two-dimensional arrays of X and Y are manually input. If there are a large number of points on the coordinates, manual input is definitely not advisable.

So there is the function np.meshgrid. This function can accept two one-dimensional arrays, and then generate a two-dimensional X, Y coordinate matrix.

The above example can be rewritten as:

x = np.array([0,1,2])
y = np.array([0,1])

xs, ys = np.meshgrid(x, y)
xs,ys
(array([[0, 1, 2],
        [0, 1, 2]]), 
 array([[0, 0, 0],
        [1, 1, 1]]))

You can see that the generated xs and ys are the same as the manual input.

With the grid coordinates, we can calculate some data based on the grid values, for example: $sqrt(x^2+y^2)$, we don’t need all the data in the variable matrix, just use the array directly for calculations To:

np.sqrt(xs ** 2 + ys ** 2)

result:

array([[0.        , 1.        , 2.        ],
       [1.        , 1.41421356, 2.23606798]])

Because xs and ys themselves are 2 3 matrices, the result is also a 2 3 matrix.

Conditional logic expression

We can use conditional logic expressions when constructing arrays:

xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])

result = [(x if c else y)
          for x, y, c in zip(xarr, yarr, cond)]
result
[1.1, 2.2, 1.3, 1.4, 2.5]

To make it simpler, we can use the where statement:

result = np.where(cond, xarr, yarr)
result
array([1.1, 2.2, 1.3, 1.4, 2.5])

We can also modify the value of the array according to the conditions of where:

arr = np.random.randn(4, 4)
arr
array([[ 0.7953,  0.1181, -0.7485,  0.585 ],
       [ 0.1527, -1.5657, -0.5625, -0.0327],
       [-0.929 , -0.4826, -0.0363,  1.0954],
       [ 0.9809, -0.5895,  1.5817, -0.5287]])

Above we have constructed a 4 * 4 array.

We can compare the data in where, if it is greater than 0, modify the data to 2; if it is less than 0, modify the data to -2:

np.where(arr > 0, 2, -2)
array([[ 2,  2, -2,  2],
       [ 2, -2, -2, -2],
       [-2, -2, -2,  2],
       [ 2, -2,  2, -2]])

statistical methods

Numpy provides statistical methods such as mean and sum:

arr = np.random.randn(5, 4)
arr
arr.mean()
np.mean(arr)
arr.sum()

Statistics can also be based on dimensions:

arr.mean(axis=1)
arr.sum(axis=0)

cumsum calculates cumulatively:

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7])
arr.cumsum()
array([ 0,  1,  3,  6, 10, 15, 21, 28])

cumprod performs cumulative multiplication calculation:

arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
arr
arr.cumsum(axis=0)
array([[ 0,  1,  2],
       [ 3,  5,  7],
       [ 9, 12, 15]])
arr.cumprod(axis=1)
array([[  0,   0,   0],
       [  3,  12,  60],
       [  6,  42, 336]])

Boolean array

any is used to test whether one or more True exists in the array, while all checks whether all values in the array are True:

bools = np.array([False, False, True, False])
bools.any()
True
bools.all()
False

Sort

Use sort to sort the array, in addition to ordinary sorting, you can also sort according to a specific axis:

arr = np.random.randn(6)
arr.sort()
array([-2.5579, -1.2943, -0.2972, -0.1516,  0.0765,  0.1608])
arr = np.random.randn(5, 3)
arr
arr.sort(1)
arr
array([[-0.8852, -0.4936, -0.1875],
       [-0.3507, -0.1154,  0.0447],
       [-1.1512, -0.8978,  0.8909],
       [-2.6123, -0.8671,  1.1413],
       [-0.437 ,  0.3475,  0.3836]])

sort(1) refers to sorting according to the second axis.

file

You can easily write the array to and read from the file:

arr = np.arange(10)
np.save('some_array', arr)

Will store the array in some_array.npy file, we can read it like this:

np.load('some_array.npy')

You can also store multiple arrays in an uncompressed manner:

np.savez('array_archive.npz', a=arr, b=arr)

Read:

arch = np.load('array_archive.npz')
arch['b']

If you want to compress, you can do this:

np.savez_compressed('arrays_compressed.npz', a=arr, b=arr)

Linear algebra

If we use ordinary arithmetic operators to perform matrix operations, it is just a simple arithmetic operation of the corresponding elements in the array. If we want to do multiplication between matrices, we can use dot.

A 2 3 matrix dot and a 3 2 matrix, and finally a 2 * 2 matrix.

x = np.array([[1., 2., 3.], [4., 5., 6.]])
y = np.array([[6., 23.], [-1, 7], [8, 9]])
x
y
x.dot(y)
array([[ 28.,  64.],
       [ 67., 181.]])

Or it can be written like this:

np.dot(x, y)
array([[ 28.,  64.],
       [ 67., 181.]])

You can also use the @ symbol:

x @ y
array([[ 28.,  64.],
       [ 67., 181.]])

Let's take a look at what operations are available:

Product operation:

Operatordescription
dot(a, b[, out])Matrix dot product
linalg.multi_dot(arrays, *[, out])Dot product of multiple matrices
vdot(a, b)Vector dot product
inner(a, b)Inner product of two arrays
outer(a, b[, out])Outer product of two vectors
matmul(x1, x2, /[, out, casting, order, …])Product of corresponding bits of two matrices
tensordot(a, b[, axes])Calculate the tensor dot product along the specified axis
einsum(subscripts, *operands[, out, dtype, …]) Einstein summation agreement
einsum_path(subscripts, *operands[, optimize])By considering the creation of the intermediate array, the lowest cost contraction order of the einsum expression is evaluated.
linalg.matrix_power(a, n)Matrix exponentiation
kron(a, b)Kronecker product of matrices

Decomposition operation:

Operatordescription
linalg.cholesky(a) Cholesky decomposition
linalg.qr(a[, mode])Calculate the qr factorization of a matrix
linalg.svd(a[, full_matrices, compute_uv, …])Singular value decomposition

Eigenvalue and eigenvector:

operatingdescription
linalg.eig(a)Calculate the eigenvalues and right eigenvectors of the square matrix.
linalg.eigh(a[, UPLO])Returns the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or real symmetric matrix.
linalg.eigvals(a)Calculate the eigenvalues of the general matrix.
linalg.eigvalsh(a[, UPLO])Calculate the eigenvalues of complex Hermitian (conjugate symmetric) or real symmetric matrices.

Reference value:

operatingdescription
linalg.norm(x[, ord, axis, keepdims])Matrix or vector norm
linalg.cond(x[, p])Compute the condition number of a matrix.
linalg.det(a)Matrix determinant
linalg.matrix_rank(M[, tol, hermitian])Use the SVD method to return the matrix rank of the array
linalg.slogdet(a)Calculate the sign and (natural) logarithm of the array determinant.
trace(a[, offset, axis1, axis2, dtype, out])Returns the sum along the diagonal of the array.

Solve and reverse:

operatingdescription
linalg.solve(a, b)Solve linear matrix equations or linear scalar equations.
linalg.tensorsolve(a, b[, axes])Solve the tensor equation'ax = b'for x.
linalg.lstsq(a, b[, rcond])Return the least squares solution to a linear matrix equation
linalg.inv(a)Calculate the (multiplicative) inverse of the matrix.
linalg.pinv(a[, rcond, hermitian])Calculate the (Moore-Penrose) pseudo-inverse of the matrix.
linalg.tensorinv(a[, ind])Calculate the "inverse" of an N-dimensional array.

random number

Many times we need to generate random numbers. The generation of random numbers in NumPy is very simple:

samples = np.random.normal(size=(4, 4))
samples
array([[-2.0016, -0.3718,  1.669 , -0.4386],
       [-0.5397,  0.477 ,  3.2489, -1.0212],
       [-0.5771,  0.1241,  0.3026,  0.5238],
       [ 0.0009,  1.3438, -0.7135, -0.8312]])

The above uses normal to get a 4×4 sample array of standard normal distribution.

Using np.random is much faster than using the random number generator that comes with Python.

np.random can specify the seed for generating random numbers:

np.random.seed(1234)

The data generation function of numpy.random uses a global random seed. To avoid global state, you can use numpy.random.RandomState to create a random number generator isolated from others:

rng = np.random.RandomState(1234)
rng.randn(10)
This article has been included in http://www.flydean.com/10-python-numpy-func/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com