NumPy Essentials Reference
Core NumPy operations for data engineering — array creation, shape manipulation, and vectorized computation.
Create a NumPy array from a Python list or nested lists.
Used In: Feature arrays, metric batches, numerical columns.
np.array(object, dtype=None)import numpy as np
scores = np.array([88, 92, 79, 95, 84])
print(scores * 1.1) # vectorized scaling[96.8 101.2 86.9 104.5 92.4]Time Complexity: O(N)
Common Mistakes: Mixing types causes silent upcasting — e.g. [1, 'a'] becomes ['1', 'a'] as object dtype.
Related Methods: np.asarray()
List vs ndarray: Python lists store object references; NumPy arrays store contiguous raw values enabling vectorized C operations.
Generate an array of evenly spaced values within a given interval.
Used In: Time index arrays, sliding window ranges.
np.arange(start, stop, step)import numpy as np
print(np.arange(0, 1.0, 0.25))[0. 0.25 0.5 0.75]Time Complexity: O(N)
Related Methods: np.linspace()
Generate N evenly spaced values between start and stop (inclusive).
Used In: Sampling intervals, generating test datasets.
np.linspace(start, stop, num=50)import numpy as np
print(np.linspace(0, 1, 5))[0. 0.25 0.5 0.75 1. ]Time Complexity: O(N)
Related Methods: np.arange()
Create arrays filled with zeros or ones, with a given shape.
Used In: Initializing score matrices, mask arrays.
np.zeros(shape) / np.ones(shape)import numpy as np
print(np.zeros((2, 3)))
print(np.ones(4, dtype=int))# print(np.zeros((2, 3)))
[[0. 0. 0.]
[0. 0. 0.]]
# print(np.ones(4, dtype=int))
[1 1 1 1]Time Complexity: O(N)
Related Methods: np.full()
Inspect the dimensions and data type of an array.
Used In: Debugging array dimensions, verifying ETL output shapes.
arr.shape / arr.dtypeimport numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
print(arr.dtype)# print(arr.shape)
(2, 3)
# print(arr.dtype)
int64Time Complexity: O(1)
Related Methods: arr.ndim, arr.size
Change the shape of an array without copying data.
Used In: Reshaping feature vectors, batch matrix preparation.
arr.reshape(new_shape) or np.reshape(arr, new_shape)import numpy as np
arr = np.arange(12)
matrix = arr.reshape(3, 4)
print(matrix.shape)(3, 4)Time Complexity: O(1) — returns a view when possible
Common Mistakes: New shape must have the same total number of elements as the original array.
Cast array elements to a different data type.
Used In: Type conversion in ETL, converting floats to ints after normalization.
arr.astype(dtype)import numpy as np
arr = np.array([1.7, 2.9, 3.1])
print(arr.astype(int))[1 2 3]Time Complexity: O(N)
Related Methods: arr.dtype
Select elements conditionally — element-wise if/else across arrays.
Used In: Conditional column derivation, flag labeling in ETL.
np.where(condition, x, y)import numpy as np
scores = np.array([55, 72, 90, 45, 88])
passed = np.where(scores >= 60, 'Pass', 'Fail')
print(passed)['Fail' 'Pass' 'Pass' 'Fail' 'Pass']Time Complexity: O(N)
Related Methods: np.select()
Compute aggregate statistics across an array or along an axis.
Used In: Metric aggregation, quality scoring.
arr.mean() / arr.sum(axis=0)import numpy as np
data = np.array([[10, 20], [30, 40]])
print(data.mean())
print(data.sum(axis=0)) # column sums# print(data.mean())
25.0
# print(data.sum(axis=0))
[40 60]Time Complexity: O(N)
Related Methods: np.median(), np.std()
Return sorted unique elements of an array, optionally with counts.
Used In: Category discovery, data quality checks.
np.unique(arr, return_counts=False)import numpy as np
statuses = np.array(['ok', 'error', 'ok', 'warning', 'error', 'error'])
uniq, counts = np.unique(statuses, return_counts=True)
print(dict(zip(uniq, counts))){'error': 3, 'ok': 2, 'warning': 1}Time Complexity: O(N log N)
Related Methods: collections.Counter
Return a sorted copy of an array.
Used In: Ranking scores, sorting metrics for percentile analysis.
np.sort(arr, axis=-1)import numpy as np
arr = np.array([5, 2, 8, 1, 9, 3])
print(np.sort(arr))
print(np.sort(arr)[::-1]) # descending# print(np.sort(arr))
[1 2 3 5 8 9]
# print(np.sort(arr)[::-1])
[9 8 5 3 2 1]Time Complexity: O(N log N)
Related Methods: np.argsort()