NumPy Study Sheet

Reference: numpy.org/doc/stable/

Examples updated to reflect the popular Iris Dataset (Sepal/Petal lengths & widths).

Array Creation

Keyword Description Syntax Example
Import NumPy Imports the NumPy library, usually aliased as np. import numpy as np import numpy as np
np.array() Creates a NumPy array from a Python list (1D or Multi-dimensional). np.array(list) # Sepal lengths for 5 flowers
sepal_len = np.array([5.1, 4.9, 4.7, 4.6, 5.0])
np.zeros() Creates an array filled with zeros. Useful for initializing weights. np.zeros(shape) # Initialize placeholders
zeros = np.zeros(5)
np.ones() Creates an array filled with ones. np.ones(shape) ones = np.ones(5)
np.empty() Creates an array without initializing entries (contains garbage values). np.empty(shape) empty = np.empty(3)
np.arange() Creates an array with a sequence of numbers (start, stop, step). np.arange(start, stop, step) # Create index IDs 0, 2, 4, 6, 8
ids = np.arange(0, 10, 2)

Inspecting Arrays

Keyword Description Syntax Example
shape Returns a tuple representing the dimensions of the array (rows, cols). arr.shape # (150, 4) for 150 flowers, 4 features
print(data.shape)
ndim Returns the number of dimensions (axes). arr.ndim print(data.ndim) # 2
size Returns the total number of elements in the array. arr.size print(data.size) # 600
dtype Returns the data type of the elements (e.g., float64, int32). arr.dtype print(data.dtype) # float64

Basic Array Operations

Keyword Description Syntax Example
Arithmetic (+ - * /) Performs element-wise addition, subtraction, multiplication, and division. arr1 + arr2 # Convert cm to mm
sepal_mm = sepal_cm * 10
np.dot() Calculates the dot product of two arrays (matrix multiplication). np.dot(a, b) # Weighted sum of features
score = np.dot(features, weights)
np.sqrt() Computes the square root of each element. np.sqrt(arr) variance = np.array([0.25, 0.16])
std_dev = np.sqrt(variance)
Trig Functions Sine, Cosine, etc. (Inputs should often be in radians). np.sin(arr) np.sin(np.radians(angles))
Aggregation Sum, min, max, mean of array elements. arr.sum(), arr.mean() total_len = sepal_len.sum()
avg_len = sepal_len.mean()

Indexing and Slicing

Keyword Description Syntax Example
1D Indexing Access specific element by integer index (0-based). arr[i] # First flower's length
print(sepal_len[0])
1D Slicing Access a subset of the array [start:stop:step]. arr[start:stop] # First 3 flowers
print(sepal_len[0:3])
2D Indexing Access element at [row, column]. arr[row, col] # 2nd flower (row 1), 1st feature (col 0)
val = iris_data[1, 0]
2D Slicing Slice rows and columns simultaneously. arr[r_start:r_end, c_start:c_end] # First 2 flowers, first 2 features
subset = iris_data[:2, :2]

Filtering (Boolean Indexing)

Keyword Description Syntax Example
Boolean Masking Select elements that satisfy a condition. arr[condition] # Select sepal lengths > 5.0
long_sepals = sepal_len[sepal_len > 5.0]
& (AND) Combine conditions: Both must be true. arr[(cond1) & (cond2)] # Length > 5 AND Width < 3
filter = sepal_len[(sepal_len > 5) & (sepal_width < 3)]
| (OR) Combine conditions: At least one must be true. arr[(cond1) | (cond2)] # Length > 6 OR Width > 4
filter = sepal_len[(sepal_len > 6) | (sepal_width > 4)]

Reshaping Arrays

Keyword Description Syntax Example
reshape() Changes the shape of an array without changing its data. Total elements must remain constant. arr.reshape(rows, cols) # Reshape 12 measurements into 4 flowers x 3 features
matrix = measurements.reshape(4, 3)
Flattening Converting a multi-dimensional array into a 1D array. arr.reshape(-1) or arr.reshape(total_size) # Flatten 2D data into 1 long sequence
flat = matrix.reshape(12)

Functions and Vectorization

Keyword Description Syntax Example
np.vectorize() Converts a Python function to one that applies element-wise to an array (broadcasting). np.vectorize(func) def classify(len):
return "Long" if len > 5 else "Short"

v_classify = np.vectorize(classify)
classes = v_classify(sepal_len)