Numpy functions include np.sin(), np.cos(), np.tan(), np.exp(), np.log(), np.log10(), np.log2() , np.mean(), np.median(), np.var(), np.std(), np.max(), np.min(), np.percentile(), etc.
The operating system for this tutorial: Windows 10 system, Python version 3.11.4, DELL G3 computer.
NumPy is an important library for numerical calculations in Python. It provides a rich set of mathematical, logical, statistical and linear algebra functions. The following are some commonly used functions in NumPy and their application examples:
1. Mathematical functions:
np.sin(), np.cos(), np. tan(): Calculates the sine, cosine, and tangent values of each element in the array.
np.exp(): Calculate the exponent value of each element in the array.
np.log(), np.log10(), np.log2(): Calculate the natural logarithm, the logarithm with base 10, and the logarithm with base 2 of each element in the array.
import numpy as np arr = np.array([1, 2, 3]) print(np.sin(arr)) print(np.exp(arr)) print(np.log10(arr))
2. Statistical functions:
np.mean(), np.median(), np.var(), np.std(): calculated separately The mean, median, variance, and standard deviation of an array.
np.max(), np.min(): Calculate the maximum and minimum values of the array.
np.percentile(): Calculate the percentile of an array.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(np.mean(arr)) print(np.max(arr)) print(np.percentile(arr, 50))
3. Logical functions:
np.logical_and(), np.logical_or(), np.logical_not(): perform logical AND, logical OR and respectively Logical NOT operation.
np.all(), np.any(): Determine whether all elements in the array are True, or whether any element is True.
import numpy as np arr1 = np.array([True, True, False]) arr2 = np.array([False, True, False]) print(np.logical_and(arr1, arr2)) print(np.any(arr1))
4. Linear algebra function:
np.dot(): Calculate the dot product of two arrays.
np.linalg.inv(): Calculate the inverse matrix of a matrix.
np.linalg.det(): Calculate the determinant value of the matrix.
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) print(np.dot(arr1, arr2)) print(np.linalg.inv(arr1)) print(np.linalg.det(arr1))
These are just one of the commonly used functions in NumPy. It also provides many other functions, such as image processing functions, numerical integration functions, discrete Fourier transform functions, etc. These functions provide very powerful tools for numerical calculations, making NumPy an indispensable part of the field of scientific computing. Hopefully these examples will help you better understand functions in NumPy.
The above is the detailed content of What are the numpy functions?. For more information, please follow other related articles on the PHP Chinese website!