Below is an expanded and detailed breakdown of all the features and methods in NumPy, organized into broader categories for clarity. I’ll ensure that no important functionality is missed.
NumPy provides various functions to create arrays, ranging from simple arrays to more complex patterns and shapes.
numpy.array(object, dtype=None)
: Converts input to an ndarray.numpy.zeros(shape, dtype=float)
: Create an array filled with zeros.numpy.ones(shape, dtype=float)
: Create an array filled with ones.numpy.empty(shape, dtype=float)
: Create an uninitialized array (filled with random data initially).
numpy.arange([start,] stop, [step,], dtype=None)
: Return evenly spaced values within a given interval.numpy.linspace(start, stop, num=50, endpoint=True, retstep=False)
: Return evenly spaced numbers over a specified interval.
numpy.eye(N, M=None, k=0, dtype=float)
: Return a 2-D array with ones on the diagonal and zeros elsewhere.numpy.identity(n, dtype=float)
: Return the identity array.
numpy.full(shape, fill_value, dtype=None)
: Return a new array of given shape and type, filled withfill_value
.numpy.full_like(a, fill_value, dtype=None)
: Return a full array with the same shape and type as a given array.
numpy.random.rand(d0, d1, ..., dn)
: Generate random values in a given shape (uniform distribution between 0 and 1).numpy.random.randn(d0, d1, ..., dn)
: Generate random numbers from the standard normal distribution.numpy.random.randint(low, high=None, size=None)
: Return random integers from a specified range.
Each ndarray object has a number of attributes that provide information about the array's structure and data.
ndarray.shape
: Shape (dimensions) of the array.ndarray.ndim
: Number of dimensions (axes) of the array.ndarray.size
: Total number of elements in the array.ndarray.dtype
: Data type of the array elements.ndarray.itemsize
: Size in bytes of each element in the array.ndarray.nbytes
: Total number of bytes consumed by the elements of the array.ndarray.T
: Transpose of the array (equivalent tondarray.transpose()
).
Accessing and modifying arrays using indices, slices, and loops.
- Standard indexing:
a[i]
,a[i, j]
- Boolean indexing:
a[a > 0]
- Fancy indexing: Use arrays of indices to access multiple array elements.
a[start:stop:step]
: Works similarly to Python lists, allowing subsetting.a[::]
: Slice all elements across axes.
numpy.nditer()
: Efficient multi-dimensional iterator over arrays.ndarray.flat
: Return a 1-D iterator over the array.
numpy.reshape(a, newshape)
: Give a new shape to an array without changing its data.numpy.ravel(a)
: Flatten an array into a 1D array.numpy.flatten()
: Returns a copy of the array flattened to 1D.numpy.transpose(a, axes=None)
: Permute the dimensions of an array.numpy.swapaxes(a, axis1, axis2)
: Interchange two axes of an array.
numpy.concatenate((a1, a2, ...), axis=0)
: Join a sequence of arrays along an existing axis.numpy.hstack()
: Stack arrays horizontally (column-wise).numpy.vstack()
: Stack arrays vertically (row-wise).numpy.split(ary, indices_or_sections)
: Split an array into multiple sub-arrays.
numpy.add(a, b)
,numpy.subtract(a, b)
,numpy.multiply(a, b)
,numpy.divide(a, b)
: Basic element-wise operations.numpy.mod(a, b)
: Element-wise modulus.numpy.power(a, b)
: Raise elements ofa
to the powerb
.numpy.exp(a)
,numpy.log(a)
,numpy.log10(a)
: Exponential and logarithmic functions.
numpy.sin(a)
,numpy.cos(a)
,numpy.tan(a)
: Element-wise trigonometric functions.numpy.arcsin(a)
,numpy.arccos(a)
,numpy.arctan(a)
: Inverse trigonometric functions.
numpy.mean(a)
: Mean of array elements.numpy.median(a)
: Median of array elements.numpy.var(a)
,numpy.std(a)
: Variance and standard deviation.numpy.min(a)
,numpy.max(a)
: Minimum and maximum of array elements.
numpy.sum(a)
: Sum of array elements.numpy.cumsum(a)
: Cumulative sum of elements.numpy.prod(a)
: Product of array elements.numpy.cumprod(a)
: Cumulative product of elements.
numpy.dot(a, b)
: Dot product of two arrays.numpy.linalg.inv(a)
: Inverse of a matrix.numpy.linalg.det(a)
: Determinant of a matrix.numpy.linalg.eig(a)
: Eigenvalues and eigenvectors.numpy.linalg.norm(a)
: Matrix or vector norm.
(Part of numpy.random
)
numpy.random.seed(seed)
: Seed the random number generator.numpy.random.rand(d0, d1, ..., dn)
: Random values from a uniform distribution.numpy.random.normal(loc=0.0, scale=1.0, size=None)
: Draw samples from a normal distribution.numpy.random.randint(low, high=None, size=None, dtype='l')
: Random integers.
Broadcasting allows arrays with different shapes to be used together in arithmetic operations.
- Automatic Broadcasting: NumPy automatically broadcasts smaller arrays to match the shape of larger arrays during operations like addition or multiplication.
numpy.sort(a, axis=-1)
: Sort an array along a given axis.numpy.argsort(a, axis=-1)
: Indices that would sort the array.numpy.lexsort()
: Indirect stable sort on multiple keys.
numpy.where(condition[, x, y])
: Return elements fromx
ory
depending on condition.numpy.nonzero(a)
: Return indices of non-zero elements.numpy.argmax(a)
,numpy.argmin(a)
: Indices of the maximum and minimum values.numpy.searchsorted(a, v)
: Find indices where elements should be inserted to maintain order.
(Part of numpy.lib
)
numpy.unique(a)
: Find unique elements in an array.numpy.intersect1d(ar1, ar2)
: Find intersection of two arrays.numpy.union1d(ar1, ar2)
: Find union of two arrays.numpy.setdiff1d(ar1, ar2)
: Set difference of two arrays.numpy.in1d(ar1, ar2)
: Test if elements of one array are in another.
numpy.isnan(a)
: Return a boolean array indicating whether values areNaN
.numpy.isfinite(a)
: Return a boolean array indicating whether values are finite.numpy.nan_to_num(a)
: Replace NaN with zero and infinity with large finite numbers.
numpy.save(file, arr)
: Save an array to a binary file in.npy
format.numpy.load(file)
: Load an array from a binary.npy
file.numpy.savetxt(fname, X)
: Save an array to a text file.numpy.loadtxt(fname)
: Load data from a text file.
numpy.bitwise_and(x1, x2)
,numpy.bitwise_or(x1, x2)
,numpy.bitwise_xor(x1, x2)
: Bitwise operations on integers.numpy.invert(x)
: Bitwise NOT of an array.numpy.left_shift(x1, x2)
,numpy.right_shift(x1, x2)
: Bitwise left or right shift.
. Data Types and Casting
numpy.astype(dtype)
: Cast array to a specified data type.numpy.dtype()
: Get or specify the data type of an array.numpy.can_cast(from_, to)
: Check if one data type can be cast to another.numpy.iscomplex()
,numpy.isreal()
: Check if array elements are complex or real.
ndarray.flags
: Information about the memory layout of the array.numpy.copy(a)
: Create a copy of the array.numpy.memmap()
: Create a memory-mapped array for large datasets.