NumPy, short for Numerical Python, is a powerful library in Python that provides support for large, multi-dimensional arrays and matrices.
Numpy Functions
Mathematical Functions
- numpy.sin()
sin(x, /[, out, where, casting, order, ...])
- Computes the trigonometric sine of each element in the input array, element-wise.
- numpy.cos()
cos(x, /[, out, where, casting, order, ...])
- Computes the trigonometric cosine of each element in the input array, element-wise.
- numpy.tan()
tan(x, /[, out, where, casting, order, ...])
- Computes the trigonometric tangent of each element in the input array, element-wise.
- numpy.arcsin()
arcsin(x, /[, out, where, casting, order, ...])
- Computes the inverse sine (arcsin) of each element in the input array, element-wise.
- numpy.asin()
asin(x, /[, out, where, casting, order, ...])
- Alias for
arcsin()
, computing the inverse sine element-wise. - numpy.arccos()
arccos(x, /[, out, where, casting, order, ...])
- Computes the inverse cosine (arccos) of each element in the input array, element-wise.
- numpy.acos()
acos(x, /[, out, where, casting, order, ...])
- Alias for
arccos()
, computing the inverse cosine element-wise. - numpy.arctan()
arctan(x, /[, out, where, casting, order, ...])
- Computes the inverse tangent (arctan) of each element in the input array, element-wise.
- numpy.atan()
atan(x, /[, out, where, casting, order, ...])
- Alias for
arctan()
, computing the inverse tangent element-wise. - numpy.hypot()
hypot(x1, x2, /[, out, where, casting, ...])
- Computes the hypotenuse of a right triangle given its two perpendicular sides, element-wise.
- numpy.arctan2()
arctan2(x1, x2, /[, out, where, casting, ...])
- Computes the element-wise inverse tangent of
x1/x2
, determining the correct quadrant. - numpy.atan2()
atan2(x1, x2, /[, out, where, casting, ...])
- Alias for
arctan2()
, computing the inverse tangent ofx1/x2
with quadrant correction. - numpy.degrees()
degrees(x, /[, out, where, casting, order, ...])
- Converts input angles from radians to degrees, element-wise.
- numpy.radians()
radians(x, /[, out, where, casting, order, ...])
- Converts input angles from degrees to radians, element-wise.
- numpy.unwrap()
unwrap(p[, discont, axis, period])
- Unwraps a phase angle array by removing discontinuities exceeding a given threshold.
- numpy.deg2rad()
deg2rad(x, /[, out, where, casting, order, ...])
- Converts input angles from degrees to radians, equivalent to
numpy.radians()
. - numpy.rad2deg()
rad2deg(x, /[, out, where, casting, order, ...])
- Converts input angles from radians to degrees, equivalent to
numpy.degrees()
.
Hyperbolic Functions
- numpy.sinh()
sinh(x, /[, out, where, casting, order, ...])
- Computes the hyperbolic sine of each element in the input array, element-wise.
- numpy.cosh()
cosh(x, /[, out, where, casting, order, ...])
- Computes the hyperbolic cosine of each element in the input array, element-wise.
- numpy.tanh()
tanh(x, /[, out, where, casting, order, ...])
- Computes the hyperbolic tangent of each element in the input array, element-wise.
- numpy.arcsinh()
arcsinh(x, /[, out, where, casting, order, ...])
- Computes the inverse hyperbolic sine (arsinh) of each element in the input array, element-wise.
- numpy.asinh()
asinh(x, /[, out, where, casting, order, ...])
- Alias for
arcsinh()
, computing the inverse hyperbolic sine element-wise. - numpy.arccosh()
arccosh(x, /[, out, where, casting, order, ...])
- Computes the inverse hyperbolic cosine (arcosh) of each element in the input array, element-wise.
- numpy.acosh()
acosh(x, /[, out, where, casting, order, ...])
- Alias for
arccosh()
, computing the inverse hyperbolic cosine element-wise. - numpy.arctanh()
arctanh(x, /[, out, where, casting, order, ...])
- Computes the inverse hyperbolic tangent (artanh) of each element in the input array, element-wise.
- numpy.atanh()
atanh(x, /[, out, where, casting, order, ...])
- Alias for
arctanh()
, computing the inverse hyperbolic tangent element-wise.
Rounding Functions
- numpy.round()
round(a[, decimals, out])
- Rounds elements in an array to the specified number of decimal places, using standard rounding rules.
- numpy.around()
around(a[, decimals, out])
- Rounds an array to the given number of decimals, functionally similar to
numpy.round()
. - numpy.rint()
rint(x, /[, out, where, casting, order, ...])
- Rounds each element of the array to the nearest integer, preserving the floating-point type.
- numpy.fix()
fix(x[, out])
- Rounds each element towards zero, effectively truncating the decimal part.
- numpy.floor()
floor(x, /[, out, where, casting, order, ...])
- Computes the largest integer less than or equal to each element, rounding down.
- numpy.ceil()
ceil(x, /[, out, where, casting, order, ...])
- Computes the smallest integer greater than or equal to each element, rounding up.
- numpy.trunc()
trunc(x, /[, out, where, casting, order, ...])
- Truncates each element of the array by removing its fractional part, effectively rounding towards zero.
Sums, Products, Differences Functions
- numpy.prod()
prod(a[, axis, dtype, out, keepdims, ...])
- Computes the product of all elements along the specified axis.
- numpy.sum()
sum(a[, axis, dtype, out, keepdims, ...])
- Computes the sum of all elements along the specified axis.
- numpy.nanprod()
nanprod(a[, axis, dtype, out, keepdims, ...])
- Computes the product of array elements along the specified axis, treating NaN values as 1.
- numpy.nansum()
nansum(a[, axis, dtype, out, keepdims, ...])
- Computes the sum of array elements along the specified axis, treating NaN values as 0.
- numpy.cumsum()
cumsum(a[, axis, dtype, out])
- Computes the cumulative sum along the specified axis.
- numpy.cumprod()
cumprod(a[, axis, dtype, out])
- Computes the cumulative product along the specified axis.
- numpy.nancumsum()
nancumsum(a[, axis, dtype, out])
- Computes the cumulative sum along the specified axis, treating NaN values as 0.
- numpy.nancumprod()
nancumprod(a[, axis, dtype, out])
- Computes the cumulative product along the specified axis, treating NaN values as 1.
- numpy.diff()
diff(a[, n, axis, prepend, append])
- Computes the n-th discrete difference along the specified axis.
- numpy.ediff1d()
ediff1d(ary[, to_end, to_begin])
- Computes the differences between consecutive elements in a flattened array.
- numpy.gradient()
gradient(f, *varargs[, axis, edge_order])
- Computes the gradient of an N-dimensional array, approximating derivatives.
- numpy.cross()
cross(a, b[, axisa, axisb, axisc, axis])
- Computes the cross product of two vectors or arrays of vectors.
- numpy.trapezoid()
trapezoid(y[, x, dx, axis])
- Computes the numerical integral along the specified axis using the trapezoidal rule.
Exponents and Logarithm Functions
- numpy.exp()
exp(x, /[, out, where, casting, order, ...])
- Computes the exponential of all elements in the input array, i.e.,
e^x
, wheree
is Euler’s number. - numpy.expm1()
expm1(x, /[, out, where, casting, order, ...])
- Computes
exp(x) - 1
for all elements in the array, providing higher precision for small values ofx
. - numpy.exp2()
exp2(x, /[, out, where, casting, order, ...])
- Computes
2^x
for each element in the input array. - numpy.log()
log(x, /[, out, where, casting, order, ...])
- Computes the natural logarithm (base
e
) of each element in the input array. - numpy.log10()
log10(x, /[, out, where, casting, order, ...])
- Computes the base-10 logarithm of the input array, element-wise.
- numpy.log2()
log2(x, /[, out, where, casting, order, ...])
- Computes the base-2 logarithm of each element in the input array.
- numpy.log1p()
log1p(x, /[, out, where, casting, order, ...])
- Computes
log(1 + x)
for each element in the input array, providing greater precision for small values ofx
. - numpy.logaddexp()
logaddexp(x1, x2, /[, out, where, casting, ...])
- Computes the logarithm of the sum of exponentials, i.e.,
log(exp(x1) + exp(x2))
, useful for numerical stability. - numpy.logaddexp2()
logaddexp2(x1, x2, /[, out, where, casting, ...])
- Computes the logarithm of the sum of exponentials in base-2, i.e.,
log2(2^x1 + 2^x2)
, useful for numerical stability in binary calculations.
Special Functions
- numpy.i0()
i0(x)
- Computes the modified Bessel function of the first kind, order 0, for each element in the input array.
- numpy.sinc()
sinc(x)
- Computes the normalized sinc function, defined as
sin(πx) / (πx)
, element-wise.
Floating Point Functions
- numpy.signbit()
signbit(x, /[, out, where, casting, order, ...])
- Returns
True
for each element where the sign bit is set (i.e., values less than zero), element-wise. - numpy.copysign()
copysign(x1, x2, /[, out, where, casting, ...])
- Returns a value with the magnitude of
x1
and the sign ofx2
, element-wise. - numpy.frexp()
frexp(x[, out1, out2], / [[, out, where, ...])
- Decomposes each element in
x
into its mantissa and exponent, returning two separate arrays. - numpy.ldexp()
ldexp(x1, x2, /[, out, where, casting, ...])
- Computes
x1 * 2**x2
for each element, effectively performing the inverse offrexp()
. - numpy.nextafter()
nextafter(x1, x2, /[, out, where, casting, ...])
- Returns the next representable floating-point value after
x1
moving towardsx2
, element-wise. - numpy.spacing()
spacing(x, /[, out, where, casting, order, ...])
- Returns the distance between
x
and the nearest adjacent representable floating-point number.
Rational Routine Functions
- numpy.lcm()
lcm(x1, x2, /[, out, where, casting, order, ...])
- Computes the lowest common multiple (LCM) of the absolute values of
x1
andx2
, element-wise. - numpy.gcd()
gcd(x1, x2, /[, out, where, casting, order, ...])
- Computes the greatest common divisor (GCD) of the absolute values of
x1
andx2
, element-wise.
Arithmetic Functions
- numpy.add()
add(x1, x2, /[, out, where, casting, order, ...])
- Adds two arrays element-wise.
- numpy.reciprocal()
reciprocal(x, /[, out, where, casting, ...])
- Computes the reciprocal (1/x) of each element in the input array, element-wise.
- numpy.positive()
positive(x, /[, out, where, casting, order, ...])
- Returns the numerical positive value of each element, element-wise.
- numpy.negative()
negative(x, /[, out, where, casting, order, ...])
- Computes the numerical negative of each element, element-wise.
- numpy.multiply()
multiply(x1, x2, /[, out, where, casting, ...])
- Multiplies two arrays element-wise.
- numpy.divide()
divide(x1, x2, /[, out, where, casting, ...])
- Divides the elements of one array by another, element-wise.
- numpy.power()
power(x1, x2, /[, out, where, casting, ...])
- Computes the exponentiation of elements in
x1
raised to the power of elements inx2
, element-wise. - numpy.pow()
pow(x1, x2, /[, out, where, casting, order, ...])
- Alias for
numpy.power()
, computing exponentiation element-wise. - numpy.subtract()
subtract(x1, x2, /[, out, where, casting, ...])
- Subtracts the elements of one array from another, element-wise.
- numpy.true_divide()
true_divide(x1, x2, /[, out, where, ...])
- Performs element-wise true division, always returning floating-point results.
- numpy.floor_divide()
floor_divide(x1, x2, /[, out, where, ...])
- Performs element-wise division and rounds down the result to the nearest integer.
- numpy.float_power()
float_power(x1, x2, /[, out, where, ...])
- Raises elements in
x1
to powers inx2
, ensuring floating-point precision. - numpy.fmod()
fmod(x1, x2, /[, out, where, casting, ...])
- Computes the remainder of division, using the same sign as
x1
, element-wise. - numpy.mod()
mod(x1, x2, /[, out, where, casting, order, ...])
- Computes the remainder of division, element-wise, following Python’s modulus behavior.
- numpy.modf()
modf(x[, out1, out2], / [[, out, where, ...])
- Splits each element into its fractional and integer parts, element-wise.
- numpy.remainder()
remainder(x1, x2, /[, out, where, casting, ...])
- Computes the remainder of division, using the same sign as
x2
, element-wise. - numpy.divmod()
divmod(x1, x2[, out1, out2], / [[, out, ...])
- Computes the quotient and remainder of element-wise division, returning both results.
Complex Number Functions
- numpy.angle()
angle(z[, deg])
- Returns the phase angle (in radians by default) of a complex number. If
deg=True
, the result is in degrees. - numpy.real()
real(val)
- Extracts the real part of a complex number, element-wise.
- numpy.imag()
imag(val)
- Extracts the imaginary part of a complex number, element-wise.
- numpy.conj()
conj(x, /[, out, where, casting, order, ...])
- Computes the complex conjugate of each element in the array, replacing
a + bj
witha - bj
. - numpy.conjugate()
conjugate(x, /[, out, where, casting, ...])
- Alias for
numpy.conj()
, returning the complex conjugate element-wise.
Extrema Finding Functions
- numpy.maximum()
maximum(x1, x2, /[, out, where, casting, ...])
- Computes the element-wise maximum of two arrays.
- numpy.max()
max(a[, axis, out, keepdims, initial, where])
- Returns the maximum value of an array or along a specified axis.
- numpy.amax()
amax(a[, axis, out, keepdims, initial, where])
- Alias for
numpy.max()
, returning the maximum of an array along an axis. - numpy.fmax()
fmax(x1, x2, /[, out, where, casting, ...])
- Computes the element-wise maximum, ignoring NaN values (NaNs are replaced by non-NaN values).
- numpy.nanmax()
nanmax(a[, axis, out, keepdims, initial, where])
- Computes the maximum of an array along a specified axis, ignoring NaN values.
- numpy.minimum()
minimum(x1, x2, /[, out, where, casting, ...])
- Computes the element-wise minimum of two arrays.
- numpy.min()
min(a[, axis, out, keepdims, initial, where])
- Returns the minimum value of an array or along a specified axis.
- numpy.amin()
amin(a[, axis, out, keepdims, initial, where])
- Alias for
numpy.min()
, returning the minimum of an array along an axis. - numpy.fmin()
fmin(x1, x2, /[, out, where, casting, ...])
- Computes the element-wise minimum, ignoring NaN values (NaNs are replaced by non-NaN values).
- numpy.nanmin()
nanmin(a[, axis, out, keepdims, initial, where])
- Computes the minimum of an array along a specified axis, ignoring NaN values.
Misc Functions
- numpy.convolve()
convolve(a, v[, mode])
- Computes the discrete, linear convolution of two one-dimensional sequences.
- numpy.clip()
clip(a[, a_min, a_max, out, min, max])
- Clips (limits) the values in an array to the interval
[a_min, a_max]
, replacing values outside this range. - numpy.sqrt()
sqrt(x, /[, out, where, casting, order, ...])
- Computes the non-negative square root of each element in the array, element-wise.
- numpy.cbrt()
cbrt(x, /[, out, where, casting, order, ...])
- Computes the cube root of each element in the array, element-wise.
- numpy.square()
square(x, /[, out, where, casting, order, ...])
- Computes the square of each element in the array, element-wise.
- numpy.absolute()
absolute(x, /[, out, where, casting, order, ...])
- Computes the absolute value of each element in the array, element-wise.
- numpy.fabs()
fabs(x, /[, out, where, casting, order, ...])
- Computes the absolute value of each element, similar to
absolute()
but always returning a floating-point array. - numpy.sign()
sign(x, /[, out, where, casting, order, ...])
- Returns
-1
for negative numbers,0
for zero, and1
for positive numbers, element-wise. - numpy.heaviside()
heaviside(x1, x2, /[, out, where, casting, ...])
- Computes the Heaviside step function, returning
0
for negative values,1
for positive values, andx2
for zero. - numpy.nan_to_num()
nan_to_num(x[, copy, nan, posinf, neginf])
- Replaces NaNs with zero, positive infinity with a large finite number, and negative infinity with a small finite number, or user-defined values.
- numpy.real_if_close()
real_if_close(a[, tol])
- Returns the real part of a complex array if all imaginary parts are close to zero within a given tolerance.
- numpy.interp()
interp(x, xp, fp[, left, right, period])
- Performs one-dimensional linear interpolation for monotonically increasing sample points.
- numpy.bitwise_count()
bitwise_count(x, /[, out, where, casting, ...])
- Computes the number of
1
-bits in the absolute value ofx
, element-wise.