NumPy (Numerical Python) is an open source Python scientific computing library that provides multi-dimensional array objects and tools for operating on arrays. It is one of the core libraries of the Python data science ecosystem and is widely used in fields such as scientific computing, data analysis, and machine learning. This article will analyze the commonly used functions in the NumPy library one by one, including array creation, array operations, mathematical functions, statistical functions, linear algebra, etc., and provide specific code examples.
1.1 numpy.array(): Create an array from a list or tuple.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) # 输出:[1 2 3 4 5]
1.2 numpy.zeros(): Creates an all-zero array of specified dimensions.
import numpy as np arr = np.zeros((3, 4)) print(arr) """ 输出: [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] """
1.3 numpy.ones(): Create an all-one array of specified dimensions.
import numpy as np arr = np.ones((2, 3)) print(arr) """ 输出: [[1. 1. 1.] [1. 1. 1.]] """
1.4 numpy.arange(): Create an arithmetic array.
import numpy as np arr = np.arange(0, 10, 2) print(arr) # 输出:[0 2 4 6 8]
2.1 reshape(): Change the shape of the array.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) new_arr = arr.reshape((3, 2)) print(new_arr) """ 输出: [[1 2] [3 4] [5 6]] """
2.2 indexing and slicing: operate arrays through indexing and slicing.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr[2]) # 输出:3 print(arr[1:4]) # 输出:[2 3 4] print(arr[:3]) # 输出:[1 2 3] print(arr[-3:]) # 输出:[3 4 5]
2.3 concatenate(): Concatenate two or more arrays.
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.concatenate((arr1, arr2)) print(arr) # 输出:[1 2 3 4 5 6]
2.4 transpose(): Transpose the array.
import numpy as np arr = np.array([[1, 2], [3, 4]]) new_arr = np.transpose(arr) print(new_arr) """ 输出: [[1 3] [2 4]] """
3.1 np.mean(): Calculate the average of an array.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) mean = np.mean(arr) print(mean) # 输出:3.0
3.2 np.sin(): Calculate the sine value of the array element.
import numpy as np arr = np.array([0, np.pi/2, np.pi]) sin = np.sin(arr) print(sin) # 输出:[0. 1. 1.2246468e-16]
3.3 np.exp(): Perform exponential operation on array elements.
import numpy as np arr = np.array([1, 2, 3]) exp = np.exp(arr) print(exp) # 输出:[ 2.71828183 7.3890561 20.08553692]
4.1 np.max(): Calculate the maximum value of the array.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) max_value = np.max(arr) print(max_value) # 输出:5
4.2 np.min(): Calculate the minimum value of the array.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) min_value = np.min(arr) print(min_value) # 输出:1
4.3 np.median(): Calculate the median of the array.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) median = np.median(arr) print(median) # 输出:3.0
4.4 np.var(): Calculate the variance of the array.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) variance = np.var(arr) print(variance) # 输出:2.0
5.1 np.dot(): Calculate the dot product of two arrays.
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) dot_product = np.dot(arr1, arr2) print(dot_product) """ 输出: [[19 22] [43 50]] """
5.2 np.linalg.inv(): Calculate the inverse of a matrix.
import numpy as np arr = np.array([[1, 2], [3, 4]]) inverse = np.linalg.inv(arr) print(inverse) """ 输出: [[-2. 1. ] [ 1.5 -0.5]] """
The above are only part of the functions in the NumPy library. By understanding how to use these common functions, we can use NumPy more efficiently to perform computing tasks such as array operations, mathematical operations, statistical analysis, and linear algebra. At the same time, by in-depth study of the relevant documents of the NumPy library, we can discover more powerful functions and functions to provide strong support for our scientific computing work.
The above is the detailed content of A complete guide to parsing NumPy functions. For more information, please follow other related articles on the PHP Chinese website!