NumPy is a powerful library in Python that provides a wide range of functions to perform mathematical and logical operations on arrays. It is especially useful for scientific computing. This page introduces various NumPy functions categorized into different groups, such as trigonometric, hyperbolic, rounding, sums/products/differences, exponents/logarithms, arithmetic operations, rational routines, handling complex numbers, extrema finding, and miscellaneous functions. Each function is explained in simple terms, and links are provided for detailed guides and official documentation.
Python NumPy Functions
NumPy – Trigonometric Functions
Function |
Description |
sin() |
Computes the trigonometric sine of each element in the array. |
cos() |
Computes the trigonometric cosine of each element in the array. |
tan() |
Computes the trigonometric tangent of each element in the array. |
arcsin() |
Computes the inverse sine of each element in the array. |
arccos() |
Computes the inverse cosine of each element in the array. |
arctan() |
Computes the inverse tangent of each element in the array. |
arctan2() |
Computes the element-wise inverse tangent of y/x. |
hypot() |
Computes the hypotenuse given the lengths of the two legs. |
degrees() |
Converts angles from radians to degrees. |
radians() |
Converts angles from degrees to radians. |
unwrap() |
Unwraps by changing large differences between elements to their 2π complement. |
deg2rad() |
Converts angles from degrees to radians. |
rad2deg() |
Converts angles from radians to degrees. |
NumPy – Hyperbolic Functions
Function |
Description |
sinh() |
Computes the hyperbolic sine of each element in the array. |
cosh() |
Computes the hyperbolic cosine of each element in the array. |
tanh() |
Computes the hyperbolic tangent of each element in the array. |
arcsinh() |
Computes the inverse hyperbolic sine of each element in the array. |
NumPy – Rounding Functions
Function |
Description |
round() |
Rounds each element in the array to the nearest integer. |
around() |
Rounds each element in the array to the given number of decimals. |
rint() |
Rounds each element in the array to the nearest integer, preserving the sign. |
fix() |
Rounds each element in the array towards zero. |
floor() |
Rounds each element in the array down to the nearest integer. |
ceil() |
Rounds each element in the array up to the nearest integer. |
trunc() |
Truncates each element in the array towards zero. |
NumPy – Sums, Products, Differences
Function |
Description |
prod() |
Returns the product of array elements over a given axis. |
sum() |
Returns the sum of array elements over a given axis. |
nanprod() |
Returns the product of array elements over a given axis treating Not a Numbers (NaNs) as ones. |
nansum() |
Returns the sum of array elements over a given axis treating Not a Numbers (NaNs) as zeros. |
cumprod() |
Returns the cumulative product of array elements over a given axis. |
diff() |
Calculates the n-th discrete difference along a given axis. |
gradient() |
Returns the gradient of an N-dimensional array. |
cross() |
Returns the cross product of two arrays. |
trapz() |
Integrates along the given axis using the trapezoidal rule. |
NumPy – Exponents and Logarithms
Function |
Description |
exp() |
Calculates the exponential of all elements in the array. |
expm1() |
Calculates exp(x) – 1 for all elements in the array. |
exp2() |
Calculates 2 raised to the power of all elements in the array. |
logaddexp() |
Calculates the log of the sum of exponentiations of the inputs. |
log10() |
Calculates the base-10 logarithm of all elements in the array. |
log1p() |
Calculates the natural logarithm of 1 plus each element in the array. |
log() |
Calculates the natural logarithm of all elements in the array. |
NumPy – Arithmetic Operations
Function |
Description |
add() |
Adds arguments element-wise. |
reciprocal() |
Returns the reciprocal of each element in the array. |
positive() |
Returns an array with the positive value of each element. |
negative() |
Returns an array with the negative value of each element. |
multiply() |
Multiplies arguments element-wise. |
divide() |
Divides arguments element-wise. |
power() |
Raises each element in the array to the specified power. |
pow() |
Raises each element in the array to the specified power. |
mod() |
Returns the element-wise remainder of division. |
remainder() |
Returns the element-wise remainder of division. |
divmod() |
Returns element-wise quotient and remainder simultaneously. |
NumPy – Rational Routines
Function |
Description |
lcm() |
Calculates the least common multiple of array elements. |
gcd() |
Calculates the greatest common divisor of array elements. |
NumPy – Handling Complex Numbers
Function |
Description |
angle() |
Returns the angle of the complex argument. |
real() |
Returns the real part of the complex argument. |
imag() |
Returns the imaginary part of the complex argument. |
conj() |
Returns the complex conjugate of each element. |
conjugate() |
Returns the complex conjugate of each element. |
NumPy – Extrema Finding
Function |
Description |
maximum() |
Compares two arrays and returns a new array containing the element-wise maxima. |
max() |
Returns the maximum element of an array or maximum along an axis. |
min() |
Returns the minimum element of an array or minimum along an axis. |
minimum() |
Compares two arrays and returns a new array containing the element-wise minima. |
NumPy – Miscellaneous
Function |
Description |
absolute() |
Returns the absolute value of each element in the array. |
convolve() |
Returns the discrete, linear convolution of two one-dimensional sequences. |
clip() |
Clips (limits) the values in an array. |
sqrt() |
Computes the square root of each element in the array. |
cbrt() |
Computes the cube root of each element in the array. |
square() |
Computes the square of each element in the array. |
fabs() |
Computes the absolute value element-wise. |
sign() |
Returns an element-wise indication of the sign of a number. |
nan_to_num() |
Replaces NaN with zero and infinity with large finite numbers. |
real_if_close() |
Returns a real array if complex parts are close to zero. |
interp() |
Performs linear interpolation. |
For more detailed information on each function, refer to the official NumPy documentation.