np.array()
Creates a NumPy array from a list, tuple, or other iterable.
np.array(34)
Creates a zero-dimensional (scalar) NumPy array.
.ndim
Returns the number of dimensions of an array.
.shape
Returns the shape of an array.
(rows, columns) for a 2D array.
np.arange()
Creates evenly spaced values within a range.
np.linspace()
Creates evenly spaced numbers between two values.
np.logspace()
Creates numbers spaced evenly on a logarithmic scale.
np.zeros()
Creates an array filled with zeros.
np.ones()
Creates an array filled with ones.
np.full()
Creates an array filled with a specified value.
np.empty()
Creates an uninitialized array.
.dtype
Returns the data type of array elements.
astype()
Converts an array to another data type.
.size
Returns the total number of elements.
.itemsize
Returns the memory occupied by one element.
Array Manipulation
reshape()
Changes the shape of an array.
ravel()
Converts an array into a 1D view.
flatten()
Returns a flattened copy of an array.
transpose()
Swaps rows and columns.
swapaxes()
Swaps two specified axes.
Indexing, Slicing & Iteration
Access Elements
Accesses elements using their index.Slicing
Extracts a portion of an array.start is included, stop is excluded.
Reverse Slicing
Returns elements in reverse order.[::-1] is the easiest way to reverse an array.
Multidimensional Slicing
Slices rows and columns together.array[rows, columns].
Specific Rows and Columns
Accesses complete rows or columns.np.take()
Selects elements using index positions.
np.nditer()
Efficiently iterates through every element.
np.ndenumerate()
Iterates through elements with their indices.
Arithmetic Operations on NumPy Arrays
Performs mathematical operations element-wise on arrays.Universal Functions (ufuncs)
Built-in NumPy functions that operate element-wise on arrays.np.sin()
Computes the sine of each element (in radians).
Broadcasting
Allows arrays of different shapes to perform arithmetic automatically.Vectorization
Performs operations on entire arrays without using loops.Joining Arrays
Combines multiple arrays into a single array.np.concatenate()
Joins arrays along an existing axis.
axis=1 to join column-wise.
np.vstack()
Stacks arrays vertically (row-wise).
np.hstack()
Stacks arrays horizontally (column-wise).
np.stack()
Stacks arrays along a new axis.
concatenate(), stack() creates a new axis.
Splitting Arrays
Divides an array into multiple sub-arrays.np.split()
Splits an array into equal sections.
np.repeat()
Repeats each element a specified number of times.
np.tile()
Repeats the entire array multiple times.
repeat() repeats elements, while tile() repeats the entire array.
np.sum()
Calculates the total sum of array elements.
np.std()
Calculates the standard deviation of array elements.
np.min()
Returns the smallest value in an array.
Sum by Axis
Calculates sums along specific dimensions.axis=0 adds columns and axis=1 adds rows.
Tip:
axis=0→ vertical operation (columns)axis=1→ horizontal operation (rows)
np.cumsum()
Returns the cumulative sum of elements.
np.cumprod()
Returns the cumulative product of elements.
np.where()
Returns indices or values based on a condition.
np.argwhere()
Returns indices of elements that satisfy a condition.
Masking in Arrays
Selects elements using a condition.np.logical_and()
Combines two conditions using AND operation.
np.logical_or()
Combines two conditions using OR operation.
np.nan
Represents missing or undefined numerical values.
nan represents a missing value.