NumPy - Functions
The NumPy has a number of built-in functions which can be used for array creation and manipulation. For example, array() function is used to create ndarray object from an array-like object. Along with this, it also contains various functions for mathematical, arithmetical and statistical operations. Below is the list of most commonly used functions of NumPy:
NumPy - Functions
Array Creation/Manipulation Functions
Function | Description |
---|---|
append() | Appends values to the end of an array. |
arange() | Return evenly spaced values within a given interval. |
array() | Returns an array. |
asarray() | Converts the input to an array. |
broadcast() | Produce an object that mimics broadcasting. |
broadcast_to() | Broadcast an array to a new shape. |
concatenate() | Returns a concatenated array along the specified axis. |
empty() | Returns a new array of given shape and type, without initializing entries. |
expand_dims() | Expand the shape of an array. |
eye() | Returns a 2-D array with ones on the diagonal and zeros elsewhere. |
frombuffer() | Interpret a buffer as a 1-dimensional array. |
fromiter() | Create a new 1-dimensional array from an iterable object. |
histogram() | Computes the histogram of a set of data. |
identity() | Returns the identity array. |
linspace() | Returns evenly spaced numbers over a specified interval. |
logspace() | Return numbers spaced evenly on a log scale. |
ndarray.copy() | Return a copy of the array. |
ndarray.flat | A 1-D iterator over the array. |
ndarray.flatten() | Returns a copy of the array collapsed into one dimension. |
meshgrid() | Return coordinate matrices from coordinate vectors. |
ones() | Returns a new array of given shape and type, filled with ones. |
pad() | Pad an array. |
ravel() | Returns a contiguous flattened array. |
reshape() | Gives a new shape to an array without changing its data. |
resize() | Return a new array with the specified shape. |
ndarray.resize() | Changes the shape and size of array in-place. |
ndarray.view() | New view of array with the same data. |
rollaxis() | Roll the specified axis backwards, until it lies in a given position. |
swapaxes() | Interchange two axes of an array. |
stack() | Join a sequence of arrays along a new axis. |
hstack() | Stack arrays in sequence horizontally (column wise). |
vstack() | Stack arrays in sequence vertically (row wise). |
split() | Split an array into multiple sub-arrays. |
hsplit() | Split an array into multiple sub-arrays horizontally (column-wise). |
vsplit() | Split an array into multiple sub-arrays vertically (row-wise). |
insert() | Insert values along the given axis before the given indices. |
delete() | Return a new array with sub-arrays along an axis deleted. |
transpose() | Reverses or permutes the axes of an array. |
ndarray.T | Returns the transposed array. |
squeeze() | Remove axes of length one from an array. |
unique() | Find the unique elements of an array. |
zeros() | Returns a new array of given shape and type, filled with zeros. |
Linear Algebra Functions
Functions | Description |
---|---|
linalg.det() | Compute the determinant of an array. |
linalg.inv() | Compute the determinant of a matrix. |
linalg.solve() | Compute the determinant of a matrix. |
Matrix Functions
Functions | Description |
---|---|
matlib.empty() | Returns a matrix of given shape and type, without initializing entries. |
matlib.eye() | Returns a matrix with ones on the diagonal and zeros elsewhere. |
matlib.identity() | Returns the square identity matrix of given size. |
matlib.ones() | Returns a matrix of given shape and type, filled with ones. |
matlib.rand() | Return a matrix of random values with given shape. |
matlib.randn() | Return a random matrix with data from the “standard normal” distribution. |
matlib.zeros() | Returns a matrix of given shape and type, filled with zeros. |
Statistical Functions
Functions | Description |
---|---|
amax() | Returns the maximum of an array or maximum along an axis. |
amin() | Returns the minimum of an array or minimum along an axis. |
average() | Computes the weighted average along the specified axis. |
mean() | Computes the arithmetic mean along the specified axis. |
median() | Computes the median along the specified axis. |
percentile() | Computes the specified percentile of the data along the specified axis. |
ptp() | Return range of values (maximum - minimum) of an array or along an axis. |
std() | Compute the standard deviation along the specified axis. |
var() | Compute the variance along the specified axis. |
Sorting, Searching & Counting Functions
Functions | Description |
---|---|
argmax() | Returns the indices of the maximum values along an axis. |
argmin() | Returns the indices of the minimum values along an axis. |
argsort() | Returns the indices that would sort an array. |
extract() | Return the elements of an array that satisfy some condition. |
lexsort() | Perform an indirect stable sort using a sequence of keys. |
nonzero() | Return the indices of the elements that are non-zero. |
sort() | Returns a sorted copy of an array. |
where() | Return elements depending on condition. |
Basic Math Functions
Functions | Description |
---|---|
dot() | Returns dot product of two arrays. |
inner() | Returns inner product of two arrays. |
matmul() | Returns matrix product of two arrays. |
sum() | Sum of array elements over a given axis. |
vdot() | Returns dot product of two vectors. |
sqrt() | Returns the square root of the given number. |
cbrt() | Returns the cube root of the given number. |
clip() | Clip (limit) the values in an array. |
diff() | Calculate the n-th discrete difference along the given axis. |
Trigonometric Functions
Functions | Description |
---|---|
sin() | Returns the trigonometric sine of an angle in radians. |
cos() | Returns the trigonometric cosine of an angle in radians. |
tan() | Returns the trigonometric tangent of an angle in radians. |
arcsin() | Returns the arc sine of a value. |
arccos() | Returns the arc cosine of a value. |
arctan() | Returns the arc tangent of a value. |
hypot() | Given the legs of a right triangle, return its hypotenuse. |
degrees() | Convert angles from radians to degrees. |
radians() | Convert angles from degrees to radians. |
rad2deg() | Convert angles from radians to degrees. |
deg2rad() | Convert angles from degrees to radians. |
Hyperbolic Functions
Functions | Description |
---|---|
sinh() | Returns the hyperbolic sine of a value. |
cosh() | Returns the hyperbolic cosine of a value. |
tanh() | Returns the hyperbolic tangent of a value. |
arcsinh() | Returns the inverse hyperbolic sine of a value. |
arccosh() | Returns the inverse hyperbolic cosine of a value. |
arctanh() | Returns the inverse hyperbolic tangent of a value. |
Rounding Functions
Functions | Description |
---|---|
around() | Rounds to the given number of decimals. |
ceil() | Rounds the given number up to the nearest integer. |
floor() | Rounds the given number down to the nearest integer. |
fix() | Round to nearest integer towards zero. |
rint() | Round elements of the array to the nearest integer. |
trunc() | Return the truncated value of the input, element-wise. |
Exponents and Logarithms Functions
Functions | Description |
---|---|
exp() | Returns the exponent of e. |
expm1() | Returns the exponent of e minus 1, i.e., ex-1. |
log() | Returns the natural logarithm of a given number. |
log1p() | Returns the natural logarithm of (1+number), i.e., log(1+number). |
log2() | Returns the base-2 logarithm of a given number. |
log10() | Returns the base-10 logarithm of a given number. |
Arithmetic Functions
Functions | Description |
---|---|
add() | Add arguments element-wise. |
subtract() | Subtract arguments, element-wise. |
multiply() | Multiply arguments element-wise. |
divide() | Returns a true division of the inputs, element-wise. |
true_divide() | Returns a true division of the inputs, element-wise. |
floor_divide() | Return the largest integer smaller or equal to the division of the inputs. |
power() | Returns base raised to the power of exponent. |
fmod() | Return element-wise remainder of division. |
mod() | Return element-wise remainder of division. |
remainder() | Return element-wise remainder of division. |
reciprocal() | Return the reciprocal of the argument, element-wise. |
Handling Complex Numbers
Functions | Description |
---|---|
angle() | Return the angle of the complex argument. |
real() | Return the real part of the complex argument. |
imag() | Return the imaginary part of the complex argument. |
conj() | Return the complex conjugate, element-wise. |
conjugate() | Return the complex conjugate, element-wise. |
Input & Output Functions
Functions | Description |
---|---|
save() | Save an array to a binary file in NumPy .npy format. |
load() | Load arrays from .npy file. |
savetxt() | Save an array to a text file. |
loadtxt() | Load data from a text file. |
ndarray.tolist() | Return the array as a (nested) Python list. |