PYTHON Reference Guide
Revision Time: 4 mins

NumPy Essentials Reference

Core NumPy operations for data engineering — array creation, shape manipulation, and vectorized computation.

np.array()
Return: ndarray

Create a NumPy array from a Python list or nested lists.

Used In: Feature arrays, metric batches, numerical columns.

Syntax signature:np.array(object, dtype=None)
Code snippet:
python
import numpy as np
scores = np.array([88, 92, 79, 95, 84])
print(scores * 1.1)  # vectorized scaling
Expected Output:[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()

Comparison:

List vs ndarray: Python lists store object references; NumPy arrays store contiguous raw values enabling vectorized C operations.

Remember: All elements must be the same type — NumPy auto-upcasts mixed types (int + float → float).
np.arange()
Return: ndarray

Generate an array of evenly spaced values within a given interval.

Used In: Time index arrays, sliding window ranges.

Syntax signature:np.arange(start, stop, step)
Code snippet:
python
import numpy as np
print(np.arange(0, 1.0, 0.25))
Expected Output:[0. 0.25 0.5 0.75]

Time Complexity: O(N)

Related Methods: np.linspace()

Remember: Prefer np.linspace() when you need a specific number of points rather than a specific step size.
np.linspace()
Return: ndarray

Generate N evenly spaced values between start and stop (inclusive).

Used In: Sampling intervals, generating test datasets.

Syntax signature:np.linspace(start, stop, num=50)
Code snippet:
python
import numpy as np
print(np.linspace(0, 1, 5))
Expected Output:[0. 0.25 0.5 0.75 1. ]

Time Complexity: O(N)

Related Methods: np.arange()

Remember: Unlike arange(), linspace() guarantees the stop value is included and gives exactly num points.
np.zeros() / np.ones()
Return: ndarray

Create arrays filled with zeros or ones, with a given shape.

Used In: Initializing score matrices, mask arrays.

Syntax signature:np.zeros(shape) / np.ones(shape)
Code snippet:
python
import numpy as np
print(np.zeros((2, 3)))
print(np.ones(4, dtype=int))
Expected Output:# 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()

Remember: Use np.zeros() to initialize accumulators and np.ones() for multiplicative identities.
shape / dtype
Return: tuple / dtype

Inspect the dimensions and data type of an array.

Used In: Debugging array dimensions, verifying ETL output shapes.

Syntax signature:arr.shape / arr.dtype
Code snippet:
python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
print(arr.dtype)
Expected Output:# print(arr.shape) (2, 3) # print(arr.dtype) int64

Time Complexity: O(1)

Related Methods: arr.ndim, arr.size

Remember: shape returns (rows, cols) for 2D arrays. Always check shape before matrix operations to catch mismatches.
reshape()
Return: ndarray

Change the shape of an array without copying data.

Used In: Reshaping feature vectors, batch matrix preparation.

Syntax signature:arr.reshape(new_shape) or np.reshape(arr, new_shape)
Code snippet:
python
import numpy as np
arr = np.arange(12)
matrix = arr.reshape(3, 4)
print(matrix.shape)
Expected Output:(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.

Remember: Use -1 for one dimension to let NumPy infer it: arr.reshape(-1, 4) auto-calculates rows.
astype()
Return: ndarray

Cast array elements to a different data type.

Used In: Type conversion in ETL, converting floats to ints after normalization.

Syntax signature:arr.astype(dtype)
Code snippet:
python
import numpy as np
arr = np.array([1.7, 2.9, 3.1])
print(arr.astype(int))
Expected Output:[1 2 3]

Time Complexity: O(N)

Related Methods: arr.dtype

Remember: astype() always returns a copy — it never modifies the original array.
np.where()
Return: ndarray

Select elements conditionally — element-wise if/else across arrays.

Used In: Conditional column derivation, flag labeling in ETL.

Syntax signature:np.where(condition, x, y)
Code snippet:
python
import numpy as np
scores = np.array([55, 72, 90, 45, 88])
passed = np.where(scores >= 60, 'Pass', 'Fail')
print(passed)
Expected Output:['Fail' 'Pass' 'Pass' 'Fail' 'Pass']

Time Complexity: O(N)

Related Methods: np.select()

Remember: Vectorized — runs in C, much faster than a Python for-loop with if/else.
mean() / sum() / min() / max()
Return: float | ndarray

Compute aggregate statistics across an array or along an axis.

Used In: Metric aggregation, quality scoring.

Syntax signature:arr.mean() / arr.sum(axis=0)
Code snippet:
python
import numpy as np
data = np.array([[10, 20], [30, 40]])
print(data.mean())
print(data.sum(axis=0))  # column sums
Expected Output:# print(data.mean()) 25.0 # print(data.sum(axis=0)) [40 60]

Time Complexity: O(N)

Related Methods: np.median(), np.std()

Remember: Use axis=0 for column-wise, axis=1 for row-wise aggregation in 2D arrays.
np.unique()
Return: ndarray

Return sorted unique elements of an array, optionally with counts.

Used In: Category discovery, data quality checks.

Syntax signature:np.unique(arr, return_counts=False)
Code snippet:
python
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)))
Expected Output:{'error': 3, 'ok': 2, 'warning': 1}

Time Complexity: O(N log N)

Related Methods: collections.Counter

Remember: np.unique() always returns a sorted array. Use return_counts=True for frequency analysis.
np.sort()
Return: ndarray

Return a sorted copy of an array.

Used In: Ranking scores, sorting metrics for percentile analysis.

Syntax signature:np.sort(arr, axis=-1)
Code snippet:
python
import numpy as np
arr = np.array([5, 2, 8, 1, 9, 3])
print(np.sort(arr))
print(np.sort(arr)[::-1])  # descending
Expected Output:# 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()

Remember: np.sort() returns a copy; use arr.sort() to sort in-place. For descending, reverse the result.