Comprehensive Guide to NumPy Functions
1. Array Creation Functions
1.1 Basic Arrays
➢ numpy.array(object) Creates an array from a list, tuple, or any array-like
structure.
import numpy as np
array = np.array([1, 2, 3]) # Creates a 1D array
print(array)
➢ numpy.zeros(shape) Creates an array filled with zeros of the specified
shape.
zeros_array = np.zeros((2, 3)) # 2x3 array of zeros
print(zeros_array)
➢ numpy.ones(shape) Creates an array filled with ones.
ones_array = np.ones((2, 2)) # 2x2 array of ones
print(ones_array)
➢ numpy.empty(shape) Creates an uninitialized array of the given shape
(contains random values).
empty_array = np.empty((2, 2)) # 2x2 array with uninitialized values
print(empty_array)
1.2 Ranges and Sequences
• numpy.arange(start, stop, step) Creates an array with evenly spaced values
within a range.
arange_array = np.arange(1, 10, 2) # [1, 3, 5, 7, 9]
print(arange_array)
• numpy.linspace(start, stop, num) Creates an array with num evenly spaced
values between start and stop.
linspace_array = np.linspace(0, 1, 5) # [0.0, 0.25, 0.5, 0.75, 1.0]
print(linspace_array)
1.3 Special Matrices
• numpy.eye(N) Creates an identity matrix of size NN.
identity_matrix = np.eye(3) # 3x3 identity matrix
print(identity_matrix)
• numpy.diag(v) Extracts or creates a diagonal matrix.
diagonal_matrix = np.diag([1, 2, 3])
print(diagonal_matrix)
1.4 Random Arrays
• numpy.random.rand(d0, d1, ...) Generates random numbers uniformly in
the range [0, 1).
random_uniform = np.random.rand(2, 2)
print(random_uniform)
• numpy.random.randint(low, high, size) Generates random integers
between low and high.
random_integers = np.random.randint(0, 10, (2, 2))
print(random_integers)
• numpy.random.randn(d0, d1, ...) Generates random numbers from a
normal distribution (mean=0, std=1).
random_normal = np.random.randn(3)
print(random_normal)
2. Array Manipulation
2.1 Shape Manipulation
• numpy.reshape(a, newshape) Reshapes an array without changing its data.
reshaped_array = np.reshape(np.arange(6), (2, 3))
print(reshaped_array)
• numpy.flatten() Flattens a multi-dimensional array into a 1D array.
flattened_array = np.array([[1, 2], [3, 4]]).flatten()
print(flattened_array)
2.2 Joining and Splitting
• numpy.concatenate((a1, a2), axis) Joins arrays along the specified axis.
concatenated_array = np.concatenate(([1, 2], [3, 4]))
print(concatenated_array)
• numpy.split(a, indices) Splits an array into multiple subarrays.
split_arrays = np.split(np.array([1, 2, 3, 4]), 2)
print(split_arrays)
2.3 Transposing
• numpy.transpose(a) Transposes the dimensions of an array.
transposed_array = np.transpose(np.array([[1, 2], [3, 4]]))
print(transposed_array)
3. Mathematical Operations
• numpy.add(a, b): Element-wise addition.
added_array = np.add([1, 2], [3, 4])
print(added_array)
• numpy.subtract(a, b): Element-wise subtraction.
subtracted_array = np.subtract([3, 4], [1, 2])
print(subtracted_array)
• numpy.multiply(a, b): Element-wise multiplication.
multiplied_array = np.multiply([1, 2], [3, 4])
print(multiplied_array)
• numpy.divide(a, b): Element-wise division.
divided_array = np.divide([4, 6], [2, 3])
print(divided_array)
• numpy.sqrt(a): Square root.
sqrt_array = np.sqrt([1, 4, 9])
print(sqrt_array)
4. Statistical Functions
• numpy.mean(a): Computes the mean.
mean_value = np.mean([1, 2, 3, 4])
print(mean_value)
• numpy.median(a): Computes the median.
median_value = np.median([1, 2, 3, 4])
print(median_value)
• numpy.std(a): Standard deviation.
std_dev = np.std([1, 2, 3, 4])
print(std_dev)
• numpy.var(a): Variance.
variance = np.var([1, 2, 3, 4])
print(variance)
5. Linear Algebra
• numpy.linalg.inv(a): Inverse of a matrix.
matrix = np.array([[1, 2], [3, 4]])
inverse_matrix = np.linalg.inv(matrix)
print(inverse_matrix)
• numpy.linalg.det(a): Determinant of a matrix.
determinant = np.linalg.det([[1, 2], [3, 4]])
print(determinant)
• numpy.linalg.eig(a): Eigenvalues and eigenvectors.
eigenvalues, eigenvectors = np.linalg.eig([[1, 2], [3, 4]])
print(eigenvalues)
print(eigenvectors)
• numpy.linalg.norm(a): Norm of an array.
norm = np.linalg.norm([1, 2, 3])
print(norm)