Skip to content
Machine Learning & Data Science

NumPy interview questions and answers

70 questions, 70 with code. About 3 h 31 min to read them all.

By TestLab · Updated

Plan for an interview
70 questions

Track what you know and what to revise.

Sign in to track progress

You can open a few full answers without signing in. Sign in to read all

1

What is NumPy and why is it important in machine learning?

NumPy is Python’s core library for numerical computing: it provides the ndarray, a fast, memory-efficient n-dimensional array of a single data type stored in one contiguous block, together with vectorized math functions (ufuncs), broadcasting, linear algebra (np.linalg), random number generation (np.random.default_rng), and tools for reading, reshaping and aggregating numeric data. It matters in machine learning because nearly every ML library speaks NumPy: datasets loaded by pandas, images loaded by Pillow or OpenCV, and features passed to scikit-learn are NumPy arrays; scikit-learn’s models, SciPy’s algorithms and Matplotlib’s plots operate on them; and deep-learning frameworks (PyTorch, TensorFlow, JAX) copy its API and convert to and from it cheaply. Vectorized NumPy code runs in compiled C loops, typically tens to hundreds of times faster than equivalent Python loops, which makes it practical to implement and prototype algorithms such as linear regression, k-means, gradient descent and neural-network layers directly. Its limits are that it runs on the CPU, in memory, and without automatic differentiation, which is where PyTorch, JAX, Dask and others take over.

Read full answer

Applies toNumPy 2.x

Where NumPy Appears in ML

Task NumPy role
Data representation Feature matrices X (n_samples × n_features), label vectors y
Preprocessing Scaling, normalization, masking, reshaping images
Algorithms Distances, matrix products, gradients written with arrays
Linear algebra Solving systems, SVD, eigenvalues for PCA
Randomness Reproducible sampling with default_rng(seed)
Interoperability scikit-learn, pandas, SciPy, Matplotlib, PyTorch/JAX conversion

Code Example: Linear Regression with NumPy Alone

import numpy as np

rng = np.random.default_rng(42)
X = rng.normal(size=(100, 2))
y = X @ np.array([2.0, -1.0]) + 0.5 + rng.normal(0, 0.1, 100)

X_b = np.column_stack([np.ones(len(X)), X])          # add a bias column
w = np.linalg.lstsq(X_b, y, rcond=None)[0]           # least-squares fit
print(X.shape, y.shape, w.shape)
# (100, 2) (100,) (3,)
print(np.round(w, 1))
# [ 0.5  2.  -1. ]

pred = X_b @ w
print(round(float(np.mean((pred - y) ** 2)), 3) < 0.02)
# True: the mean squared error is close to the noise variance 0.01

Common Pitfalls

  • Writing Python loops over array elements instead of vectorized expressions.

  • Using the legacy np.random.seed / np.random.rand API instead of default_rng.

  • Mixing up row vectors, column vectors and 1-D arrays in shapes.

Interview Tips

  • Describe the ndarray, vectorization and broadcasting.

  • Explain its role as the common data format of the ML ecosystem.

2

How are NumPy arrays different from Python lists?

A Python list is a flexible container of pointers to separate Python objects of any type, which can grow and shrink; a NumPy array is a fixed-size block of memory holding raw values of one data type (for example 8-byte float64 numbers) side by side, with a shape describing its dimensions. That design difference explains everything else: arrays use far less memory (8 bytes per float64 element instead of a pointer plus a roughly 24-byte Python float object), operations on them run in compiled loops over contiguous memory and are often 10–100 times faster, and arithmetic is element-wise and vectorized (a * 2 doubles every element, while lst * 2 repeats the list). Arrays also support multiple dimensions, broadcasting, slicing that returns views rather than copies, boolean and fancy indexing, and a large library of math, statistics and linear-algebra functions. Lists are better when items have mixed types, when you append many items one by one, or when you store non-numeric objects; arrays are better for numeric data and computation. Converting is easy (np.array(lst), arr.tolist()).

Read full answer

Applies toNumPy 2.x

Comparison

Python list NumPy array
Element types Any, mixed One dtype
Memory Pointers + boxed objects Contiguous raw values
Size Grows/shrinks (append) Fixed (resizing creates a new array)
* 2 Repeats the list Multiplies each element
+ Concatenates Element-wise addition
Dimensions Nested lists True n-D with shape
Slicing Returns a copy Returns a view
Speed for math Slow (Python loop) Fast (C loop)

Code Example

import sys

import numpy as np

lst = [1, 2, 3]
arr = np.array([1, 2, 3])
print(lst * 2, arr * 2)
# [1, 2, 3, 1, 2, 3] [2 4 6]
print(lst + lst, arr + arr)
# [1, 2, 3, 1, 2, 3] [2 4 6]

big_list = list(range(1_000_000))
big_arr = np.arange(1_000_000, dtype=np.int64)
list_bytes = sys.getsizeof(big_list) + sum(sys.getsizeof(x) for x in big_list)
print(f"list about {list_bytes / 1e6:.0f} MB, array {big_arr.nbytes / 1e6:.0f} MB")
# list about 36 MB, array 8 MB

view = arr[1:]
view[0] = 99                        # slices of arrays are views
print(arr.tolist())
# [1, 99, 3]
sub = lst[1:]
sub[0] = 99                         # slices of lists are copies
print(lst)
# [1, 2, 3]

print(np.array([1, 2.5, "a"]).dtype)   # mixed input is coerced to one dtype (here a Unicode string)
# <U32

The list size counts the list’s pointer array (about 8 MB) plus one int object per element (28 bytes each for these small integers).

Common Pitfalls

  • Appending to a NumPy array in a loop (np.append copies every time); collect in a list, then convert once.

  • Expecting + on arrays to concatenate.

  • Accidentally creating an object-dtype array from ragged nested lists (NumPy 2.x raises an error unless dtype=object).

Interview Tips

  • Explain homogeneous contiguous memory vs pointers to objects.

  • Mention vectorized arithmetic, views and when lists are still the better choice.

3

What are the main attributes of a NumPy ndarray?

The main attributes describe an array’s shape, type and memory layout: ndim (number of dimensions), shape (a tuple with the size of each dimension), size (total number of elements), dtype (the data type of every element, such as float64 or int32), itemsize (bytes per element), nbytes (total bytes of data, size × itemsize), strides (bytes to step in each dimension to reach the next element), flags (memory properties such as C_CONTIGUOUS, F_CONTIGUOUS, WRITEABLE and OWNDATA), base (the array this one is a view of, or None), and T (the transpose), plus real, imag and flat. Together they tell you how the array is interpreted and stored: for example, transposing changes shape and strides but not the data, which is why T is instant and why the transposed array is no longer C-contiguous. These attributes are the first thing to check when debugging shape errors, unexpected dtypes, memory use or whether an operation returned a view or a copy.

Read full answer

Applies toNumPy 2.x

Attributes

Attribute Meaning Example for np.zeros((3, 4))
ndim Number of axes 2
shape Size per axis (3, 4)
size Number of elements 12
dtype Element type float64
itemsize Bytes per element 8
nbytes Total data bytes 96
strides Bytes per step along each axis (32, 8)
flags Contiguity, writeability, ownership C_CONTIGUOUS True
base Owner if a view None
T Transposed view shape (4, 3)

Code Example

import numpy as np

a = np.arange(12, dtype=np.float64).reshape(3, 4)
print(a.ndim, a.shape, a.size, a.dtype, a.itemsize, a.nbytes)
# 2 (3, 4) 12 float64 8 96
print(a.strides)
# (32, 8)   next row: 4 elements x 8 bytes; next column: 8 bytes

t = a.T
print(t.shape, t.strides, t.flags["C_CONTIGUOUS"], t.base is a.base or t.base is a)
# (4, 3) (8, 32) False True

row = a[1]
print(row.base is a, row.flags["OWNDATA"], a.flags["OWNDATA"])
# False False False

a itself is a view created by reshape of the arange result, so a.flags["OWNDATA"] is False and a[1].base points to the original 1-D array rather than to a; the transposed array shares the same underlying buffer.

Common Pitfalls

  • Confusing size (element count) with nbytes (memory) or len(a) (length of the first axis).

  • Assuming a view owns its data; check base or np.shares_memory.

  • Passing non-contiguous arrays to C code that expects contiguous memory; use np.ascontiguousarray.

Interview Tips

  • List shape, dtype, size, ndim, itemsize, nbytes and strides.

  • Explain how strides and flags reveal views and layout.

4

How do you create a NumPy array from a Python list?

Pass the list to np.array: np.array([1, 2, 3]) creates a 1-D array, a list of equal-length lists such as np.array([[1, 2], [3, 4]]) creates a 2-D array, and NumPy infers the dtype from the values (all ints give int64 on most platforms, any float gives float64, mixed numbers and strings give a string dtype). Pass dtype= to control the type (np.array(values, dtype=np.float32)), np.asarray(lst) when you want to avoid a copy if the input is already an array, and ndmin= or reshape for specific shapes. In NumPy 2.x, creating an array from ragged nested lists (sublists of different lengths) raises a ValueError unless you explicitly ask for dtype=object, which keeps Python objects and loses most of NumPy’s speed. Other ways to build arrays from Python data are np.fromiter for generators (with a dtype and optional count) and np.stack for a list of arrays.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

a = np.array([1, 2, 3])
print(a, a.dtype, a.shape)
# [1 2 3] int64 (3,)

b = np.array([[1, 2, 3], [4, 5, 6]])
print(b.shape, b.ndim)
# (2, 3) 2

c = np.array([1, 2.5, 3])
print(c, c.dtype)
# [1.  2.5 3. ] float64

d = np.array([1, 2, 3], dtype=np.float32)
print(d.dtype, d.nbytes)
# float32 12

e = np.asarray(d)                      # no copy for an existing array of the same dtype
print(e is d)
# True

try:
    np.array([[1, 2], [3]])            # ragged: different lengths
except ValueError as exc:
    print("ragged input:", type(exc).__name__)
# ragged input: ValueError
ragged = np.array([[1, 2], [3]], dtype=object)
print(ragged.shape, ragged.dtype)
# (2,) object

squares = np.fromiter((x * x for x in range(5)), dtype=np.int64, count=5)
print(squares.tolist())
# [0, 1, 4, 9, 16]

On Windows with NumPy 1.x the default integer was 32-bit; NumPy 2.x made the default integer 64-bit on all 64-bit platforms.

Common Pitfalls

  • Ragged nested lists, which now raise an error.

  • Unintended dtypes (integers when you need floats, so later division assignments truncate).

  • Creating arrays element by element in a loop instead of building a list first.

Interview Tips

  • Show np.array, dtype inference and the dtype= argument.

  • Mention np.asarray, ragged-list behaviour in 2.x and np.fromiter.

5

Explain broadcasting in NumPy

Broadcasting is the set of rules that lets NumPy combine arrays of different shapes in element-wise operations without copying data: shapes are compared from the rightmost dimension leftward, two dimensions are compatible if they are equal or one of them is 1, missing leading dimensions are treated as 1, and each size-1 dimension is virtually stretched to match the other array. So a (3, 4) matrix plus a (4,) vector adds the vector to every row, a (3, 1) column plus a (1, 4) row produces a (3, 4) table of all pairwise sums, and a scalar combines with any array. If any pair of dimensions differs and neither is 1, NumPy raises “operands could not be broadcast together”. Broadcasting is what makes expressions like centring data (X - X.mean(axis=0)), scaling, distance matrices (A[:, None, :] - B[None, :, :]) and outer products short and fast, but it can also hide bugs: a (n,) array combined with an (n, 1) array silently creates an (n, n) result.

Read full answer

Applies toNumPy 2.x

The Rules, Step by Step

A shape B shape Aligned from the right Result
(3, 4) (4,) (3, 4) vs (1, 4) (3, 4)
(3, 1) (1, 4) (3, 1) vs (1, 4) (3, 4)
(2, 3, 4) (3, 1) (2, 3, 4) vs (1, 3, 1) (2, 3, 4)
(5,) (5, 1) (1, 5) vs (5, 1) (5, 5), often a bug
(3, 4) (3,) (3, 4) vs (1, 3) Error: 4 vs 3

Code Example

import numpy as np

X = np.array([[1.0, 2.0, 3.0],
              [4.0, 5.0, 6.0]])                    # shape (2, 3)
centred = X - X.mean(axis=0)                       # (2, 3) - (3,)
print(centred.tolist())
# [[-1.5, -1.5, -1.5], [1.5, 1.5, 1.5]]

row_scale = np.array([[10.0], [100.0]])            # shape (2, 1): one factor per row
print((X * row_scale).tolist())
# [[10.0, 20.0, 30.0], [400.0, 500.0, 600.0]]

a = np.arange(3)[:, None]                          # (3, 1)
b = np.arange(4)[None, :]                          # (1, 4)
print((a + b).shape, (a + b)[2].tolist())
# (3, 4) [2, 3, 4, 5]

points = np.array([[0.0, 0.0], [3.0, 4.0]])
centres = np.array([[0.0, 0.0], [6.0, 8.0], [3.0, 0.0]])
dists = np.linalg.norm(points[:, None, :] - centres[None, :, :], axis=2)   # (2, 3) distance matrix
print(dists.tolist())
# [[0.0, 10.0, 3.0], [5.0, 5.0, 4.0]]

y = np.array([1.0, 2.0, 3.0])
pred = y[:, None]                                  # accidentally (3, 1)
print((y - pred).shape)                            # silent (3, 3) instead of (3,)
# (3, 3)

try:
    X + np.array([1.0, 2.0])                       # (2, 3) + (2,): 3 vs 2
except ValueError as exc:
    print("error:", "could not be broadcast" in str(exc))
# error: True

Common Pitfalls

  • Mixing (n,) and (n, 1) arrays, which broadcasts to (n, n).

  • Expecting a per-row vector of shape (n,) to align with rows; reshape it to (n, 1) first.

  • Huge intermediate arrays from broadcasting large dimensions (for example all-pairs distances on big data).

Interview Tips

  • State the right-to-left, equal-or-1 rule and work an example.

  • Show centring and a pairwise distance matrix, and warn about (n,) vs (n, 1).

6

What data types do NumPy arrays support?

NumPy arrays have a single fixed dtype, chosen from a rich set: booleans (bool), signed integers (int8, int16, int32, int64), unsigned integers (uint8 through uint64), floating-point numbers (float16, float32, float64, and longdouble), complex numbers (complex64, complex128), dates and durations (datetime64, timedelta64 with units such as days or nanoseconds), fixed-length byte and Unicode strings (S10, U10), the new variable-length StringDType added in NumPy 2.0, structured dtypes that combine named fields into records, and object for arbitrary Python objects. The dtype decides memory use, range and precision: uint8 holds 0–255 in one byte (ideal for image pixels), float32 halves memory compared with float64 (common for deep learning) at about 7 significant digits instead of 16, and fixed-size integers overflow silently by wrapping around. Use astype to convert, np.iinfo and np.finfo to check limits, and remember that NumPy 2.x removed old aliases such as np.float and np.int (use Python’s float/int or explicit np.float64/np.int64) and changed type promotion rules (NEP 50) so that Python scalars no longer upcast arrays unexpectedly.

Read full answer

Applies toNumPy 2.x

Main dtypes

Kind dtypes Bytes Notes
Boolean bool 1 Masks
Signed int int8 … int64 1–8 Wraps on overflow
Unsigned int uint8 … uint64 1–8 Pixels (uint8)
Float float16, float32, float64 2, 4, 8 ~3, 7, 16 significant digits
Complex complex64, complex128 8, 16 Signal processing
Date/time datetime64[D], timedelta64[s] … 8 Units in brackets
Strings S (bytes), U (Unicode), StringDType Fixed / variable StringDType is new in 2.0
Structured [("name", "U10"), ("age", "i4")] Sum of fields Records
Object object Pointer Slow, Python objects

Code Example

import numpy as np

print(np.array([1, 2]).dtype, np.array([1.0, 2]).dtype, np.array([True]).dtype)
# int64 float64 bool

pixels = np.array([250, 10], dtype=np.uint8)
print((pixels + np.uint8(10)).tolist())             # wraps around above 255
# [4, 20]
print(np.iinfo(np.int8).min, np.iinfo(np.int8).max, np.finfo(np.float32).eps)
# -128 127 1.1920929e-07

x = np.array([1.123456789], dtype=np.float32)
print(x[0], x.astype(np.float64)[0])
# 1.1234568 1.1234568357467651

dates = np.array(["2026-01-01", "2026-03-01"], dtype="datetime64[D]")
print((dates[1] - dates[0]), dates.dtype)
# 59 days datetime64[D]

names = np.array(["ana", "bartholomew"], dtype=np.dtypes.StringDType())   # variable-length strings (NumPy 2.0+)
print(names.dtype, np.strings.upper(names).tolist())
# StringDType() ['ANA', 'BARTHOLOMEW']

small = np.array([1, 2], dtype=np.int8)
print((small + 1).dtype)                            # NumPy 2 (NEP 50): Python int doesn't upcast
# int8

Common Pitfalls

  • Silent integer overflow in small integer types (sums of uint8 images).

  • Assuming float32 precision is enough for accumulating large sums.

  • Using removed aliases (np.float, np.int, np.bool) from old tutorials; np.bool exists again in 2.x as the NumPy boolean, but np.float and np.int don’t.

  • Object arrays, which lose vectorized speed.

Interview Tips

  • Name the families and give memory/precision trade-offs.

  • Mention overflow, astype, iinfo/finfo, StringDType and NumPy 2 promotion changes.

7

How do you inspect the shape and size of a NumPy array?

Use the array’s attributes: a.shape gives a tuple with the length of each dimension, a.ndim the number of dimensions, a.size the total number of elements (the product of the shape), len(a) the length of the first dimension only, and a.nbytes the memory taken by the data in bytes (size × itemsize); the functions np.shape(a), np.ndim(a) and np.size(a, axis=...) do the same and also accept lists and scalars. Shapes are the most common source of bugs in numerical code, so check them often: a 1-D vector has shape (n,), which is different from a column (n, 1) or a row (1, n); a batch of 32 RGB images might be (32, 224, 224, 3) in channels-last format or (32, 3, 224, 224) channels-first; and a scalar has shape (). Assertions (assert X.shape == (n, d)) and printing shapes after each step of a pipeline catch mismatches early, and you can change shapes with reshape (using -1 for one inferred dimension), ravel, squeeze, np.newaxis and expand_dims.

Read full answer

Applies toNumPy 2.x

Quick Reference

Expression Returns
a.shape Tuple of dimension sizes
a.ndim Number of dimensions
a.size Number of elements
len(a) Length of axis 0
a.nbytes Bytes of data
np.size(a, axis=1) Length of one axis
a.reshape(-1, k) New shape with inferred first dimension

Code Example

import numpy as np

images = np.zeros((32, 224, 224, 3), dtype=np.uint8)
print(images.shape, images.ndim, images.size, len(images), images.nbytes)
# (32, 224, 224, 3) 4 4816896 32 4816896

v = np.arange(6)
print(v.shape, v.reshape(-1, 1).shape, v.reshape(1, -1).shape, v.reshape(2, -1).shape)
# (6,) (6, 1) (1, 6) (2, 3)

print(np.shape(5.0), np.ndim([[1, 2], [3, 4]]), np.size(images, axis=1))
# () 2 224

col = v[:, np.newaxis]
print(col.shape, col.squeeze().shape)
# (6, 1) (6,)

channels_first = np.transpose(images, (0, 3, 1, 2))
print(channels_first.shape)
# (32, 3, 224, 224)

def check(X, y):
    assert X.ndim == 2, f"X must be 2-D, got shape {X.shape}"
    assert X.shape[0] == y.shape[0], f"{X.shape[0]} rows vs {y.shape[0]} labels"

check(np.zeros((10, 4)), np.zeros(10))           # passes silently

Each image here is 224 × 224 × 3 = 150,528 bytes as uint8, so the batch of 32 takes 4,816,896 bytes, the same as its number of elements.

Common Pitfalls

  • Confusing len(a) with a.size for multi-dimensional arrays.

  • Treating (n,) and (n, 1) as the same shape.

  • Reshaping to a shape whose product doesn’t match size (raises an error).

Interview Tips

  • List shape, ndim, size, len and nbytes.

  • Show reshaping with -1 and common ML shapes (batches of images).

8

What is the difference between a deep copy and a shallow copy in NumPy?

In NumPy the practical distinction is between a view and a copy: a view (also called a shallow copy), created by basic slicing (a[1:3]), reshape when possible, transpose, a.view() or ravel on contiguous data, is a new array object that shares the same memory buffer as the original, so changing one changes the other; a copy, created by a.copy(), np.array(a), fancy indexing (a[[0, 2]]) or boolean masks (a[a > 0]), allocates new memory, so the two are independent. For ordinary numeric arrays, a.copy() is already a full deep copy, because the elements are raw numbers, not references. The difference between copy.copy and copy.deepcopy only matters for object-dtype arrays, where a.copy() copies the references and the Python objects inside are still shared; copy.deepcopy(a) copies those objects too. Use np.shares_memory(a, b) or b.base is a to check, copy explicitly when you need independence (for example before modifying a slice of training data), and prefer views when you only read data, because they are free.

Read full answer

Applies toNumPy 2.x

Views vs Copies

Operation Result
a[1:3], a[:, 0], a[::2] View
a.T, a.reshape(...) (when possible), a.view() View
a.ravel() View if contiguous, else copy
a.flatten() Always a copy
a[[0, 2]] (fancy), a[a > 0] (mask) Copy
a.copy(), np.array(a) Copy
copy.deepcopy(a) (object dtype) Copy including inner objects

Code Example

import copy

import numpy as np

a = np.arange(6)
v = a[1:4]                      # view
c = a[1:4].copy()               # independent copy
v[0] = 100
c[1] = 200
print(a.tolist(), v.tolist(), c.tolist())
# [0, 100, 2, 3, 4, 5] [100, 2, 3] [1, 200, 3]
print(np.shares_memory(a, v), np.shares_memory(a, c))
# True False

fancy = a[[0, 2]]               # fancy indexing returns a copy
fancy[0] = -1
print(a[0])
# 0

print(np.shares_memory(a, a.ravel()), np.shares_memory(a, a.flatten()))
# True False

objs = np.array([[1, 2], [3]], dtype=object)    # object array holding Python lists
shallow, deep = objs.copy(), copy.deepcopy(objs)
objs[0].append(99)                               # mutate the inner list
print(shallow[0], deep[0])
# [1, 2, 99] [1, 2]

Common Pitfalls

  • Modifying a slice and unintentionally changing the original data.

  • Assuming fancy or boolean indexing returns a view (assignments to a[mask] work, but b = a[mask]; b[:] = 0 doesn’t change a).

  • Keeping a small view of a huge array, which keeps the whole buffer alive in memory; copy the small part.

Interview Tips

  • Frame it as views vs copies and list which operations create which.

  • Mention shares_memory, object arrays and memory retention by views.

9

How do you perform element-wise operations in NumPy?

Element-wise operations apply an operation to each pair of corresponding elements, and in NumPy you write them as ordinary expressions on whole arrays: arithmetic operators (a + b, a - b, a * b, a / b, a // b, a % b, a ** 2), comparisons that return boolean arrays (a > 0, a == b), logical and bitwise operators (&, |, ~, or np.logical_and), and universal functions such as np.sqrt, np.exp, np.log, np.abs, np.maximum and np.where. Arrays must have the same shape or broadcast-compatible shapes. Note that * is element-wise multiplication, not matrix multiplication (use @ for that), that division of integers with / gives floats while // floors, and that invalid operations such as dividing by zero produce inf or nan with a warning rather than an exception (control this with np.errstate). Operations return new arrays unless you use in-place forms (a += 1) or the out= argument, which save memory.

Read full answer

Applies toNumPy 2.x

Operators and Functions

Operation Operator Function
Add, subtract +, - np.add, np.subtract
Multiply (element-wise) * np.multiply
Divide, floor divide /, // np.divide, np.floor_divide
Power, modulo **, % np.power, np.mod
Compare <, ==, >= np.less, np.equal
Logical (and, or, not) &, pipe, ~ np.logical_and, np.logical_or, np.logical_not
Math — np.sqrt, np.exp, np.log, np.abs, np.clip
Element-wise max/min — np.maximum, np.minimum

Code Example

import numpy as np

a = np.array([1.0, 4.0, 9.0, 16.0])
b = np.array([2.0, 2.0, 3.0, 4.0])

print((a + b).tolist(), (a * b).tolist(), (a / b).tolist())
# [3.0, 6.0, 12.0, 20.0] [2.0, 8.0, 27.0, 64.0] [0.5, 2.0, 3.0, 4.0]
print(np.sqrt(a).tolist(), (a ** 0.5 == np.sqrt(a)).all())
# [1.0, 2.0, 3.0, 4.0] True
print(((a > 3) & (a < 15)).tolist())
# [False, True, True, False]
print(np.maximum(a, 5).tolist(), np.where(a > 5, a, 0).tolist())
# [5.0, 5.0, 9.0, 16.0] [0.0, 0.0, 9.0, 16.0]

M = np.array([[1, 2], [3, 4]])
print((M * M).tolist(), (M @ M).tolist())                 # element-wise vs matrix product
# [[1, 4], [9, 16]] [[7, 10], [15, 22]]

with np.errstate(divide="ignore", invalid="ignore"):
    print((np.array([1.0, 0.0]) / np.array([0.0, 0.0])).tolist())
# [inf, nan]

x = np.arange(5, dtype=np.float64)
np.multiply(x, 2, out=x)                                   # in place, no new array
print(x.tolist())
# [0.0, 2.0, 4.0, 6.0, 8.0]

Common Pitfalls

  • Using * when matrix multiplication was meant.

  • and/or instead of &/| with arrays, which raises “truth value of an array is ambiguous”.

  • Operator precedence: a > 3 & a < 15 without parentheses is evaluated as a > (3 & a) < 15.

  • In-place operations on integer arrays with float results (a /= 2 on int arrays raises an error).

Interview Tips

  • Show arithmetic, comparisons, ufuncs and broadcasting.

  • Distinguish * from @ and mention np.errstate and in-place out=.

10

What are universal functions (ufuncs) in NumPy?

A universal function (ufunc) is a NumPy function that operates element by element on arrays in compiled C loops, supporting broadcasting, type casting and output arrays; np.add, np.multiply, np.sqrt, np.exp, np.log, np.sin, np.maximum, np.greater and the logical functions are all ufuncs, and the arithmetic operators on arrays call them (a + b is np.add(a, b)). Beyond element-wise evaluation, every ufunc shares a common interface: keyword arguments out= (write into an existing array), where= (compute only where a mask is True), dtype= and casting=; and methods that turn the operation into other patterns: reduce (fold along an axis, np.add.reduce is a sum), accumulate (running results, np.multiply.accumulate is a cumulative product), outer (apply to all pairs, np.multiply.outer is an outer product), reduceat (reductions over segments) and at (unbuffered in-place operations at repeated indices, useful for histogram-like counting). Ufuncs are why vectorized NumPy code is fast, and libraries like Numba (@vectorize) and np.frompyfunc let you create your own, though np.frompyfunc and np.vectorize only wrap Python functions and are not faster than loops.

Read full answer

Applies toNumPy 2.x

Ufunc Features

Feature Example Result
Element-wise call np.add(a, b) a + b
out= np.exp(x, out=buf) No new allocation
where= np.log(x, out=res, where=x > 0) Compute only where True
reduce np.add.reduce(a, axis=0) Sum over an axis
accumulate np.add.accumulate(a) Running sum
outer np.multiply.outer(u, v) Pairwise products
at np.add.at(counts, idx, 1) Correct repeated-index updates
Introspection np.add.nin, np.add.nout Number of inputs/outputs

Code Example

import numpy as np

a = np.array([1, 2, 3, 4])
print(np.add.reduce(a), np.add.accumulate(a).tolist(), np.multiply.accumulate(a).tolist())
# 10 [1, 3, 6, 10] [1, 2, 6, 24]
print(np.multiply.outer([1, 2, 3], [10, 20]).tolist())
# [[10, 20], [20, 40], [30, 60]]

x = np.array([4.0, -1.0, 0.0, 9.0])
safe_log = np.full_like(x, np.nan)
np.log(x, out=safe_log, where=x > 0)                     # log only of positive values
print(np.round(safe_log, 3).tolist())
# [1.386, nan, nan, 2.197]

counts = np.zeros(4, dtype=int)
idx = np.array([0, 2, 2, 2, 3])
counts[idx] += 1                                         # buffered: repeated indices counted once
print(counts.tolist())
# [1, 0, 1, 1]
counts = np.zeros(4, dtype=int)
np.add.at(counts, idx, 1)                                # unbuffered: every occurrence counted
print(counts.tolist())
# [1, 0, 3, 1]

print(np.add.reduceat(np.arange(8), [0, 3, 5]).tolist())  # sums of segments [0:3], [3:5], [5:]
# [3, 7, 18]
print(isinstance(np.sqrt, np.ufunc), isinstance(np.median, np.ufunc))
# True False

Common Pitfalls

  • Using np.vectorize expecting a speed-up; it is a convenience loop.

  • Repeated-index updates with a[idx] += 1 instead of np.add.at.

  • Forgetting to initialize out when using where= (unselected positions keep whatever was there).

Interview Tips

  • Define ufuncs as compiled element-wise functions with broadcasting.

  • Show reduce, accumulate, outer, at and the out=/where= arguments.

11

How do you perform matrix multiplication in NumPy?

Use the @ operator (or np.matmul): for 2-D arrays A @ B computes the matrix product, where A’s number of columns must equal B’s number of rows, so a (m, n) array times a (n, p) array gives (m, p). For arrays with more than two dimensions, @ treats the last two axes as matrices and broadcasts over the leading “batch” axes, so a stack of 32 matrices (32, 3, 4) times (4, 5) gives (32, 3, 5). A 1-D array is treated as a vector: A @ v returns a 1-D result. np.dot gives the same answer for 1-D and 2-D inputs but has different, rarely wanted behaviour for higher dimensions (it sums over the last axis of the first array and the second-to-last of the second, producing a larger array), so prefer @ in modern code. Do not confuse it with *, which multiplies element by element. Other tools include np.vdot and np.inner for dot products, np.outer for outer products, np.einsum for arbitrary index contractions, and np.linalg.multi_dot to multiply a chain of matrices in the cheapest order. NumPy hands matrix products to an optimized BLAS library (OpenBLAS or MKL), which is multithreaded and very fast.

Read full answer

Applies toNumPy 2.x

Options

Function Use
A @ B, np.matmul Matrix product, batched over leading axes
np.dot Same for 1-D/2-D; avoid for 3-D+
A * B Element-wise, not matrix product
np.einsum("ij,jk->ik", A, B) Explicit index notation
np.linalg.multi_dot([A, B, C]) Chain in optimal order
np.outer(u, v), np.inner(u, v) Outer and inner products

Code Example

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print((A @ B).tolist(), np.array_equal(A @ B, np.dot(A, B)))
# [[19, 22], [43, 50]] True
print((A * B).tolist())                         # element-wise, different
# [[5, 12], [21, 32]]

v = np.array([1, 1])
print((A @ v).tolist(), (v @ A).tolist())       # matrix-vector and vector-matrix
# [3, 7] [4, 6]

rng = np.random.default_rng(42)
batch = rng.normal(size=(32, 3, 4))
W = rng.normal(size=(4, 5))
print((batch @ W).shape, np.dot(batch, rng.normal(size=(32, 4, 5))).shape)
# (32, 3, 5) (32, 3, 32, 5)

print(np.allclose(np.einsum("bij,jk->bik", batch, W), batch @ W))
# True

try:
    A @ np.ones((3, 2))
except ValueError as exc:
    print("shape mismatch:", "mismatch" in str(exc))
# shape mismatch: True

Common Pitfalls

  • Using * instead of @.

  • Inner dimensions that don’t match, or 1-D vs (n, 1) shape confusion.

  • Using np.dot on 3-D arrays and getting an unexpected 4-D result.

  • Integer matrices overflowing; integer products also don’t use BLAS and are slower.

Interview Tips

  • State the shape rule (m, n) @ (n, p) → (m, p) and batching.

  • Distinguish @, np.dot and *, and mention BLAS.

12

How do you invert a matrix in NumPy?

Call np.linalg.inv(A) on a square, non-singular matrix; it returns A⁻¹ such that A @ A_inv is the identity up to floating-point error, and it raises numpy.linalg.LinAlgError if the matrix is singular (determinant zero, rows linearly dependent). In practice you rarely need the inverse itself: to solve a linear system Ax = b, use np.linalg.solve(A, b), which is faster and more numerically accurate than inv(A) @ b because it factorizes A once and never forms the inverse. For matrices that are singular, not square or badly conditioned, use the Moore–Penrose pseudo-inverse np.linalg.pinv (computed by SVD) or least squares np.linalg.lstsq, and check the condition number with np.linalg.cond: a large value (for example above 1e12 for float64) means the inverse amplifies rounding errors and results are unreliable. inv also works on stacks of matrices with shape (…, n, n).

Read full answer

Applies toNumPy 2.x

Choosing a Function

Need Function
The inverse of a well-conditioned square matrix np.linalg.inv
Solve Ax = b np.linalg.solve (preferred)
Singular or non-square matrix np.linalg.pinv
Overdetermined system, best fit np.linalg.lstsq
Check numerical stability np.linalg.cond

Code Example

import numpy as np

A = np.array([[4.0, 7.0], [2.0, 6.0]])           # determinant = 4*6 - 7*2 = 10
A_inv = np.linalg.inv(A)
print(np.round(A_inv, 2).tolist())
# [[0.6, -0.7], [-0.2, 0.4]]
print(np.allclose(A @ A_inv, np.eye(2)))
# True

b = np.array([1.0, 2.0])
x = np.linalg.solve(A, b)                         # better than A_inv @ b
print(np.round(x, 2).tolist(), np.allclose(A @ x, b))
# [-0.8, 0.6] True

S = np.array([[1.0, 2.0], [2.0, 4.0]])           # singular: row 2 = 2 * row 1
try:
    np.linalg.inv(S)
except np.linalg.LinAlgError as exc:
    print("LinAlgError:", exc)
# LinAlgError: Singular matrix
print(np.round(np.linalg.pinv(S), 2).tolist())
# [[0.04, 0.08], [0.08, 0.16]]

nearly = np.array([[1.0, 1.0], [1.0, 1.0 + 1e-12]])
print(f"{np.linalg.cond(nearly):.0e}")
# 4e+12

For a 2×2 matrix [[a, b], [c, d]] the inverse is (1 / (ad − bc)) [[d, −b], [−c, a]], which gives the values above.

Common Pitfalls

  • Computing inv(A) @ b instead of solve(A, b).

  • Inverting ill-conditioned matrices and trusting the result.

  • Expecting inv to fail on nearly singular matrices; it may return huge numbers instead.

  • Using **-1 (element-wise reciprocal) when the matrix inverse was meant.

Interview Tips

  • Show inv, then explain why solve is preferred.

  • Mention pinv, lstsq, cond and LinAlgError.

13

How do you calculate a matrix determinant in NumPy?

Use np.linalg.det(A) on a square matrix (or a stack of square matrices with shape (…, n, n)); NumPy computes it from an LU factorization, so it runs in O(n³) time and returns a float that may carry small rounding errors, for example 10.000000000000002 instead of 10. The determinant tells you whether a matrix is invertible (zero means singular), how the linear map scales volume (|det| is the volume scale factor, and a negative sign means orientation flips), and it equals the product of the eigenvalues. Because of floating-point error, never test det(A) == 0; a singular matrix may give something like 1e-16, and a tiny determinant does not by itself mean a matrix is nearly singular (scaling a 100×100 identity by 0.1 gives det 1e-100 but is perfectly well-conditioned), so use np.linalg.matrix_rank or np.linalg.cond to judge invertibility. For large matrices the determinant overflows or underflows easily, so use np.linalg.slogdet, which returns the sign and the natural log of the absolute value; this is standard when computing Gaussian log-likelihoods.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

A = np.array([[4.0, 7.0], [2.0, 6.0]])
print(round(float(np.linalg.det(A)), 6))
# 10.0

B = np.array([[2.0, 0.0, 1.0],
              [1.0, 3.0, 2.0],
              [1.0, 1.0, 2.0]])
print(round(float(np.linalg.det(B)), 6))           # 2*(3*2-2*1) - 0 + 1*(1*1-3*1) = 8 - 2 = 6
# 6.0
print(np.isclose(np.linalg.det(A @ A), np.linalg.det(A) ** 2))
# True
print(np.isclose(np.prod(np.linalg.eigvals(B)).real, np.linalg.det(B)))
# True

S = np.array([[1.0, 2.0], [2.0, 4.0]])
print(np.isclose(np.linalg.det(S), 0.0), np.linalg.matrix_rank(S))
# True 1

big = 0.1 * np.eye(100)
print(np.linalg.det(big), np.linalg.cond(big))      # tiny det, perfectly conditioned
# about 1e-100, and 1.0
sign, logdet = np.linalg.slogdet(big)
print(sign, round(float(logdet), 2))
# 1.0 -230.26

The exact digits of the tiny determinant depend on rounding; ln(0.1) × 100 = −230.26.

Common Pitfalls

  • Testing det == 0 with floats.

  • Using a small determinant as a measure of near-singularity instead of cond or matrix_rank.

  • Overflow or underflow for large matrices; use slogdet.

  • Computing determinants to solve systems (Cramer’s rule) instead of solve.

Interview Tips

  • Show np.linalg.det and explain what the determinant means.

  • Mention floating-point tolerance, slogdet and cond.

14

What does the axis parameter do in NumPy functions?

The axis parameter tells a function which dimension to operate along; for reductions such as sum, mean, max, argmax and std, it is the dimension that gets collapsed, so for a 2-D array axis=0 reduces down the rows and returns one value per column, while axis=1 reduces across the columns and returns one value per row. The default axis=None operates on the flattened array, returning a single value. You can pass a tuple such as axis=(1, 2) to reduce several axes at once (for example, average over height and width of an image batch to get per-image, per-channel means), negative values count from the end (axis=-1 is the last axis), and keepdims=True keeps the reduced axis with length 1 so the result broadcasts back against the original array, which is handy for normalization. Non-reducing functions also use axis with a similar meaning: np.concatenate, np.sort, np.cumsum, np.diff and np.apply_along_axis work along the given dimension. A useful way to remember it: the result’s shape is the input’s shape with the axis entry removed.

Read full answer

Applies toNumPy 2.x

Shapes for X of Shape (2, 3)

Call Result shape Meaning
X.sum() () Total
X.sum(axis=0) (3,) Column sums
X.sum(axis=1) (2,) Row sums
X.sum(axis=1, keepdims=True) (2, 1) Row sums, broadcastable
X.sum(axis=-1) (2,) Same as axis=1 here

Code Example

import numpy as np

X = np.array([[1, 2, 3],
              [4, 5, 6]])
print(X.sum(), X.sum(axis=0).tolist(), X.sum(axis=1).tolist())
# 21 [5, 7, 9] [6, 15]
print(X.max(axis=0).tolist(), X.argmax(axis=1).tolist())
# [4, 5, 6] [2, 2]

row_share = X / X.sum(axis=1, keepdims=True)    # (2, 3) / (2, 1)
print(np.round(row_share, 2).tolist())
# [[0.17, 0.33, 0.5], [0.27, 0.33, 0.4]]

images = np.arange(2 * 4 * 4 * 3).reshape(2, 4, 4, 3)    # (batch, height, width, channel)
per_image_channel = images.mean(axis=(1, 2))
print(per_image_channel.shape, per_image_channel[0].tolist())
# (2, 3) [22.5, 23.5, 24.5]

print(np.cumsum(X, axis=1).tolist(), np.diff(X, axis=0).tolist())
# [[1, 3, 6], [4, 9, 15]] [[3, 3, 3]]

For the first image the channel-0 values are 0, 3, 6, … 45, whose mean is 22.5.

Common Pitfalls

  • Mixing up axis=0 and axis=1 (“axis 0 = down the rows, one result per column”).

  • Forgetting keepdims=True and getting broadcasting errors or wrong broadcasting when dividing.

  • Assuming the default reduces per column (it reduces everything).

Interview Tips

  • Explain that the axis is the one that disappears and give the shape rule.

  • Show tuples of axes and keepdims for normalization.

15

How do you concatenate two arrays in NumPy?

Use np.concatenate([a, b], axis=k), which joins a sequence of arrays along an existing axis; all arrays must have the same number of dimensions and the same size on every other axis, and the default axis is 0 (stack rows below each other), while axis=1 places them side by side. To join along a new axis instead, use np.stack([a, b], axis=k), which requires identical shapes and adds a dimension (two (3,) arrays become (2, 3)). Convenience wrappers are np.vstack (rows, promoting 1-D arrays to rows), np.hstack (columns, or end-to-end for 1-D), np.column_stack (1-D arrays as columns) and np.dstack (depth). With axis=None, concatenate flattens everything first. Every call allocates a new array and copies the data, so don’t concatenate inside a loop; collect pieces in a Python list and join once at the end. NumPy 2.x also lets you pass dtype= and casting= to control the result type.

Read full answer

Applies toNumPy 2.x

Functions

Function Axis Input requirement
np.concatenate(seq, axis=k) Existing axis k Same shape except on axis k
np.stack(seq, axis=k) New axis k Identical shapes
np.vstack Rows Same number of columns
np.hstack Columns (or 1-D end-to-end) Same number of rows
np.column_stack 1-D → columns Same length
np.concatenate(seq, axis=None) Flattened Any shapes

Code Example

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.array([[7], [8]])

print(np.concatenate([a, b]).tolist())              # axis=0
# [[1, 2], [3, 4], [5, 6]]
print(np.concatenate([a, c], axis=1).tolist())
# [[1, 2, 7], [3, 4, 8]]

x, y = np.array([1, 2, 3]), np.array([4, 5, 6])
print(np.concatenate([x, y]).tolist())
# [1, 2, 3, 4, 5, 6]
print(np.stack([x, y]).shape, np.stack([x, y], axis=1).tolist())
# (2, 3) [[1, 4], [2, 5], [3, 6]]
print(np.column_stack([x, y]).shape, np.vstack([x, y]).shape)
# (3, 2) (2, 3)

print(np.concatenate([a, x], axis=None).tolist())
# [1, 2, 3, 4, 1, 2, 3]

try:
    np.concatenate([a, x])                          # 2-D and 1-D
except ValueError as exc:
    print("error:", "dimensions" in str(exc))
# error: True

parts = [np.full(2, i) for i in range(3)]          # build a list, join once
print(np.concatenate(parts).tolist())
# [0, 0, 1, 1, 2, 2]

Common Pitfalls

  • Mismatched sizes on the non-concatenation axis.

  • Joining 1-D and 2-D arrays without reshaping.

  • Using concatenate when a new axis was wanted (stack).

  • Concatenating in a loop, which is O(n²).

Interview Tips

  • Contrast concatenate (existing axis) with stack (new axis).

  • Mention vstack/hstack/column_stack and avoiding loops.

16

How do you flatten a multi-dimensional NumPy array?

There are three main ways: a.flatten() always returns a new 1-D copy, a.ravel() (or np.ravel(a)) returns a 1-D view without copying when the data is contiguous and a copy only when it must, and a.reshape(-1) behaves like ravel, returning a view when possible. All three read elements in row-major © order by default, so a 2×3 array [[1, 2, 3], [4, 5, 6]] becomes [1, 2, 3, 4, 5, 6]; pass order="F" for column-major order ([1, 4, 2, 5, 3, 6]). Choose ravel or reshape(-1) for speed and memory when you only read the result, and flatten when you need an independent array you can modify without affecting the original. To flatten only some dimensions, for example turning a batch of images (N, H, W, C) into a feature matrix (N, H·W·C) for a classical ML model, use a.reshape(len(a), -1). The a.flat attribute gives an iterator over the elements without creating an array.

Read full answer

Applies toNumPy 2.x

Comparison

Method Returns Copies?
a.flatten() 1-D array Always
a.ravel() 1-D array Only if needed
a.reshape(-1) 1-D array Only if needed
a.reshape(n, -1) 2-D, flatten trailing axes Only if needed
a.flat Iterator No

Code Example

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.flatten().tolist(), a.ravel().tolist(), a.reshape(-1).tolist())
# [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
print(a.ravel(order="F").tolist())
# [1, 4, 2, 5, 3, 6]

r, f = a.ravel(), a.flatten()
r[0] = 100                               # view: changes a
f[1] = 200                               # copy: does not
print(a[0].tolist())
# [100, 2, 3]

t = a.T                                  # not C-contiguous
print(np.shares_memory(t, t.ravel()))    # ravel has to copy here
# False

images = np.zeros((10, 28, 28))
features = images.reshape(len(images), -1)
print(features.shape)
# (10, 784)

print(list(a.flat)[:3])
# [np.int64(100), np.int64(2), np.int64(3)]

In NumPy 2.x, the repr of a NumPy scalar shows its type (np.int64(100)); use tolist() to get plain Python numbers.

Common Pitfalls

  • Modifying the output of ravel and accidentally changing the original.

  • Expecting ravel to always avoid copies (non-contiguous arrays are copied).

  • Flattening the batch dimension too (reshape(-1) instead of reshape(n, -1)).

Interview Tips

  • Contrast flatten (copy) with ravel/reshape (view when possible).

  • Mention order “C” vs “F” and flattening for ML features.

17

How do you compute eigenvalues and eigenvectors in NumPy?

Use np.linalg.eig(A) for a general square matrix; it returns the eigenvalues w and a matrix v whose columns are the eigenvectors, so A @ v[:, i] equals w[i] * v[:, i], and the results may be complex even for a real matrix (a 90-degree rotation has eigenvalues ±i). For symmetric (or Hermitian) matrices, such as covariance matrices in PCA, use np.linalg.eigh, which is faster, more accurate, guaranteed to return real eigenvalues sorted in ascending order and orthonormal eigenvectors. np.linalg.eigvals and np.linalg.eigvalsh return only the eigenvalues. Remember that eigenvectors are only defined up to scale and sign (NumPy normalizes them to unit length, but the sign can differ between libraries or versions), and that eig doesn’t sort its output, so sort explicitly with np.argsort when order matters. In NumPy 2.x, eig and eigh return named tuples, so you can write res.eigenvalues and res.eigenvectors. For very large or sparse matrices where you need only a few eigenpairs, use scipy.sparse.linalg.eigsh.

Read full answer

Applies toNumPy 2.x

Functions

Function Matrix type Output
np.linalg.eig General square Unsorted, possibly complex
np.linalg.eigh Symmetric/Hermitian Real, ascending, orthonormal vectors
np.linalg.eigvals / eigvalsh As above Eigenvalues only
np.linalg.svd Any shape Singular values (related, often more stable)

Code Example

import numpy as np

S = np.array([[2.0, 1.0], [1.0, 2.0]])          # symmetric
vals, vecs = np.linalg.eigh(S)
print(vals.tolist())
# [1.0, 3.0]  (ascending; tiny rounding differences possible)
print(np.allclose(S @ vecs, vecs * vals))        # A v = lambda v for every column
# True
print(np.round(np.abs(vecs), 3).tolist())        # abs: signs are arbitrary
# [[0.707, 0.707], [0.707, 0.707]]

R = np.array([[0.0, -1.0], [1.0, 0.0]])          # 90-degree rotation
res = np.linalg.eig(R)
print(np.sort(res.eigenvalues.imag).tolist(), np.allclose(res.eigenvalues.real, 0))
# [-1.0, 1.0] True   (eigenvalues are -i and +i)

rng = np.random.default_rng(42)
X = rng.normal(size=(200, 3)) * np.array([3.0, 1.0, 0.2])
cov = np.cov(X, rowvar=False)
w, v = np.linalg.eigh(cov)
order = np.argsort(w)[::-1]                      # largest first, as PCA wants
print(np.argmax(np.abs(v[:, order[0]])))         # top component points along the high-variance axis
# 0
print(np.isclose(w.sum(), np.trace(cov)))
# True

The sum of the eigenvalues equals the trace, and their product equals the determinant.

Common Pitfalls

  • Using eig on symmetric matrices instead of eigh.

  • Reading eigenvectors from rows instead of columns.

  • Comparing eigenvectors without accounting for sign.

  • Assuming eig returns sorted or real values.

Interview Tips

  • Show eig vs eigh and the column convention, verified with A @ v.

  • Connect to PCA and mention sign ambiguity and sorting.

18

How do slicing and indexing work with NumPy arrays?

NumPy indexing selects elements with square brackets, one index or slice per dimension separated by commas: a[i] takes one element (negative indices count from the end), a[start:stop:step] takes a range with the stop excluded, M[i, j] selects a single element of a 2-D array, M[1] a whole row, M[:, 2] a whole column, and M[1:, 1:3] a sub-block. Basic slicing returns a view that shares memory with the original, so assigning to it changes the source. Advanced indexing returns a copy: integer-array (fancy) indexing like M[[0, 2]] picks arbitrary rows, M[[0, 2], [1, 3]] picks pairs of coordinates (elements (0, 1) and (2, 3)), and boolean masks like a[a > 5] pick elements where a condition holds. Other tools include the ellipsis ... (all remaining dimensions, so x[..., 0] takes the first channel of an image batch), np.newaxis/None to insert a dimension, and np.ix_ to select a rectangular block from lists of rows and columns. Indexing with an integer drops a dimension while slicing keeps it, so M[1] has shape (4,) but M[1:2] has shape (1, 4).

Read full answer

Applies toNumPy 2.x

Indexing Types

Syntax Kind Returns
a[3], a[-1] Integer Element (drops a dim)
a[2:7:2], a[::-1] Slice View
M[1, 2], M[:, 0], M[1:, :2] Multi-dim Element / view
M[[0, 2]] Fancy (integer array) Copy
a[a > 5] Boolean mask Copy (1-D)
x[..., 0] Ellipsis View
a[:, None] New axis View
M[np.ix_(rows, cols)] Outer selection Copy

Code Example

import numpy as np

a = np.arange(10)
print(a[3], a[-1], a[2:7:2].tolist(), a[-3:].tolist(), a[::-1][:3].tolist())
# 3 9 [2, 4, 6] [7, 8, 9] [9, 8, 7]

M = np.arange(12).reshape(3, 4)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]
print(M[1, 2], M[1].tolist(), M[:, 2].tolist(), M[1:, 1:3].tolist())
# 6 [4, 5, 6, 7] [2, 6, 10] [[5, 6], [9, 10]]
print(M[1].shape, M[1:2].shape)
# (4,) (1, 4)

print(M[[0, 2]].tolist(), M[[0, 2], [1, 3]].tolist())
# [[0, 1, 2, 3], [8, 9, 10, 11]] [1, 11]
print(M[np.ix_([0, 2], [1, 3])].tolist())
# [[1, 3], [9, 11]]
print(M[M % 5 == 0].tolist())
# [0, 5, 10]

block = M[:2, :2]
block[:] = -1                       # slice is a view: M changes
print(M[0].tolist())
# [-1, -1, 2, 3]

imgs = np.zeros((8, 32, 32, 3))
print(imgs[..., 0].shape, a[:, None].shape)
# (8, 32, 32) (10, 1)

Common Pitfalls

  • Forgetting slices are views and modifying the original.

  • Expecting M[[0, 2], [1, 3]] to return a 2×2 block (use np.ix_).

  • Chained assignment with advanced indexing (a[mask][0] = 5 changes a copy, not a).

  • Off-by-one errors with exclusive stop indices.

Interview Tips

  • Cover basic slicing (views) vs fancy and boolean indexing (copies).

  • Show multi-dimensional slices, ellipsis, None and np.ix_.

19

How can you reverse an array in NumPy?

Reverse a 1-D array with the slice a[::-1], which uses a step of −1 and returns a view in constant time without copying any data, or with np.flip(a), which does the same and generalizes to any axis. For multi-dimensional arrays, choose the axis: np.flip(M, axis=0) (same as M[::-1] or np.flipud) reverses the order of rows, np.flip(M, axis=1) (same as M[:, ::-1] or np.fliplr) reverses each row’s columns, and np.flip(M) with no axis reverses all dimensions at once (equivalent to rotating a 2-D array by 180 degrees). Because these are views with negative strides, they are instant but share memory with the original, and some libraries that need contiguous memory (for example torch.from_numpy) reject them, so call .copy() or np.ascontiguousarray when needed. Related operations: np.rot90 rotates by 90 degrees, np.sort(a)[::-1] sorts in descending order, and np.roll shifts elements cyclically, which is not a reversal.

Read full answer

Applies toNumPy 2.x

Options

Goal Code
Reverse 1-D a[::-1] or np.flip(a)
Reverse rows (upside down) M[::-1], np.flip(M, 0), np.flipud(M)
Reverse columns (mirror) M[:, ::-1], np.flip(M, 1), np.fliplr(M)
Reverse every axis np.flip(M)
Descending sort np.sort(a)[::-1]
Rotate 90° np.rot90(M)

Code Example

import numpy as np

a = np.array([1, 2, 3, 4, 5])
r = a[::-1]
print(r.tolist(), np.flip(a).tolist(), r.strides, np.shares_memory(a, r))
# [5, 4, 3, 2, 1] [5, 4, 3, 2, 1] (-8,) True

M = np.array([[1, 2, 3],
              [4, 5, 6]])
print(np.flip(M, axis=0).tolist())
# [[4, 5, 6], [1, 2, 3]]
print(M[:, ::-1].tolist())
# [[3, 2, 1], [6, 5, 4]]
print(np.flip(M).tolist(), np.array_equal(np.flip(M), np.rot90(M, 2)))
# [[6, 5, 4], [3, 2, 1]] True

print(np.sort(np.array([3, 1, 2]))[::-1].tolist())
# [3, 2, 1]

r_copy = a[::-1].copy()
print(r_copy.flags["C_CONTIGUOUS"], r.flags["C_CONTIGUOUS"])
# True False

Common Pitfalls

  • Modifying a reversed view and changing the original.

  • Passing negative-stride views to libraries that require contiguous memory.

  • Reversing the wrong axis in multi-dimensional data (for example image rows vs columns).

  • Confusing np.roll with reversing.

Interview Tips

  • Show [::-1] and explain it is a view with a negative stride.

  • Mention np.flip with an axis and when to copy.

20

How do you filter a NumPy array with a condition?

Write the condition as a vectorized comparison, which produces a boolean array (a mask) of the same shape, and index the array with it: a[a > 0] returns a new 1-D array containing only the elements where the mask is True. Combine conditions with & (and), | (or) and ~ (not), wrapping each comparison in parentheses because these operators bind more tightly than comparisons: a[(a > 0) & (a < 8)]. To replace values instead of removing them, use np.where(cond, x, y) (choose element-wise) or assign through the mask (a[a < 0] = 0, which modifies a in place); to get positions, use np.nonzero(mask) or np.flatnonzero; to count matches, use np.count_nonzero(mask) or mask.sum(); and np.any/np.all test whether any or all elements match. For 2-D data, filter rows with a 1-D mask on the first axis (X[X[:, 0] > 2] keeps rows whose first column exceeds 2). Boolean indexing always returns a copy, and np.isin tests membership in a set of values.

Read full answer

Applies toNumPy 2.x

Tools

Goal Code
Keep matching values a[mask]
Combine conditions (c1) & (c2), ~c1, pipe for or
Replace by condition np.where(cond, x, y), a[mask] = v
Positions np.nonzero(mask), np.flatnonzero(mask)
Count np.count_nonzero(mask)
Filter rows X[X[:, j] > t]
Membership np.isin(a, values)

Code Example

import numpy as np

a = np.array([3, -1, 7, 0, -5, 9])
print(a[a > 0].tolist(), a[(a > 0) & (a < 8)].tolist(), a[~(a > 0)].tolist())
# [3, 7, 9] [3, 7] [-1, 0, -5]
print(np.where(a < 0, 0, a).tolist())
# [3, 0, 7, 0, 0, 9]
print(np.flatnonzero(a > 5).tolist(), np.count_nonzero(a > 0), (a > 8).any())
# [2, 5] 3 True
print(a[np.isin(a, [0, 7, 100])].tolist())
# [7, 0]

X = np.array([[1, 10], [3, 20], [5, 30]])
print(X[X[:, 0] > 2].tolist())
# [[3, 20], [5, 30]]

b = a.copy()
b[b < 0] = 0                                  # in-place replacement
print(b.tolist())
# [3, 0, 7, 0, 0, 9]

try:
    a[a > 0 and a < 8]
except ValueError as exc:
    print("use & instead of and:", "ambiguous" in str(exc))
# use & instead of and: True

Common Pitfalls

  • Using and/or, or forgetting parentheses around each comparison.

  • Expecting a 2-D mask on a 2-D array to keep the shape (it returns a 1-D array).

  • Assuming filtered = a[mask] is a view; changing it doesn’t change a.

Interview Tips

  • Show boolean masks, combined conditions and np.where.

  • Mention row filtering, counting and in-place assignment through masks.

21

How do you compute mean, median and standard deviation in NumPy?

Use np.mean(a), np.median(a) and np.std(a) (or the methods a.mean() and a.std(); median has no method), each of which accepts axis= to compute per column or per row and keepdims=True to keep the reduced dimension for broadcasting. Two details trip people up. First, np.std and np.var default to the population formula (ddof=0, dividing by n); for the sample standard deviation that matches pandas, R and most statistics textbooks, pass ddof=1 (dividing by n − 1). Second, a single NaN makes the result NaN, so use the NaN-aware versions np.nanmean, np.nanmedian and np.nanstd for data with missing values. The mean is sensitive to outliers while the median is robust, so comparing them is a quick check for skew. Related functions are np.average (weighted mean), np.percentile/np.quantile, np.var, np.ptp (range) and np.mean(a, dtype=np.float64) to accumulate low-precision arrays (such as float16 or float32) in higher precision.

Read full answer

Applies toNumPy 2.x

Functions

Statistic Function NaN-aware
Mean np.mean, a.mean() np.nanmean
Weighted mean np.average(a, weights=w) —
Median np.median np.nanmedian
Standard deviation np.std(a, ddof=0 or 1) np.nanstd
Variance np.var np.nanvar
Range np.ptp —

Code Example

import numpy as np

data = np.array([2, 4, 4, 4, 5, 5, 7, 9])
print(np.mean(data), np.median(data), np.std(data))
# 5.0 4.5 2.0
print(round(float(np.std(data, ddof=1)), 3))        # sample std: sqrt(32 / 7)
# 2.138

X = np.array([[1.0, 10.0], [2.0, 20.0], [3.0, 60.0]])
print(X.mean(axis=0).tolist(), np.median(X, axis=0).tolist())
# [2.0, 30.0] [2.0, 20.0]
print(np.round(X.std(axis=0), 2).tolist())
# [0.82, 21.6]

with_nan = np.array([1.0, 2.0, np.nan, 5.0])
print(np.mean(with_nan), np.nanmean(with_nan), np.nanmedian(with_nan))
# nan 2.6666666666666665 2.0

print(np.average([1, 2, 3], weights=[3, 1, 0]))
# 1.25

z = (X - X.mean(axis=0)) / X.std(axis=0)            # standardize each column
print(np.allclose(z.mean(axis=0), 0), np.allclose(z.std(axis=0), 1))
# True True

For column 2 the mean 30 is pulled up by the outlier 60, while the median stays at 20; the population std is sqrt((400 + 100 + 900) / 3) ≈ 21.6.

Common Pitfalls

  • Forgetting ddof=1 when a sample standard deviation is expected (pandas .std() uses ddof=1, NumPy uses 0).

  • NaNs silently turning results into NaN.

  • Using the wrong axis.

  • Accumulating float16 data without a higher-precision dtype.

Interview Tips

  • Show the three functions with axis and ddof.

  • Mention NaN-aware versions and mean vs median robustness.

22

How do you generate random data with NumPy?

Create a generator with rng = np.random.default_rng(seed) and call its methods: rng.random(size) for uniform floats in [0, 1), rng.integers(low, high, size) for integers (high is excluded by default), rng.normal(loc, scale, size) for Gaussian values, rng.uniform, rng.binomial, rng.poisson, rng.exponential and many other distributions, plus rng.choice(a, size, replace=False, p=...) to sample from an array, rng.permutation(a) to return a shuffled copy and rng.shuffle(a) to shuffle in place. This Generator API (with the PCG64 bit generator by default) is the recommended interface since NumPy 1.17; the older np.random.seed / np.random.rand / np.random.randint functions are legacy, rely on hidden global state and are kept only for backward compatibility. Passing a seed makes results reproducible, which matters for experiments, tests and train/test splits; passing an independent generator to each function or component (instead of one global seed) keeps parts of a program from affecting each other, and rng.spawn(n) or np.random.SeedSequence creates independent streams for parallel workers. NumPy’s generators are not suitable for cryptography; use the secrets module for that.

Read full answer

Applies toNumPy 2.x

Common Methods

Method Output
rng.random(size) Uniform floats in [0, 1)
rng.integers(lo, hi, size) Integers in [lo, hi)
rng.normal(mu, sigma, size) Gaussian
rng.uniform(a, b, size) Uniform in [a, b)
rng.choice(a, size, replace, p) Samples from an array
rng.permutation(a) / rng.shuffle(a) Shuffled copy / in place
rng.binomial, rng.poisson, rng.exponential Other distributions
rng.multivariate_normal(mean, cov, size) Correlated Gaussians

Code Example

import numpy as np

rng = np.random.default_rng(42)
u = rng.random(5)
ints = rng.integers(0, 10, size=(2, 3))
gauss = rng.normal(loc=170, scale=10, size=10_000)
print(u.shape, ints.shape, ints.min() >= 0, ints.max() < 10)
# (5,) (2, 3) True True
print(round(float(gauss.mean())), round(float(gauss.std())))
# 170 10

same1 = np.random.default_rng(7).random(3)
same2 = np.random.default_rng(7).random(3)
print(np.array_equal(same1, same2))                       # same seed, same numbers
# True

labels = rng.choice(["cat", "dog"], size=1000, p=[0.9, 0.1])
print(labels.shape, 0.85 < np.mean(labels == "cat") < 0.95)
# (1000,) True

idx = rng.permutation(100)                                # shuffled indices for a split
train_idx, test_idx = idx[:80], idx[80:]
print(len(train_idx), len(test_idx), len(set(idx)))
# 80 20 100

workers = rng.spawn(3)                                    # independent streams for parallel jobs
print(len(workers), type(workers[0]).__name__)
# 3 Generator

The exact values depend on the seed and on NumPy’s algorithms, so the example prints checks rather than the numbers.

Common Pitfalls

  • Using the legacy global np.random.seed API in new code.

  • Re-creating a seeded generator inside a loop, which repeats the same numbers.

  • rng.integers(0, 10) excludes 10 (use endpoint=True to include it).

  • Using NumPy random numbers for security tokens.

Interview Tips

  • Recommend default_rng(seed) and list the main methods.

  • Explain reproducibility and independent streams with spawn.

23

What is the purpose of np.histogram?

np.histogram(a, bins=10, range=None, density=False, weights=None) computes the distribution of data without plotting it: it splits the value range into bins and returns two arrays, the count of values in each bin and the bin edges, which has one more element than the counts. You can pass an integer number of equal-width bins, an explicit sequence of edges (for unequal widths), or a rule name such as "auto", "fd" (Freedman–Diaconis) or "sturges", and np.histogram_bin_edges returns just the edges. All bins are half-open [left, right) except the last, which includes its right edge. With density=True the result is normalized so that the area (count / (n × bin width)) integrates to 1, which estimates a probability density; with weights= each value contributes its weight instead of 1. Uses include exploring feature distributions, building discrete features (bin counts), comparing distributions between training and production data (drift checks such as PSI), and computing image intensity histograms. Related functions are np.histogram2d/np.histogramdd for joint distributions, np.bincount for counting non-negative integers and np.digitize for finding which bin each value falls in. Matplotlib’s plt.hist uses np.histogram under the hood, and plt.stairs(counts, edges) plots precomputed results.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

data = np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
counts, edges = np.histogram(data, bins=4)
print(counts.tolist(), edges.tolist())
# [1, 2, 3, 4] [1.0, 1.75, 2.5, 3.25, 4.0]

dens, _ = np.histogram(data, bins=4, density=True)
print(np.round(dens, 4).tolist(), round(float((dens * np.diff(edges)).sum()), 6))
# [0.1333, 0.2667, 0.4, 0.5333] 1.0

counts2, _ = np.histogram(data, bins=[0, 2, 4])          # [0, 2) and [2, 4]
print(counts2.tolist())
# [1, 9]

print(np.bincount(data).tolist())                        # counts of 0, 1, 2, 3, 4
# [0, 1, 2, 3, 4]
print(np.digitize([0.5, 2.0, 3.9], bins=[1, 2, 3]).tolist())
# [0, 2, 3]

rng = np.random.default_rng(42)
h, xe, ye = np.histogram2d(rng.normal(size=500), rng.normal(size=500), bins=5)
print(h.shape, int(h.sum()))
# (5, 5) 500

With 4 bins over [1, 4], each bin is 0.75 wide, so the density of the last bin is 4 / (10 × 0.75) ≈ 0.5333.

Common Pitfalls

  • Expecting edges to have the same length as counts.

  • Reading density=True values as probabilities (they are densities; multiply by bin width).

  • Comparing histograms of two datasets with different automatic bins; use the same edges.

  • Too few or too many bins hiding the shape.

Interview Tips

  • Explain counts plus edges, half-open bins and density.

  • Mention bincount, digitize, 2-D histograms and drift monitoring.

24

How do you normalize an array in NumPy?

“Normalize” means different things, and each is one vectorized line: min–max scaling maps values to [0, 1] with (x - x.min()) / (x.max() - x.min()); standardization (z-scores) gives mean 0 and standard deviation 1 with (x - x.mean()) / x.std(); and unit-norm (L2) normalization divides each vector by its length with x / np.linalg.norm(x, axis=1, keepdims=True), so every row has length 1. For a feature matrix, compute the statistics per column with axis=0 (and per row with axis=1, keepdims=True for unit norms) so broadcasting applies them correctly. Guard against division by zero for constant columns or zero vectors, either with np.where or by replacing zero denominators with 1. In ML, compute the statistics on the training set only and reuse them for validation and test data to avoid leakage; this is exactly what scikit-learn’s MinMaxScaler, StandardScaler and Normalizer do. Other variants are robust scaling with the median and IQR (resistant to outliers), scaling images by 255, and softmax normalization to turn scores into probabilities.

Read full answer

Applies toNumPy 2.x

Methods

Method Formula Result
Min–max (x − min) / (max − min) Range [0, 1]
Z-score (x − mean) / std Mean 0, std 1
L2 unit norm x / ‖x‖ Length 1 per row
Robust (x − median) / IQR Resistant to outliers
Pixel scaling x / 255 [0, 1] for uint8 images

Code Example

import numpy as np

x = np.array([2.0, 4.0, 6.0, 8.0, 10.0])
print(((x - x.min()) / (x.max() - x.min())).tolist())
# [0.0, 0.25, 0.5, 0.75, 1.0]
print(np.round((x - x.mean()) / x.std(), 3).tolist())     # std = sqrt(8)
# [-1.414, -0.707, 0.0, 0.707, 1.414]

V = np.array([[3.0, 4.0], [1.0, 0.0], [0.0, 0.0]])
norms = np.linalg.norm(V, axis=1, keepdims=True)
unit = V / np.where(norms == 0, 1, norms)                 # avoid division by zero
print(unit.tolist())
# [[0.6, 0.8], [1.0, 0.0], [0.0, 0.0]]

X_train = np.array([[1.0, 100.0], [2.0, 100.0], [3.0, 100.0]])   # column 2 is constant
X_test = np.array([[4.0, 100.0]])
mu, sigma = X_train.mean(axis=0), X_train.std(axis=0)
sigma[sigma == 0] = 1.0
print(np.round((X_train - mu) / sigma, 3).tolist())
# [[-1.225, 0.0], [0.0, 0.0], [1.225, 0.0]]
print(np.round((X_test - mu) / sigma, 3).tolist())        # test uses training statistics
# [[2.449, 0.0]]

img = np.array([[0, 128, 255]], dtype=np.uint8)
print(np.round(img / 255.0, 3).tolist())
# [[0.0, 0.502, 1.0]]

For column 1 of the training data the std is sqrt(2/3) ≈ 0.816, so 1/0.816 ≈ 1.225 and the test value 4 is (4 − 2)/0.816 ≈ 2.449.

Common Pitfalls

  • Normalizing with statistics computed on the whole dataset, including test data.

  • Dividing by zero for constant columns or zero vectors.

  • Normalizing along the wrong axis.

  • Integer arrays with in-place division (x /= ... fails on int dtypes).

Interview Tips

  • Distinguish min–max, z-score and unit-norm normalization.

  • Mention per-column axes, zero guards and fitting on training data only.

25

How do you compute percentiles with NumPy?

Use np.percentile(a, q) with q between 0 and 100, or np.quantile(a, q) with q between 0 and 1; both accept a single value or a list (np.percentile(a, [25, 50, 75])), an axis= for per-column results, and a method= that controls interpolation when the requested position falls between two data points. The default method="linear" places the p-th percentile at position (n − 1) × p/100 in the sorted data and interpolates between neighbours, so for the numbers 1 to 10 the 25th percentile is 3.25 and the median 5.5. Other methods are "lower", "higher", "nearest", "midpoint" and the nine textbook definitions such as "hazen", "weibull" and "median_unbiased" (the argument was called interpolation before NumPy 1.22). Different software uses different defaults, so small differences from Excel or other tools are expected. Use np.nanpercentile when data has NaNs. Percentiles drive the interquartile range (IQR = Q3 − Q1) and the 1.5 × IQR outlier rule, robust scaling, latency monitoring (p95, p99), clipping extreme values (winsorizing) and choosing classification thresholds.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

a = np.arange(1, 11)                                   # 1 .. 10
print(np.percentile(a, [25, 50, 75, 90]).tolist())
# [3.25, 5.5, 7.75, 9.1]
print(np.quantile(a, 0.5), np.median(a))
# 5.5 5.5

for m in ["lower", "higher", "nearest", "midpoint"]:
    print(m, float(np.percentile(a, 75, method=m)))
# lower 7.0
# higher 8.0
# nearest 8.0
# midpoint 7.5

data = np.array([10, 12, 12, 13, 12, 11, 14, 13, 15, 102, 12, 14, 17, 19, 107])
q1, q3 = np.percentile(data, [25, 75])
iqr = q3 - q1
outliers = data[(data < q1 - 1.5 * iqr) | (data > q3 + 1.5 * iqr)]
print(q1, q3, outliers.tolist())
# 12.0 16.0 [102, 107]

X = np.array([[1, 10], [2, 20], [3, 30], [4, 40]])
print(np.percentile(X, 50, axis=0).tolist())
# [2.5, 25.0]

print(np.nanpercentile([1.0, np.nan, 3.0], 50))
# 2.0

clipped = np.clip(data, *np.percentile(data, [5, 95]))   # winsorize at the 5th/95th percentile
print(int(clipped.max()) < 107)
# True

For the 75th percentile of 1–10, the position is 9 × 0.75 = 6.75 (zero-based), between 7 and 8: linear gives 7.75, lower 7, higher and nearest 8, midpoint 7.5. The outlier data has 15 values; sorted, positions 3.5 and 10.5 give Q1 = 12 and Q3 = 16, so the fences are 6 and 22.

Common Pitfalls

  • Passing 0–1 values to percentile or 0–100 values to quantile.

  • Expecting identical results to other tools without matching the method.

  • NaNs propagating; use nanpercentile.

  • Using the old interpolation= keyword (deprecated in favour of method=).

Interview Tips

  • Show percentile/quantile, linear interpolation and methods.

  • Apply to the IQR outlier rule and p95/p99 latency.

26

What is the difference between np.var() and np.std()?

np.var returns the variance, the average squared deviation from the mean, and np.std returns the standard deviation, which is simply the square root of the variance, so np.std(a) == np.sqrt(np.var(a)) for the same arguments. The practical difference is units: variance is in squared units (cm², dollars²), so it is awkward to interpret directly but adds nicely (the variance of a sum of independent variables is the sum of their variances) and appears in formulas such as bias–variance decomposition, PCA’s explained variance and Gaussian likelihoods; the standard deviation is in the original units, so it is the one you report and use for z-scores, error bars and “within two standard deviations” rules. Both share the same arguments: axis, keepdims, dtype and especially ddof, the “delta degrees of freedom”: the divisor is n − ddof, the default ddof=0 gives the population statistic and ddof=1 the unbiased sample variance used by pandas, R and statistics courses. Both have NaN-aware versions, np.nanvar and np.nanstd.

Read full answer

Applies toNumPy 2.x

Comparison

np.var np.std
Definition mean((x − x̄)²) sqrt(var)
Units Squared Original
Typical use Formulas, PCA, adding variances Reporting, z-scores, error bars
ddof 0 = population, 1 = sample Same
NaN-aware np.nanvar np.nanstd

Code Example

import numpy as np

data = np.array([2, 4, 4, 4, 5, 5, 7, 9])        # mean 5, squared deviations sum to 32
print(np.var(data), np.std(data))
# 4.0 2.0
print(round(float(np.var(data, ddof=1)), 4), round(float(np.std(data, ddof=1)), 4))   # divide by 7
# 4.5714 2.1381
print(np.isclose(np.std(data), np.sqrt(np.var(data))))
# True

heights_cm = np.array([160.0, 170.0, 180.0])
print(round(float(np.var(heights_cm)), 2), "cm^2 vs", round(float(np.std(heights_cm)), 2), "cm")
# 66.67 cm^2 vs 8.16 cm

rng = np.random.default_rng(42)
x, y = rng.normal(0, 3, 100_000), rng.normal(0, 4, 100_000)
print(round(float(np.var(x + y))), round(float(np.std(x + y))))   # variances add: 9 + 16 = 25
# 25 5

Common Pitfalls

  • Mixing up ddof=0 and ddof=1 when comparing with pandas or textbooks.

  • Reporting variance where standard deviation was meant (wrong units).

  • Adding standard deviations of independent variables (add variances instead).

Interview Tips

  • Say std is the square root of variance and explain the units.

  • Explain ddof and when variances add.

27

How do you calculate a correlation coefficient with NumPy?

Use np.corrcoef(x, y), which returns the Pearson correlation matrix; for two variables it is a 2×2 matrix with 1s on the diagonal and the correlation between x and y in the off-diagonal entries, so take np.corrcoef(x, y)[0, 1]. The Pearson coefficient r = cov(x, y) / (σx σy) ranges from −1 (perfect negative linear relationship) through 0 (no linear relationship) to 1 (perfect positive). By default corrcoef treats each row as a variable and each column as an observation, so for a usual data matrix with variables in columns pass rowvar=False to get the feature correlation matrix. It measures linear association only: a perfect quadratic relationship can have r = 0, a few outliers can dominate it, and correlation never implies causation. For rank-based (Spearman) correlation that handles monotonic non-linear relationships and outliers, rank the data first (scipy.stats.spearmanr), and for p-values use scipy.stats.pearsonr. np.cov returns the covariance matrix, and np.correlate is a different function that computes the cross-correlation of signals, not the correlation coefficient. A constant variable has zero variance, so its correlation is NaN with a warning.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
print(np.corrcoef(x, 2 * x)[0, 1], np.corrcoef(x, -x + 10)[0, 1])
# 1.0 -1.0  (could print 0.9999999999999999 because of rounding)

w = np.array([2.0, 1.0, 4.0, 3.0, 5.0])
print(round(float(np.corrcoef(x, w)[0, 1]), 3))
# 0.8

cov = np.cov(x, w)[0, 1]                              # sample covariance (ddof=1)
print(round(float(cov / (x.std(ddof=1) * w.std(ddof=1))), 3))
# 0.8

data = np.column_stack([x, w, x ** 2])                # variables in columns
print(np.round(np.corrcoef(data, rowvar=False), 2).shape)
# (3, 3)

sym = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
print(round(float(np.corrcoef(sym, sym ** 2)[0, 1]), 6))   # perfect but non-linear dependency
# 0.0

with np.errstate(invalid="ignore", divide="ignore"):
    print(np.corrcoef(x, np.ones(5))[0, 1])
# nan

For x and w the deviations are [−2, −1, 0, 1, 2] and [−1, −2, 1, 0, 2]; the sum of products is 8 and each sum of squares is 10, so r = 8 / 10 = 0.8.

Common Pitfalls

  • Forgetting rowvar=False for a samples × features matrix.

  • Reading low Pearson r as “no relationship” when the relationship is non-linear.

  • Confusing np.correlate with np.corrcoef.

  • Outliers inflating or hiding correlation.

Interview Tips

  • Show corrcoef(x, y)[0, 1] and explain the formula and range.

  • Mention rowvar, Spearman and linear-only limitations.

28

What do np.cumsum() and np.cumprod() do?

np.cumsum(a) returns the running total, where each output element is the sum of all input elements up to and including that position, and np.cumprod(a) returns the running product; for [1, 2, 3, 4] they give [1, 3, 6, 10] and [1, 2, 6, 24]. Both keep the length of the input (unlike sum), accept axis= (without it they flatten the array first) and dtype= to choose the accumulator type, and have NaN-aware variants np.nancumsum and np.nancumprod that treat NaN as 0 and 1. NumPy 2.1 added the array-API versions np.cumulative_sum and np.cumulative_prod with an include_initial=True option that prepends the starting value. Typical uses are cumulative totals (sales to date), turning periodic returns into growth curves (np.cumprod(1 + r)), cumulative distribution functions (cumsum of normalized histogram counts), fast range sums with prefix sums (sum of a[i:j] = c[j] − c[i]), moving averages, cumulative explained variance in PCA, and undoing np.diff. For running maxima or minima use np.maximum.accumulate. Watch out for overflow: cumulative products of integers wrap around silently, and long float products can underflow, in which case summing logs is safer.

Read full answer

Applies toNumPy 2.x

Code Example

import math

import numpy as np

a = np.array([1, 2, 3, 4])
print(np.cumsum(a).tolist(), np.cumprod(a).tolist())
# [1, 3, 6, 10] [1, 2, 6, 24]

M = np.array([[1, 2, 3], [4, 5, 6]])
print(np.cumsum(M).tolist(), np.cumsum(M, axis=0).tolist(), np.cumsum(M, axis=1).tolist())
# [1, 3, 6, 10, 15, 21] [[1, 2, 3], [5, 7, 9]] [[1, 3, 6], [4, 9, 15]]

returns = np.array([0.10, -0.05, 0.20])
print(np.round(np.cumprod(1 + returns), 3).tolist())     # 1.1, 1.1*0.95, 1.045*1.2
# [1.1, 1.045, 1.254]

x = np.array([3, 1, 4, 1, 5, 9, 2, 6])
prefix = np.concatenate([[0], np.cumsum(x)])
print(prefix[6] - prefix[2], x[2:6].sum())                # range sum from prefix sums
# 19 19

explained = np.array([0.6, 0.25, 0.1, 0.05])
print(np.round(np.cumsum(explained), 2).tolist(), int(np.argmax(np.cumsum(explained) >= 0.9)) + 1)
# [0.6, 0.85, 0.95, 1.0] 3

print(np.maximum.accumulate(x).tolist())
# [3, 3, 4, 4, 5, 9, 9, 9]
print(np.nancumsum([1.0, np.nan, 2.0]).tolist())
# [1.0, 1.0, 3.0]

big = np.cumprod(np.arange(1, 25, dtype=np.int64))       # factorials overflow int64 after 20!
print(int(big[19]) == math.factorial(20), int(big[20]) == math.factorial(21))
# True False

Here three components are enough to keep 90 percent of the variance.

Common Pitfalls

  • Forgetting axis on 2-D arrays (the result is flattened).

  • Integer overflow in cumprod.

  • Off-by-one errors with prefix sums; prepend 0.

Interview Tips

  • Define running sum/product and show the axis behaviour.

  • Give uses: growth curves, prefix sums, CDFs, PCA variance.

29

Why is NumPy faster than pure Python for numerical work?

NumPy is faster because it moves the work out of the Python interpreter and into compiled code operating on compact memory: an array stores raw machine numbers of one type in a contiguous block, and an operation like a + b makes one call into a precompiled C loop that processes all elements, instead of executing Python bytecode for each element. In pure Python, every number is a full object on the heap (about 28 bytes for a small int, plus an 8-byte pointer in the list), and each step of a loop must look up types, dispatch the + method, check for overflow, allocate a new result object and update reference counts. NumPy pays that interpreter overhead once per operation rather than once per element, and its loops benefit from CPU features Python can’t use: contiguous memory that fits cache lines and prefetching, SIMD instructions (SSE, AVX, NEON) that process several numbers per instruction, and for linear algebra highly optimized multithreaded BLAS/LAPACK libraries (OpenBLAS, MKL, Accelerate). The typical result is a 10–100× speed-up for element-wise work and far more for matrix multiplication, along with 4–8× less memory. The gains require vectorized code: looping over a NumPy array element by element in Python is often slower than looping over a list, and each NumPy call has a small fixed overhead, so for a handful of numbers plain Python can win.

Read full answer

Applies toNumPy 2.x

Sources of Speed

Factor Pure Python NumPy
Element storage Boxed objects, pointers Raw values, contiguous
Loop Interpreted bytecode per element Compiled C loop
Type checks Every element Once per operation
CPU features Little SIMD use SIMD, cache-friendly
Linear algebra Python loops BLAS/LAPACK, multithreaded
Memory per float ~32 bytes 8 bytes (float64)

Code Example

import timeit

import numpy as np

n = 1_000_000
xs = list(range(n))
arr = np.arange(n, dtype=np.float64)

t_py = timeit.timeit(lambda: [x * 2.0 + 1.0 for x in xs], number=5)
t_np = timeit.timeit(lambda: arr * 2.0 + 1.0, number=5)
print(f"pure Python {t_py:.3f}s, NumPy {t_np:.3f}s, speed-up about {t_py / t_np:.0f}x")
# Example on a laptop: pure Python 0.250s, NumPy 0.010s, speed-up about 25x (varies by machine)

t_loop = timeit.timeit(lambda: [v * 2.0 for v in arr], number=1)      # Python loop over an array
t_list = timeit.timeit(lambda: [v * 2.0 for v in xs], number=1)
print("element-wise loop over the array is slower than over the list:", t_loop > t_list)
# Usually True: each element becomes a NumPy scalar object

print(np.allclose([x * 2.0 + 1.0 for x in xs[:5]], (arr * 2.0 + 1.0)[:5]))
# True

Timings depend on hardware, NumPy build and Python version; the ratios above are typical, not guaranteed.

Common Pitfalls

  • Using NumPy arrays with Python loops and expecting speed.

  • Creating many temporary arrays in long expressions (memory traffic); use in-place ops or out=.

  • Benchmarking tiny arrays, where call overhead dominates.

Interview Tips

  • Explain contiguous typed memory, compiled loops, SIMD and BLAS.

  • Note that the speed comes from vectorization, not from the array type alone.

30

What is vectorization in NumPy?

Vectorization means expressing a computation as operations on whole arrays instead of writing explicit Python loops over elements, so that NumPy can run the loop in compiled code: y = 3 * x + 2 instead of for i in range(n): y[i] = 3 * x[i] + 2. The building blocks are element-wise arithmetic and ufuncs, broadcasting (combining arrays of different shapes without copying), reductions with axis (sum, mean, max), boolean masks and np.where in place of if statements, fancy indexing in place of lookups, and matrix operations (@, einsum) in place of nested loops. Vectorized code is typically 10–100× faster and shorter, and it reads like the underlying math, which is why ML code computes predictions as X @ w + b and gradients as X.T @ (pred - y) / n for the whole batch at once. The trade-offs are memory, since intermediate arrays are created for each step (an all-pairs distance matrix for 100,000 points needs 80 GB), and readability for logic with complex dependencies between steps, such as recurrences where each value depends on the previous one; for those, consider np.cumsum-style accumulations, chunking, or compiling a loop with Numba. Note that np.vectorize is not real vectorization: it is a convenience wrapper around a Python loop.

Read full answer

Applies toNumPy 2.x

Loop → Vectorized Patterns

Loop pattern Vectorized version
for i: y[i] = f(x[i]) y = np.f(x) (ufunc)
if x[i] > 0: ... else: ... np.where(x > 0, a, b)
Accumulate a total x.sum(axis=...)
Running total np.cumsum(x)
Nested loop over pairs Broadcasting a[:, None] - b[None, :]
Loop of dot products X @ w
Count matches np.count_nonzero(mask)

Code Example

import numpy as np

rng = np.random.default_rng(42)
X = rng.normal(size=(1000, 3))
w = np.array([0.5, -1.0, 2.0])
y = X @ w + rng.normal(0, 0.1, 1000)

def mse_loop(X, y, w):
    total = 0.0
    for i in range(len(X)):
        pred = sum(X[i, j] * w[j] for j in range(X.shape[1]))
        total += (pred - y[i]) ** 2
    return total / len(X)

def mse_vec(X, y, w):
    return np.mean((X @ w - y) ** 2)

print(np.isclose(mse_loop(X, y, w), mse_vec(X, y, w)))
# True

grad = 2 * X.T @ (X @ w - y) / len(X)                 # gradient for the whole batch at once
print(grad.shape)
# (3,)

x = np.array([-2.0, -0.5, 0.0, 1.5])
relu = np.where(x > 0, x, 0.0)                         # replaces an if/else loop
print(relu.tolist(), np.maximum(x, 0).tolist())
# [0.0, 0.0, 0.0, 1.5] [0.0, 0.0, 0.0, 1.5]

a, b = np.array([1, 5, 9]), np.array([2, 6])
print(np.abs(a[:, None] - b[None, :]).tolist())       # all pairwise distances, no nested loop
# [[1, 5], [3, 1], [7, 3]]

Common Pitfalls

  • Treating np.vectorize as a speed-up.

  • Huge intermediate arrays from broadcasting; process in chunks.

  • Forcing sequential recurrences into vectorized form; use Numba or a loop.

Interview Tips

  • Define vectorization and give before/after code.

  • Mention broadcasting, np.where, memory trade-offs and np.vectorize.

31

What are strides in a NumPy array?

Strides are a tuple giving, for each dimension, the number of bytes NumPy must jump in memory to move to the next element along that dimension; together with the data pointer, shape and dtype, they fully describe how the flat block of memory is interpreted as an n-dimensional array. For a C-ordered (row-major) int64 array of shape (3, 4), the strides are (32, 8): moving one column means skipping 8 bytes (one element), and moving one row means skipping 32 bytes (four elements). Strides are what make many operations free: transposing just swaps the strides to (8, 32), slicing with a step multiplies them (a[::2] has row stride 64), reversing uses negative strides, and broadcasting uses a stride of 0 so one value repeats without copying. They also explain performance: iterating along the axis with the smallest stride touches memory sequentially and uses the CPU cache well, while walking along a large stride jumps around and is slower; that is why summing over rows vs columns of a large C-ordered array can differ in speed, and why some functions call np.ascontiguousarray first. Fortran (column-major) order reverses the pattern, with strides (8, 24) for the same shape.

Read full answer

Applies toNumPy 2.x

Strides for an int64 Array of Shape (3, 4)

Array Shape Strides Contiguous?
a (C order) (3, 4) (32, 8) C
a.T (4, 3) (8, 32) F
a[::2] (2, 4) (64, 8) No
a[:, ::2] (3, 2) (32, 16) No
a[::-1] (3, 4) (-32, 8) No
np.asfortranarray(a) (3, 4) (8, 24) F
np.broadcast_to(a[0], (5, 4)) (5, 4) (0, 8) No

Code Example

import numpy as np

a = np.arange(12, dtype=np.int64).reshape(3, 4)
print(a.strides, a.T.strides, a[::2].strides, a[:, ::2].strides, a[::-1].strides)
# (32, 8) (8, 32) (64, 8) (32, 16) (-32, 8)

f = np.asfortranarray(a)
print(f.strides, f.flags["F_CONTIGUOUS"], a.flags["C_CONTIGUOUS"])
# (8, 24) True True

b = np.broadcast_to(a[0], (5, 4))                 # repeats a row without copying
print(b.strides, b.flags["WRITEABLE"])
# (0, 8) False

# The address of element [i, j] is base + i * strides[0] + j * strides[1]
i, j = 2, 3
offset = i * a.strides[0] + j * a.strides[1]
print(offset // a.itemsize, a.ravel()[offset // a.itemsize], a[i, j])
# 11 11 11

print(np.shares_memory(a, a.T), a.T.flags["C_CONTIGUOUS"])
# True False

Common Pitfalls

  • Assuming every array is contiguous; views often are not.

  • Writing to arrays with zero strides (broadcast views are read-only for this reason).

  • Looping over the large-stride axis in hot code; reorder loops or copy to the right layout.

Interview Tips

  • Define strides as byte steps per dimension and compute (32, 8) for a (3, 4) int64 array.

  • Explain how transpose, slicing and broadcasting use strides without copying.

32

How do you find the memory size of a NumPy array?

Use a.nbytes, which returns the number of bytes used by the array’s elements and equals a.size * a.itemsize, for example 8,000,000 bytes for a 1000×1000 float64 array. sys.getsizeof(a) adds the small fixed overhead of the array object itself (roughly a hundred bytes for the header) and includes the data buffer only when the array owns it, so for a view it reports just the header; nbytes on a view reports the size of the elements it spans, not new memory, because views share the original buffer. To see how much memory is really allocated, check a.base and a.flags["OWNDATA"], or np.shares_memory. Memory is controlled mainly by the dtype: switching from float64 to float32 halves it, and small integer types (int8, uint8) or bool use one byte per element, which matters for large datasets and image batches. Object arrays report 8 bytes per element in nbytes because they store only pointers, while the Python objects they point to use much more. For monitoring a whole program, use tools such as tracemalloc (NumPy reports its allocations to it) or memory profilers.

Read full answer

Applies toNumPy 2.x

Memory for 1,000,000 Elements

dtype itemsize nbytes
float64 8 8 MB
float32 4 4 MB
float16 2 2 MB
int8 / uint8 / bool 1 1 MB
complex128 16 16 MB

Code Example

import sys
import tracemalloc

import numpy as np

a = np.zeros((1000, 1000))
print(a.nbytes, a.size * a.itemsize, a.astype(np.float32).nbytes)
# 8000000 8000000 4000000

overhead = sys.getsizeof(a) - a.nbytes
print(0 < overhead < 500)                      # small header on top of the data
# True

view = a[:10]
print(view.nbytes, view.flags["OWNDATA"], sys.getsizeof(view) < 500)
# 80000 False True

imgs = np.zeros((64, 224, 224, 3), dtype=np.uint8)
print(f"{imgs.nbytes / 1024**2:.1f} MiB as uint8, {imgs.astype(np.float32).nbytes / 1024**2:.1f} MiB as float32")
# 9.2 MiB as uint8, 36.8 MiB as float32

tracemalloc.start()
b = np.ones(1_000_000)
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print(current >= b.nbytes)
# True

A batch of 64 RGB images of 224 × 224 is 9,633,792 bytes as uint8, four times that as float32.

Common Pitfalls

  • Using sys.getsizeof on views or len(a) as a proxy for memory.

  • Counting a view’s nbytes as extra memory.

  • Converting uint8 images to float64 by default (img / 255 creates float64, 8× larger).

  • Forgetting object arrays hide most of their memory in Python objects.

Interview Tips

  • Answer with nbytes = size × itemsize and mention getsizeof differences.

  • Show how dtype choice cuts memory.

33

What are in-place operations in NumPy and why use them?

An in-place operation writes its result into an existing array’s memory instead of allocating a new array: the augmented operators a += b, a -= b, a *= 2, a /= 2, the out= argument of ufuncs (np.multiply(a, 2, out=a), np.exp(x, out=x)), slice assignment (a[:] = ..., a[mask] = 0) and methods such as a.sort(), a.fill(0) and np.clip(a, 0, 1, out=a). By contrast, a = a + b computes a new array and rebinds the name, leaving the old one to be garbage-collected. In-place operations save memory and allocation time, which matters for large arrays or tight loops (they avoid temporary arrays in multi-step expressions), and because every view of the array sees the change. The cautions are: the result must fit the array’s dtype (a /= 2 on an integer array raises a casting error, and a += 0.5 on ints is refused for the same reason); other references and views to the same data change too, which can surprise code that expected the original; and frameworks with automatic differentiation (PyTorch autograd, JAX) restrict or forbid in-place changes because they need the original values to compute gradients, which is why JAX arrays are immutable and use x.at[i].set(v).

Read full answer

Applies toNumPy 2.x

In-Place vs New Array

New array In place
a = a + b a += b or np.add(a, b, out=a)
a = np.exp(a) np.exp(a, out=a)
b = np.sort(a) a.sort()
a = np.where(a < 0, 0, a) a[a < 0] = 0
a = np.clip(a, 0, 1) np.clip(a, 0, 1, out=a)

Code Example

import numpy as np

a = np.arange(5, dtype=np.float64)
addr = a.__array_interface__["data"][0]
a += 10
np.multiply(a, 2, out=a)
print(a.tolist(), a.__array_interface__["data"][0] == addr)    # same buffer
# [20.0, 22.0, 24.0, 26.0, 28.0] True

b = a
a = a + 1                                     # new array: b keeps the old values
print(b[0], a[0])
# 20.0 21.0

v = b[:2]
b *= 0.5                                      # views see in-place changes
print(v.tolist())
# [10.0, 11.0]

ints = np.array([1, 2, 3])
try:
    ints /= 2
except TypeError:                     # NumPy raises a casting error (a TypeError subclass)
    print("cannot store float results in an int array")
# cannot store float results in an int array

# Avoid temporaries in a multi-step expression: y = 3*x**2 + 2*x
x = np.linspace(0, 1, 5)
y = np.square(x)
y *= 3
y += 2 * x
print(np.allclose(y, 3 * x**2 + 2 * x))
# True

Common Pitfalls

  • Casting errors with integer arrays.

  • Unexpected changes to other views or references of the same data.

  • In-place changes on arrays that are shared with data loaders or caches.

  • Using in-place ops in autograd frameworks.

Interview Tips

  • Contrast a += b with a = a + b and show out=.

  • Mention memory benefits, dtype casting and shared views.

34

How do NumPy dtypes affect memory usage?

Memory use is the number of elements times the dtype’s itemsize, so the dtype is the main lever: float64 uses 8 bytes per element, float32 4, float16 2, and int8, uint8 and bool just 1, meaning a 10-million-element array takes 80 MB as float64 but 10 MB as uint8. Choosing the smallest type that safely holds your values can cut memory 2–8×, which also speeds up computation because more data fits in CPU caches and less needs to move through memory (and GPUs and SIMD units process more 32-bit or 16-bit numbers per instruction). The trade-offs are range and precision: uint8 holds only 0–255 and wraps on overflow, int32 overflows past about 2.1 billion, float32 has about 7 significant digits and float16 only about 3 (it can’t even represent 2049 exactly and overflows past 65504). Typical choices in ML are uint8 for raw images, float32 for model inputs and weights, float16/bfloat16 for mixed-precision training on GPUs, int32 or smaller for category codes and indices, and float64 when accumulating sums or doing statistics that need precision. Convert with astype, check limits with np.iinfo and np.finfo, and let np.min_scalar_type suggest the smallest type for a value. Strings and objects are expensive: fixed-width Unicode uses 4 bytes per character for the longest string, which is why NumPy 2.0 added the variable-length StringDType.

Read full answer

Applies toNumPy 2.x

Size vs Range

dtype Bytes Range / precision
bool, int8, uint8 1 uint8: 0–255
int16, float16 2 float16: ~3 digits, max 65504
int32, float32 4 int32: ±2.1e9; float32: ~7 digits
int64, float64 8 float64: ~16 digits
U20 80 20 Unicode characters × 4 bytes

Code Example

import numpy as np

n = 10_000_000
for dt in [np.float64, np.float32, np.float16, np.uint8]:
    print(np.dtype(dt).name, np.zeros(n, dtype=dt).nbytes // 1_000_000, "MB")
# float64 80 MB
# float32 40 MB
# float16 20 MB
# uint8 10 MB

print(float(np.float16(2049)), float(np.float16(70000)))   # precision and range limits
# 2048.0 inf
print(np.float32(0.1) == 0.1, np.float64(0.1) == 0.1)
# False True

print(np.min_scalar_type(200), np.min_scalar_type(-200), np.min_scalar_type(70000))
# uint8 int16 uint32

ages = np.array([23, 45, 31, 67], dtype=np.int64)
small = ages.astype(np.uint8)
print(ages.nbytes, small.nbytes, np.array_equal(ages, small))
# 32 4 True

print(np.array(["a", "bbbbbbbbbbbbbbbbbbbb"]).itemsize)    # every element sized for the longest
# 80

total32 = np.ones(10_000_000, dtype=np.float32).sum(dtype=np.float32)
print(total32 == 10_000_000)
# True (every partial sum is an integer below 2**24, so float32 is exact here)

np.float16(70000) overflows to infinity and may emit a RuntimeWarning.

Common Pitfalls

  • Downcasting values that don’t fit (silent wrap-around with astype).

  • Accumulating long float16/float32 sums without a higher-precision accumulator.

  • Implicit upcasting to float64 (for example uint8_image / 255).

  • Large fixed-width string arrays.

Interview Tips

  • Explain bytes per element × count and give the common sizes.

  • Discuss range/precision trade-offs and typical ML choices.

35

How can you manipulate strides in NumPy?

You change how memory is viewed without copying it, either indirectly through operations that adjust strides (transpose, slicing with steps, reshape, swapaxes, np.broadcast_to) or directly with np.lib.stride_tricks: sliding_window_view(x, window_shape) safely creates overlapping windows, and as_strided(x, shape, strides) builds any view you specify by hand. The classic use is a sliding window: for a 1-D float64 array, a view with shape (n − w + 1, w) and strides (8, 8) makes each row start one element after the previous one, so rows overlap and moving averages, rolling statistics, 1-D convolutions and image patch extraction become vectorized operations with no data copied. broadcast_to uses a stride of 0 to repeat data for free. as_strided is dangerous: it does no bounds checking, so wrong shapes or strides read memory outside the array (garbage values or crashes), and writing to overlapping views changes many elements at once; NumPy’s documentation advises avoiding it when possible, preferring sliding_window_view (NumPy 1.20+), and passing writeable=False. Remember that the window view itself is cheap, but operations on it (such as .mean(axis=-1) on a huge window) still compute over n × w elements, and any copy of it is that large.

Read full answer

Applies toNumPy 2.x

Tools

Tool Effect
a.T, np.swapaxes, np.moveaxis Permute strides
a[::k] Multiply a stride by k
np.broadcast_to Stride 0, repeats data
sliding_window_view(x, w) Safe overlapping windows (read-only)
as_strided(x, shape, strides) Arbitrary view, no safety checks

Code Example

import numpy as np
from numpy.lib.stride_tricks import as_strided, sliding_window_view

x = np.arange(6, dtype=np.float64)
win = sliding_window_view(x, 3)
print(win.tolist(), win.strides, np.shares_memory(x, win))
# [[0.0, 1.0, 2.0], [1.0, 2.0, 3.0], [2.0, 3.0, 4.0], [3.0, 4.0, 5.0]] (8, 8) True
print(win.mean(axis=1).tolist())                     # moving average, no loop
# [1.0, 2.0, 3.0, 4.0]

manual = as_strided(x, shape=(4, 3), strides=(8, 8), writeable=False)
print(np.array_equal(manual, win))
# True

img = np.arange(16).reshape(4, 4)
patches = sliding_window_view(img, (2, 2))           # every 2x2 patch
print(patches.shape, patches[1, 2].tolist())
# (3, 3, 2, 2) [[6, 7], [10, 11]]
print(patches[::2, ::2].shape)                       # non-overlapping patches by stepping
# (2, 2, 2, 2)

rep = np.broadcast_to(np.array([1, 2, 3]), (1000, 3))
print(rep.strides, rep.nbytes, np.array([1, 2, 3]).nbytes)
# (0, 8) 24000 24

nbytes reports the size the view spans (24,000 bytes) even though only 24 bytes of memory exist.

Common Pitfalls

  • Using as_strided with wrong strides, which reads out-of-bounds memory.

  • Writing to overlapping views.

  • Forgetting that materializing a window view (.copy()) multiplies memory by the window size.

Interview Tips

  • Explain sliding windows with strides (8, 8) and zero-stride broadcasting.

  • Recommend sliding_window_view over as_strided.

36

What is a NumPy record array?

A record array (np.recarray, created with np.rec.array, np.rec.fromrecords or arr.view(np.recarray)) is a structured array whose named fields can be read as attributes, so you can write people.age instead of people["age"]; each element is a record (a row) made of named fields with their own dtypes, stored together in one contiguous block like a C struct array. It is a thin convenience layer over structured arrays: the memory layout, dtype and field access with brackets are identical, and elements are np.record objects whose fields are also accessible as attributes. Record arrays were popular for tabular data before pandas; today they are mainly used for reading binary files and C structs (for example data from instruments or legacy formats), interfacing with libraries that use structured dtypes (such as HDF5 via h5py or astronomy’s FITS files), and lightweight tables where pandas is unnecessary. Downsides: attribute access is a little slower, fields whose names clash with array attributes (like shape or size) can only be reached with brackets, and most numerical work is easier with plain arrays per column or a pandas DataFrame (pd.DataFrame(rec) converts directly).

Read full answer

Applies toNumPy 2.x

Structured Array vs Record Array

Structured array Record array
Creation np.array(rows, dtype=[...]) np.rec.array(...), .view(np.recarray)
Field access arr["age"] arr.age or arr["age"]
Element type np.void np.record
Memory layout Same Same

Code Example

import numpy as np

rows = [("Ana", 31, 72.5), ("Ben", 25, 80.1), ("Chen", 42, 65.0)]
people = np.rec.fromrecords(rows, names="name,age,weight")
print(people.dtype.names, people.age.tolist(), people[0].name)
# ('name', 'age', 'weight') [31, 25, 42] Ana
print(people.weight.mean().round(2), people[people.age > 30].name.tolist())
# 72.53 ['Ana', 'Chen']

dt = np.dtype([("id", "i4"), ("score", "f8")])
structured = np.array([(1, 9.5), (2, 7.0)], dtype=dt)
rec = structured.view(np.recarray)
rec.score += 0.5                                  # same memory as `structured`
print(structured["score"].tolist(), type(rec[0]).__name__)
# [10.0, 7.5] record

raw = structured.tobytes()                        # binary records, like a C struct file
back = np.frombuffer(raw, dtype=dt).view(np.recarray)
print(back.id.tolist(), dt.itemsize)
# [1, 2] 12

The mean weight is (72.5 + 80.1 + 65.0) / 3 ≈ 72.53.

Common Pitfalls

  • Field names that collide with array attributes.

  • Using record arrays for heavy analytics instead of pandas or column arrays.

  • Byte-order and alignment mismatches when reading binary data (specify <i4, >f8 explicitly).

Interview Tips

  • Describe it as a structured array with attribute access.

  • Mention binary files, C structs and conversion to pandas.

37

What are masked arrays in NumPy?

A masked array (numpy.ma.MaskedArray) pairs a normal data array with a boolean mask of the same shape, where True marks elements that are invalid or missing; NumPy’s numpy.ma functions and methods then ignore masked elements, so mean, sum, std, min and arithmetic work only on the valid data, and results carry the mask forward. You create them with np.ma.masked_array(data, mask=...), or with helpers such as np.ma.masked_invalid (mask NaN and inf), np.ma.masked_equal(a, -999) (mask a sentinel value), np.ma.masked_where(cond, a) and masked_greater/masked_less. Use .filled(value) to produce a plain array with masked positions replaced, .compressed() to get the valid values as a 1-D array, .mask and .data to inspect the parts, and np.ma.count to count valid elements. Operations that would be invalid, such as division by zero or the log of a negative number, produce masked results instead of warnings and NaNs. Masked arrays are useful for sensor or satellite data with sentinel values, images with invalid pixels, and keeping the original data intact while excluding values; for most tabular missing-data work, NaN with np.nan* functions or pandas is simpler and faster, since numpy.ma is slower than plain NumPy.

Read full answer

Applies toNumPy 2.x

Key Functions

Function Purpose
np.ma.masked_array(data, mask) Create from data and mask
np.ma.masked_invalid(a) Mask NaN/inf
np.ma.masked_equal(a, v) Mask a sentinel
np.ma.masked_where(cond, a) Mask by condition
m.filled(v) Plain array with fill value
m.compressed() Valid values only
np.ma.count(m) Number of valid elements

Code Example

import numpy as np

readings = np.array([21.5, 22.0, -999.0, 23.5, -999.0])     # -999 means "sensor error"
m = np.ma.masked_equal(readings, -999.0)
print(m.mean(), np.ma.count(m), m.compressed().tolist())
# 22.333333333333332 3 [21.5, 22.0, 23.5]
print(m.filled(np.nan).tolist())
# [21.5, 22.0, nan, 23.5, nan]
print(readings.mean())                                       # the plain mean is wrong
# -386.2

x = np.ma.masked_invalid(np.array([1.0, np.nan, 4.0, np.inf]))
print(x.sum(), x.mask.tolist())
# 5.0 [False, True, False, True]

safe = np.ma.log(np.array([1.0, -1.0, np.e]))              # invalid domain becomes masked
print(safe.round(3).tolist())
# [0.0, None, 1.0]

grid = np.ma.masked_array([[1, 2], [3, 4]], mask=[[False, True], [False, False]])
print(grid.mean(axis=0).tolist(), grid.mean(axis=1).tolist())
# [2.0, 4.0] [1.0, 3.5]

tolist() turns masked entries into None.

Common Pitfalls

  • Mixing masked arrays with regular NumPy functions that ignore the mask (use np.ma.* versions).

  • Assuming masked arrays are as fast as plain arrays.

  • Losing the mask by accessing .data directly.

Interview Tips

  • Explain data plus mask and that statistics skip masked values.

  • Compare with NaN-based handling and pandas.

38

What is the difference between np.vstack() and np.hstack()?

np.vstack stacks arrays vertically, one below another, joining them along the first axis (rows), while np.hstack stacks them horizontally, side by side, joining along the second axis (columns) for 2-D arrays. So for two (2, 3) matrices, vstack gives (4, 3) and hstack gives (2, 6). They differ for 1-D inputs: vstack first turns each 1-D array of length n into a row of shape (1, n), so two vectors of length 3 become a (2, 3) matrix, whereas hstack joins 1-D arrays end to end, so the same vectors become one vector of length 6. The shape requirement follows: vstack needs the same number of columns (and identical trailing dimensions), hstack needs the same number of rows. Both are convenience wrappers around np.concatenate (vstack is concatenate on axis 0 after atleast_2d, hstack is concatenate on axis 1, or axis 0 for 1-D). Related helpers are np.column_stack (puts 1-D arrays as columns, the usual way to build a feature matrix from separate feature vectors), np.dstack (along a third, depth axis) and np.stack (along a new axis of your choice). Like all concatenation, they copy data, so build lists and stack once.

Read full answer

Applies toNumPy 2.x

Comparison

np.vstack np.hstack
Direction Vertical (rows) Horizontal (columns)
Axis (2-D) 0 1
1-D inputs Become rows → 2-D Joined end to end → 1-D
Must match Number of columns Number of rows
Two (2, 3) arrays (4, 3) (2, 6)
Two (3,) arrays (2, 3) (6,)

Code Example

import numpy as np

A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8, 9], [10, 11, 12]])
print(np.vstack([A, B]).shape, np.hstack([A, B]).shape)
# (4, 3) (2, 6)
print(np.hstack([A, B]).tolist())
# [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

x, y = np.array([1, 2, 3]), np.array([4, 5, 6])
print(np.vstack([x, y]).tolist(), np.hstack([x, y]).tolist())
# [[1, 2, 3], [4, 5, 6]] [1, 2, 3, 4, 5, 6]
print(np.column_stack([x, y]).tolist(), np.dstack([x, y]).shape)
# [[1, 4], [2, 5], [3, 6]] (1, 3, 2)

print(np.array_equal(np.vstack([A, B]), np.concatenate([A, B], axis=0)),
      np.array_equal(np.hstack([A, B]), np.concatenate([A, B], axis=1)))
# True True

bias = np.ones((2, 1))
print(np.hstack([bias, A]).tolist())                  # add a bias column
# [[1.0, 1.0, 2.0, 3.0], [1.0, 4.0, 5.0, 6.0]]

Common Pitfalls

  • Expecting hstack of 1-D arrays to create columns (use column_stack).

  • Mismatched sizes in the non-stacking dimension.

  • Stacking inside a loop.

Interview Tips

  • Give the axis and the shape results for 2-D and 1-D inputs.

  • Mention column_stack, dstack, stack and concatenate.

39

What is a structured array in NumPy?

A structured array is a NumPy array whose dtype is a composite of named fields, each with its own type, like a row in a table or a C struct; you define it as a list of (name, type) pairs, for example np.dtype([("name", "U10"), ("age", "i4"), ("score", "f8")]), and each element of the array is one record with those fields stored side by side in memory. You access a whole column with arr["age"], one record with arr[0], one value with arr[0]["age"], and several fields at once with arr[["name", "score"]]; you can sort by fields (np.sort(arr, order="age")), filter with boolean masks on a field, and convert to pandas with pd.DataFrame(arr). The dtype’s itemsize is the sum of the field sizes (52 bytes in the example above, since U10 takes 40), unless you request C-style padding with align=True. Structured arrays shine when you need a compact, fixed binary layout: reading or writing binary files and network packets, mapping C structs, memory-mapped data, and libraries like h5py. For analysis they are less convenient than pandas (no index, limited operations, adding fields means rebuilding the array), and numerical operations work on one field at a time. numpy.lib.recfunctions provides helpers to add, drop, join and convert fields (for example structured_to_unstructured).

Read full answer

Applies toNumPy 2.x

Operations

Task Code
Define dtype np.dtype([("name", "U10"), ("age", "i4")])
Field (column) arr["age"]
Several fields arr[["name", "age"]]
Sort by field np.sort(arr, order="age")
Filter arr[arr["age"] > 30]
To plain 2-D float array recfunctions.structured_to_unstructured
To pandas pd.DataFrame(arr)

Code Example

import numpy as np
from numpy.lib import recfunctions as rfn

dt = np.dtype([("name", "U10"), ("age", "i4"), ("score", "f8")])
arr = np.array([("Ana", 31, 88.5), ("Ben", 25, 92.0), ("Chen", 42, 79.5)], dtype=dt)
print(dt.itemsize, arr["age"].tolist(), arr[1]["name"])
# 52 [31, 25, 42] Ben

print(np.sort(arr, order="age")["name"].tolist())
# ['Ben', 'Ana', 'Chen']
print(arr[arr["score"] > 80]["name"].tolist())
# ['Ana', 'Ben']

arr["score"] += 1.0                                   # vectorized per field
print(arr["score"].tolist())
# [89.5, 93.0, 80.5]

numeric = rfn.structured_to_unstructured(arr[["age", "score"]], dtype=np.float64)
print(numeric.shape, numeric[0].tolist())
# (3, 2) [31.0, 89.5]

aligned = np.dtype([("a", "u1"), ("b", "f8")], align=True)
packed = np.dtype([("a", "u1"), ("b", "f8")])
print(packed.itemsize, aligned.itemsize)              # C-style padding when aligned
# 9 16

Common Pitfalls

  • String fields too short (U5 silently truncates longer names).

  • Expecting DataFrame-like operations (joins, group-by) on structured arrays.

  • Byte-order or alignment mismatches with external binary data.

Interview Tips

  • Define named fields with per-field types and show field access and sorting.

  • Mention binary I/O, C structs and when pandas is better.

40

How do you save and load NumPy arrays?

For NumPy’s own binary format, use np.save("x.npy", arr) and np.load("x.npy") for one array, and np.savez("data.npz", X=X, y=y) or np.savez_compressed for several named arrays in one zip file, loaded with data = np.load("data.npz") and accessed as data["X"]. The .npy format stores the dtype, shape and memory order in a small header followed by raw bytes, so it is fast, exact (no precision loss) and portable across machines. For human-readable files, np.savetxt("x.csv", arr, delimiter=",", fmt="%.4f", header=...) and np.loadtxt (or np.genfromtxt, which handles missing values) read and write text, at the cost of size, speed and possibly precision. For arrays larger than memory, np.load(path, mmap_mode="r") memory-maps a .npy file so only the parts you index are read from disk, and np.memmap maps raw binary files. Security matters: np.load refuses object arrays unless you pass allow_pickle=True, because unpickling untrusted files can execute arbitrary code, so only enable it for files you trust. For data shared with other tools, consider HDF5 (h5py), Parquet (via pandas or pyarrow) or Zarr, and for model weights the formats of your framework (for example safetensors).

Read full answer

Applies toNumPy 2.x

Formats

Function Format Notes
np.save / np.load .npy, one array Fast, exact
np.savez / np.savez_compressed .npz, several arrays Zip archive, named
np.savetxt / np.loadtxt Text/CSV Readable, slower, larger
np.genfromtxt Text Handles missing values
np.load(..., mmap_mode="r") .npy Memory-mapped, larger than RAM
arr.tofile / np.fromfile Raw bytes No metadata; avoid for storage

Code Example

import tempfile
from pathlib import Path

import numpy as np

rng = np.random.default_rng(42)
X = rng.normal(size=(100, 3)).astype(np.float32)
y = rng.integers(0, 2, size=100)

with tempfile.TemporaryDirectory() as tmp:
    d = Path(tmp)
    np.save(d / "X.npy", X)
    X2 = np.load(d / "X.npy")
    print(np.array_equal(X, X2), X2.dtype, X2.shape)
    # True float32 (100, 3)

    np.savez_compressed(d / "data.npz", X=X, y=y)
    with np.load(d / "data.npz") as data:
        print(sorted(data.files), data["y"].shape)
    # ['X', 'y'] (100,)

    np.savetxt(d / "X.csv", X[:2], delimiter=",", fmt="%.4f", header="a,b,c", comments="")
    back = np.loadtxt(d / "X.csv", delimiter=",", skiprows=1)
    print(back.shape, np.allclose(back, X[:2], atol=1e-4))
    # (2, 3) True

    big = np.load(d / "X.npy", mmap_mode="r")      # reads lazily from disk
    print(type(big).__name__, big[:2].shape)
    # memmap (2, 3)

    np.save(d / "obj.npy", np.array([{"a": 1}], dtype=object), allow_pickle=True)
    try:
        np.load(d / "obj.npy")                      # pickles refused by default
    except ValueError:
        print("object arrays need allow_pickle=True (only for trusted files)")
    # object arrays need allow_pickle=True (only for trusted files)

Common Pitfalls

  • Loading untrusted files with allow_pickle=True.

  • Saving with text formats and losing precision (choose fmt) or dtype.

  • Forgetting that np.load of an .npz returns a lazy, closable object.

  • Using tofile, which stores no shape or dtype.

Interview Tips

  • Recommend .npy/.npz and explain the header.

  • Mention text formats, memory mapping and the pickle security risk.

41

How do you pad a NumPy array?

Use np.pad(array, pad_width, mode="constant", ...), where pad_width says how many values to add before and after each axis: a single integer pads every side equally, a pair (before, after) applies to all axes, and a tuple of pairs such as ((1, 1), (2, 2)) sets each axis separately. The mode decides what goes into the new cells: "constant" fills with constant_values (0 by default), "edge" repeats the border value, "reflect" mirrors the array without repeating the edge, "symmetric" mirrors including the edge, "wrap" continues periodically from the opposite side, and "mean", "median", "maximum", "minimum" and "linear_ramp" use statistics or gradients; you can even pass a function. Padding always returns a new, larger array. In ML it is used for “same” convolutions (zero padding so output size equals input size), padding variable-length sequences to a common length for batching (often with a dedicated pad value and a mask), image augmentation and border handling with reflect padding, and making sizes divisible by a block size. For images with channels, don’t pad the channel axis: use ((p, p), (p, p), (0, 0)).

Read full answer

Applies toNumPy 2.x

Modes for [1, 2, 3]

Mode pad_width Result
constant (2, 1) [0, 0, 1, 2, 3, 0]
constant, values 9 1 [9, 1, 2, 3, 9]
edge 2 [1, 1, 1, 2, 3, 3, 3]
reflect 2 [3, 2, 1, 2, 3, 2, 1]
symmetric 2 [2, 1, 1, 2, 3, 3, 2]
wrap 1 [3, 1, 2, 3, 1]

Code Example

import numpy as np

a = np.array([1, 2, 3])
print(np.pad(a, (2, 1)).tolist(), np.pad(a, 1, constant_values=9).tolist())
# [0, 0, 1, 2, 3, 0] [9, 1, 2, 3, 9]
for mode in ["edge", "reflect", "symmetric"]:
    print(mode, np.pad(a, 2, mode=mode).tolist())
# edge [1, 1, 1, 2, 3, 3, 3]
# reflect [3, 2, 1, 2, 3, 2, 1]
# symmetric [2, 1, 1, 2, 3, 3, 2]
print(np.pad(a, 1, mode="wrap").tolist())
# [3, 1, 2, 3, 1]

img = np.arange(4).reshape(2, 2)
print(np.pad(img, 1).tolist())
# [[0, 0, 0, 0], [0, 0, 1, 0], [0, 2, 3, 0], [0, 0, 0, 0]]

rgb = np.zeros((32, 32, 3))
print(np.pad(rgb, ((2, 2), (2, 2), (0, 0)), mode="reflect").shape)   # don't pad channels
# (36, 36, 3)

seqs = [np.array([5, 8]), np.array([3, 1, 4, 1]), np.array([9])]
max_len = max(len(s) for s in seqs)
batch = np.stack([np.pad(s, (0, max_len - len(s)), constant_values=-1) for s in seqs])
mask = batch != -1
print(batch.tolist(), mask.sum(axis=1).tolist())
# [[5, 8, -1, -1], [3, 1, 4, 1], [9, -1, -1, -1]] [2, 4, 1]

Common Pitfalls

  • Padding the channel or batch axis by accident with a single integer pad_width.

  • Using reflect with padding larger than the array (NumPy reflects repeatedly, which may be surprising).

  • Choosing a pad value that also appears in real data, instead of keeping a mask.

Interview Tips

  • Explain pad_width per axis and the main modes.

  • Give ML uses: same convolutions and sequence batching.

42

How do you create a 3×3 identity matrix in NumPy?

Call np.eye(3) or np.identity(3); both return a 3×3 float64 array with ones on the main diagonal and zeros elsewhere, and you can pass dtype=int for integers. np.identity(n) only creates square identity matrices, while np.eye(N, M=None, k=0) is more flexible: M sets a different number of columns for a rectangular matrix, and k shifts the diagonal (k=1 above, k=−1 below the main diagonal). The identity matrix I is the neutral element of matrix multiplication (I @ A = A @ I = A), the result of A @ inv(A), and appears in ridge regression (XᵀX + λI), covariance regularization and initializing weights or transforms. A popular trick is one-hot encoding: indexing the identity with class labels, np.eye(n_classes)[labels], gives one row per label with a 1 in that label’s column. Related helpers are np.diag(v) to build a diagonal matrix from a vector (or extract the diagonal of a matrix), np.fill_diagonal to set a diagonal in place, and np.eye(3, dtype=bool) for a diagonal mask.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

I = np.eye(3)
print(I.tolist(), I.dtype)
# [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] float64
print(np.identity(3, dtype=int).tolist())
# [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

print(np.eye(3, k=1, dtype=int).tolist(), np.eye(2, 3, dtype=int).tolist())
# [[0, 1, 0], [0, 0, 1], [0, 0, 0]] [[1, 0, 0], [0, 1, 0]]

A = np.array([[2.0, 1.0, 0.0], [0.0, 3.0, 1.0], [1.0, 0.0, 4.0]])
print(np.array_equal(I @ A, A), np.allclose(A @ np.linalg.inv(A), I))
# True True

labels = np.array([0, 2, 1, 2])
print(np.eye(3, dtype=int)[labels].tolist())          # one-hot encoding
# [[1, 0, 0], [0, 0, 1], [0, 1, 0], [0, 0, 1]]

X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
y = np.array([1.0, 2.0, 3.0])
lam = 0.1
w_ridge = np.linalg.solve(X.T @ X + lam * np.eye(2), X.T @ y)
print(w_ridge.shape)
# (2,)

M = np.zeros((3, 3))
np.fill_diagonal(M, [7, 8, 9])
print(np.diag(M).tolist(), np.array_equal(M, np.diag([7.0, 8.0, 9.0])))
# [7.0, 8.0, 9.0] True

Common Pitfalls

  • Expecting integers by default (the default dtype is float64).

  • Using np.ones((3, 3)) or np.diag with the wrong argument when the identity was meant.

  • Building large identity matrices just to multiply by them (multiplying by I is a no-op; for scaling use a * s).

Interview Tips

  • Answer np.eye(3) and mention np.identity, k and rectangular forms.

  • Show the one-hot trick and ridge regression use.

43

How do you compute a moving average with NumPy?

The simplest way is convolution with a window of equal weights: np.convolve(x, np.ones(w) / w, mode="valid") returns the average of every window of w consecutive values, giving n − w + 1 results. Two equally good alternatives are the cumulative-sum trick, which is O(n) regardless of window size (c = np.cumsum(np.insert(x, 0, 0)); (c[w:] - c[:-w]) / w), and sliding_window_view(x, w).mean(axis=1), which is the most readable and generalizes to other rolling statistics such as the max, median or standard deviation. With mode="same" the output keeps the input length, but the edge values are averaged with implicit zeros and are biased toward 0; to avoid that, either trim, pad with np.pad(..., mode="edge") first, or divide by the actual number of points in each window. For weighted or exponential moving averages, use a weighted kernel or an explicit recurrence (an exponential moving average is a loop or scipy.signal.lfilter), and for time series with dates, pandas rolling and ewm handle windows, missing values and alignment. The cumulative-sum version can accumulate floating-point error on very long series, which the convolution and window-view versions avoid.

Read full answer

Applies toNumPy 2.x

Methods

Method Output length Notes
np.convolve(x, ones(w)/w, "valid") n − w + 1 Simple
Cumulative-sum differences n − w + 1 O(n), any window
sliding_window_view(x, w).mean(1) n − w + 1 Readable, other stats
np.convolve(..., "same") n Edges biased by zeros
pandas rolling(w).mean() n (NaN at start) Dates, missing values

Code Example

import numpy as np
from numpy.lib.stride_tricks import sliding_window_view

x = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
w = 3

conv = np.convolve(x, np.ones(w) / w, mode="valid")
c = np.cumsum(np.insert(x, 0, 0.0))
cums = (c[w:] - c[:-w]) / w
view = sliding_window_view(x, w).mean(axis=1)
print(conv.tolist(), np.allclose(conv, cums), np.allclose(conv, view))
# [2.0, 3.0, 4.0, 5.0] True True

same = np.convolve(x, np.ones(w) / w, mode="same")
print(np.round(same, 3).tolist())                     # edges include implicit zeros
# [1.0, 2.0, 3.0, 4.0, 5.0, 3.667]

counts = np.convolve(np.ones_like(x), np.ones(w), mode="same")   # points per window
print((np.convolve(x, np.ones(w), mode="same") / counts).tolist())
# [1.5, 2.0, 3.0, 4.0, 5.0, 5.5]

weights = np.array([0.2, 0.3, 0.5])                   # newest value weighted most
wma = sliding_window_view(x, 3) @ weights
print(np.round(wma, 2).tolist())
# [2.3, 3.3, 4.3, 5.3]

print(sliding_window_view(x, 3).max(axis=1).tolist())  # rolling max
# [3.0, 4.0, 5.0, 6.0]

For the weighted average of [1, 2, 3]: 0.2 × 1 + 0.3 × 2 + 0.5 × 3 = 2.3.

Common Pitfalls

  • Using mode="same" and trusting the biased edges.

  • Forgetting that np.convolve flips the kernel (it matters for asymmetric weights, so reverse weights or use the window view as above).

  • Python loops over windows for long series.

Interview Tips

  • Give the convolution one-liner, then the cumsum and window-view versions.

  • Discuss edge handling and weighted/exponential variants.

44

How do you create random integers and scale them to 0–1?

Generate the integers with a seeded generator, rng = np.random.default_rng(42) and a = rng.integers(0, 100, size=10), then apply min–max scaling, (a - a.min()) / (a.max() - a.min()), which maps the smallest value to 0, the largest to 1 and everything else proportionally in between. Because NumPy’s true division returns floats, the result is a float64 array even though the input is integer. Two details matter. If all values are equal the denominator is zero, so guard it (for example return zeros in that case). And decide whether you want to scale by the observed range or by the known possible range: dividing by the theoretical range, a / 99 for integers drawn from 0–99, keeps values comparable across samples, whereas min–max on each sample stretches every sample to fill exactly [0, 1]. For image data in uint8, the usual normalization is simply img / 255.0. If you just need random floats in [0, 1), skip the integers and call rng.random(size).

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

rng = np.random.default_rng(42)
a = rng.integers(0, 100, size=10)              # integers in [0, 100)
scaled = (a - a.min()) / (a.max() - a.min())
print(a.dtype, scaled.dtype, scaled.min(), scaled.max())
# int64 float64 0.0 1.0

fixed = np.array([10, 25, 40, 70])
print(((fixed - fixed.min()) / (fixed.max() - fixed.min())).tolist())
# [0.0, 0.25, 0.5, 1.0]
print(np.round(fixed / 99, 3).tolist())         # by the possible range 0-99 instead
# [0.101, 0.253, 0.404, 0.707]

def minmax(x):
    x = np.asarray(x, dtype=np.float64)
    rng_ = x.max() - x.min()
    return np.zeros_like(x) if rng_ == 0 else (x - x.min()) / rng_

print(minmax([5, 5, 5]).tolist(), minmax([3, 6, 9]).tolist())
# [0.0, 0.0, 0.0] [0.0, 0.5, 1.0]

M = rng.integers(0, 256, size=(4, 3))
cols = (M - M.min(axis=0)) / np.ptp(M, axis=0)  # per-column scaling (assumes no constant column)
print(cols.shape, bool(np.all((cols >= 0) & (cols <= 1))))
# (4, 3) True

print(bool(np.all((rng.random(5) >= 0) & (rng.random(5) < 1))))   # direct floats in [0, 1)
# True

Common Pitfalls

  • Dividing by zero when all values are equal.

  • Using // (floor division), which gives integer zeros and ones.

  • Scaling test data with its own min and max instead of the training values.

  • rng.integers(0, 100) never produces 100 (exclusive upper bound).

Interview Tips

  • Show the generator, min–max formula and the zero-range guard.

  • Mention per-column scaling and observed vs theoretical range.

45

How do you find or generate odd numbers in a NumPy array?

To generate odd numbers, use np.arange(1, n, 2) (start at 1, step 2), which gives [1, 3, 5, …] without any condition; to select the odd numbers from an existing array, use a boolean mask with the modulo operator, a[a % 2 == 1] (or a[a % 2 != 0], or the bitwise test a[(a & 1) == 1]). NumPy’s % follows Python’s rule that the result has the sign of the divisor, so −3 % 2 is 1 and the == 1 test also works for negative odd numbers (in C or Java, where −3 % 2 is −1, it would not). The same mask lets you count odds (np.count_nonzero(a % 2)), find their positions (np.flatnonzero(a % 2)), replace them (a[a % 2 == 1] = -1, a classic interview exercise) or transform them with np.where(a % 2 == 1, a * 10, a). These only make sense for integer arrays; for floats, check np.mod(a, 2) == 1 only if the values are whole numbers. Everything is vectorized, so it runs in compiled code without a Python loop.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

print(np.arange(1, 20, 2).tolist())
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(a[a % 2 == 1].tolist(), a[(a & 1) == 1].tolist())
# [1, 3, 5, 7, 9] [1, 3, 5, 7, 9]
print(np.count_nonzero(a % 2), np.flatnonzero(a % 2).tolist())
# 5 [1, 3, 5, 7, 9]

b = a.copy()
b[b % 2 == 1] = -1                                  # replace odd numbers in place
print(b.tolist())
# [0, -1, 2, -1, 4, -1, 6, -1, 8, -1]
print(np.where(a % 2 == 1, a * 10, a).tolist())     # without modifying a
# [0, 10, 2, 30, 4, 50, 6, 70, 8, 90]

neg = np.array([-5, -4, -3, 2, 7])
print((neg % 2).tolist(), neg[neg % 2 == 1].tolist())   # Python-style modulo: sign of divisor
# [1, 0, 1, 0, 1] [-5, -3, 7]

M = np.arange(12).reshape(3, 4)
print(M[M % 2 == 1].tolist())                       # 2-D mask returns a 1-D array
# [1, 3, 5, 7, 9, 11]

Common Pitfalls

  • Porting C-style x % 2 == 1 logic and expecting negative results; in NumPy it works, but np.fmod follows C (sign of the dividend).

  • Applying the test to float arrays with non-integer values.

  • Expecting the 2-D shape to be kept after boolean selection.

Interview Tips

  • Give np.arange(1, n, 2) and the modulo mask.

  • Mention negative-number behaviour and the replace-in-place exercise.

46

How do you compute the outer product of two vectors in NumPy?

Use np.outer(a, b), which returns a matrix whose element (i, j) is a[i] × b[j], so vectors of length m and n produce an m × n matrix; equivalent forms are broadcasting a[:, None] * b[None, :], np.multiply.outer(a, b) and np.einsum("i,j->ij", a, b). The outer product is the opposite of the inner (dot) product: instead of collapsing two vectors to one number, it expands them into a matrix of all pairwise products, which always has rank 1 (every row is a multiple of b). It shows up in many ML and linear-algebra places: covariance as an average of outer products of centred samples, the gradient of a linear layer (outer product of the upstream gradient and the input), Hebbian learning, rank-one updates in quasi-Newton methods like BFGS, the SVD expressed as a sum of rank-one outer products, and simple tables such as a multiplication table. Note that np.outer flattens inputs with more than one dimension; for batched or higher-dimensional outer products use broadcasting or einsum (for example “bi,bj->bij”). Any ufunc has an .outer method, so np.add.outer or np.subtract.outer give all pairwise sums or differences.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5])
O = np.outer(a, b)
print(O.tolist(), O.shape)
# [[4, 5], [8, 10], [12, 15]] (3, 2)
print(np.array_equal(O, a[:, None] * b[None, :]),
      np.array_equal(O, np.multiply.outer(a, b)),
      np.array_equal(O, np.einsum("i,j->ij", a, b)))
# True True True
print(np.linalg.matrix_rank(O), np.dot(a, a))        # outer product has rank 1; inner product is a scalar
# 1 14

print(np.outer(np.arange(1, 4), np.arange(1, 4)).tolist())   # multiplication table
# [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
print(np.subtract.outer(a, a).tolist())              # all pairwise differences
# [[0, -1, -2], [1, 0, -1], [2, 1, 0]]

rng = np.random.default_rng(42)
X = rng.normal(size=(500, 2))
Xc = X - X.mean(axis=0)
cov_outer = sum(np.outer(x, x) for x in Xc) / (len(X) - 1)
print(np.allclose(cov_outer, np.cov(X, rowvar=False)))
# True

batch_u, batch_v = rng.normal(size=(8, 3)), rng.normal(size=(8, 4))
print(np.einsum("bi,bj->bij", batch_u, batch_v).shape)   # batched outer products
# (8, 3, 4)

Common Pitfalls

  • Using np.outer on 2-D inputs, which flattens them silently.

  • Confusing outer with np.dot or @ on 1-D vectors (inner product).

  • Building huge outer products when only their product with another vector is needed (compute a * (b @ v) instead of np.outer(a, b) @ v).

Interview Tips

  • Define element (i, j) = a[i] b[j] and give the shape.

  • Show broadcasting and einsum forms and ML uses like covariance and gradients.

47

How do you make an 8×8 checkerboard with np.tile?

Build the 2×2 repeating unit [[0, 1], [1, 0]] and tile it four times in each direction: np.tile(np.array([[0, 1], [1, 0]]), (4, 4)) returns an 8×8 array in which each cell differs from its horizontal and vertical neighbours, starting with 0 in the top-left corner. np.tile(A, reps) repeats the whole array A the given number of times along each axis, so a (2, 2) unit with reps (4, 4) becomes (8, 8); it copies data into a new array. Swap the unit to [[1, 0], [0, 1]] to start with 1. Alternatives, often asked as follow-ups, are slicing assignment on a zero matrix (Z[1::2, ::2] = 1 and Z[::2, 1::2] = 1), the parity formula (np.add.outer(np.arange(8), np.arange(8)) % 2) or np.indices((8, 8)).sum(axis=0) % 2, and np.kron with a block to make larger squares (each cell becomes a block of pixels, useful for test images). Don’t confuse np.tile (repeats the whole array) with np.repeat (repeats each element).

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

unit = np.array([[0, 1], [1, 0]])
board = np.tile(unit, (4, 4))
print(board.shape, board.sum())
# (8, 8) 32
print(board[:3].tolist())
# [[0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1]]

Z = np.zeros((8, 8), dtype=int)
Z[1::2, ::2] = 1
Z[::2, 1::2] = 1
parity = np.indices((8, 8)).sum(axis=0) % 2
print(np.array_equal(board, Z), np.array_equal(board, parity))
# True True

print(np.tile(np.array([1, 2]), 3).tolist(), np.repeat(np.array([1, 2]), 3).tolist())
# [1, 2, 1, 2, 1, 2] [1, 1, 1, 2, 2, 2]

big = np.kron(board, np.ones((10, 10), dtype=int))     # each cell becomes 10x10 pixels
print(big.shape, big[0, 9], big[0, 10])
# (80, 80) 0 1

Common Pitfalls

  • Tiling a 1-D [0, 1] pattern, which repeats rows identically instead of alternating.

  • Using np.repeat instead of np.tile.

  • Getting the wrong starting colour; swap the unit.

Interview Tips

  • Give the np.tile one-liner and explain the repetition.

  • Show the slicing alternative and the tile vs repeat difference.

48

How do you add a border around a NumPy array?

To surround an existing array with a frame, use np.pad: np.pad(a, pad_width=1, mode="constant", constant_values=0) returns a new array that is two rows and two columns larger, with the original in the middle and the border filled with 0 (or any value you choose). Use a larger pad_width for a thicker border, a per-axis tuple such as ((1, 1), (2, 2)) for different thicknesses, and mode="edge" or "reflect" to extend the content instead of using a fixed value. A related classic exercise is the opposite: making an array whose border is 1 and interior is 0, done with Z = np.ones((5, 5)); Z[1:-1, 1:-1] = 0, which writes zeros into every cell that isn’t on the outer rows and columns (or with np.pad(np.zeros((3, 3)), 1, constant_values=1)). For images with channels, pad only the spatial axes, ((1, 1), (1, 1), (0, 0)). Selecting just the border cells, for example to check that they are all background, uses a mask that is True except for the interior slice.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

a = np.array([[1, 2], [3, 4]])
print(np.pad(a, 1).tolist())
# [[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]]
print(np.pad(a, 1, constant_values=9)[0].tolist(), np.pad(a, ((0, 0), (2, 1))).shape)
# [9, 9, 9, 9] (2, 5)

Z = np.ones((5, 5), dtype=int)
Z[1:-1, 1:-1] = 0                                     # border of ones, zeros inside
print(Z.tolist(), Z.sum())
# [[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]] 16
print(np.array_equal(Z, np.pad(np.zeros((3, 3), dtype=int), 1, constant_values=1)))
# True

img = np.full((4, 4, 3), 200, dtype=np.uint8)
framed = np.pad(img, ((2, 2), (2, 2), (0, 0)), constant_values=0)   # black 2-pixel frame
print(framed.shape, framed[0, 0].tolist(), framed[3, 3].tolist())
# (8, 8, 3) [0, 0, 0] [200, 200, 200]

border = np.ones((5, 5), dtype=bool)
border[1:-1, 1:-1] = False
print(np.count_nonzero(border), Z[border].all())     # select only the border cells
# 16 True

A 5×5 grid has 25 cells, 9 of them interior, so the border has 16.

Common Pitfalls

  • Expecting np.pad to modify the array in place (it returns a new one).

  • Padding the channel axis of images.

  • Off-by-one errors in the interior slice; 1:-1 excludes the first and last row and column.

Interview Tips

  • Show np.pad for adding a frame and the Z[1:-1, 1:-1] = 0 exercise.

  • Mention per-axis widths and modes.

49

How do you implement a 2D convolution with NumPy?

A 2-D convolution slides a small kernel over an image and, at each position, multiplies the overlapping values element-wise and sums them; in NumPy you can do this without Python loops by building all windows with sliding_window_view(image, kernel.shape), which gives an array of shape (H − kh + 1, W − kw + 1, kh, kw), and contracting the last two axes with the kernel using np.einsum("ijkl,kl->ij", windows, kernel) (or np.tensordot). That computes cross-correlation, which is what deep-learning libraries call “convolution”; a true mathematical convolution flips the kernel in both directions first (kernel[::-1, ::-1]), which matters for asymmetric kernels such as edge detectors. The result above is the “valid” output (no padding); for “same” output size pad the image first with np.pad by kh // 2 and kw // 2, and for stride s take every s-th window (windows[::s, ::s]). Multi-channel images and multiple filters extend the einsum to “hwcij,ijcf->hwf”-style contractions. For real work, use scipy.signal.convolve2d or scipy.ndimage, FFT-based convolution for large kernels, or a deep-learning framework on a GPU; the NumPy version is mainly for understanding and for interviews.

Read full answer

Applies toNumPy 2.x

Steps

Step Code
Pad (for “same”) np.pad(img, ((ph, ph), (pw, pw)))
Extract windows sliding_window_view(img, (kh, kw))
Optional stride windows[::s, ::s]
Multiply and sum np.einsum("ijkl,kl->ij", windows, k)
True convolution Use k[::-1, ::-1]

Code Example

import numpy as np
from numpy.lib.stride_tricks import sliding_window_view

def conv2d(image, kernel, mode="valid", flip=True, stride=1):
    k = kernel[::-1, ::-1] if flip else kernel
    if mode == "same":
        ph, pw = k.shape[0] // 2, k.shape[1] // 2
        image = np.pad(image, ((ph, ph), (pw, pw)))
    windows = sliding_window_view(image, k.shape)[::stride, ::stride]
    return np.einsum("ijkl,kl->ij", windows, k)

img = np.arange(16, dtype=float).reshape(4, 4)
box = np.ones((2, 2))
print(conv2d(img, box).tolist())
# [[10.0, 14.0, 18.0], [26.0, 30.0, 34.0], [42.0, 46.0, 50.0]]

diag = np.array([[1.0, 0.0], [0.0, -1.0]])
print(conv2d(img, diag, flip=False)[0].tolist(), conv2d(img, diag, flip=True)[0].tolist())
# [-5.0, -5.0, -5.0] [5.0, 5.0, 5.0]   cross-correlation vs true convolution

blur = np.ones((3, 3)) / 9
print(conv2d(img, blur, mode="same").shape, conv2d(img, box, stride=2).shape)
# (4, 4) (2, 2)

def conv2d_loop(image, kernel):                      # reference version with loops
    kh, kw = kernel.shape
    k = kernel[::-1, ::-1]
    out = np.zeros((image.shape[0] - kh + 1, image.shape[1] - kw + 1))
    for i in range(out.shape[0]):
        for j in range(out.shape[1]):
            out[i, j] = np.sum(image[i:i + kh, j:j + kw] * k)
    return out

rng = np.random.default_rng(42)
big, kern = rng.normal(size=(20, 20)), rng.normal(size=(3, 3))
print(np.allclose(conv2d(big, kern), conv2d_loop(big, kern)))
# True

In the box example, the top-left window [[0, 1], [4, 5]] sums to 10. In the diagonal example, img[i + 1, j + 1] is always img[i, j] + 5, so the result is −5 without flipping and +5 with it.

Common Pitfalls

  • Forgetting to flip the kernel when a true convolution is required (or flipping when matching a CNN framework).

  • Getting output sizes wrong; valid size is H − kh + 1.

  • Materializing the windows with a copy, which multiplies memory by kh × kw.

Interview Tips

  • Explain sliding windows plus multiply-and-sum, and show a vectorized version.

  • Discuss padding, stride, flipping and output-size formulas.

50

How do you compute Fibonacci numbers with matrix power?

Use the identity [[1, 1], [1, 0]]ⁿ = [[F(n+1), F(n)], [F(n), F(n−1)]]: raising this matrix to the n-th power with np.linalg.matrix_power(M, n) and reading element [0, 1] gives the n-th Fibonacci number, for example F(10) = 55. The trick works because multiplying the vector (F(k+1), F(k)) by M produces (F(k+2), F(k+1)), so applying M n times advances the sequence n steps, and matrix_power uses repeated squaring, which needs only O(log n) matrix multiplications instead of n additions. Two practical limits matter in NumPy. With int64 the values overflow silently after F(92), because F(93) exceeds about 9.2 × 10¹⁸; use dtype=object so NumPy multiplies Python integers of arbitrary size (slower, but exact). And the closed-form alternative, Binet’s formula F(n) = round(φⁿ / √5) with φ = (1 + √5)/2, is fully vectorized for many n at once but is exact in float64 only up to about n = 70. For a whole list of the first n Fibonacci numbers, a simple loop or the matrix approach applied cumulatively is simplest; the matrix method shines when you need one large term, and the same idea solves any linear recurrence.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

M = np.array([[1, 1], [1, 0]], dtype=np.int64)
print(np.linalg.matrix_power(M, 10).tolist())
# [[89, 55], [55, 34]]

def fib(n):
    return int(np.linalg.matrix_power(M, n)[0, 1])

print([fib(n) for n in range(1, 11)])
# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
print(fib(92))
# 7540113804746346429

with np.errstate(over="ignore"):
    print(fib(93) < 0)                                 # int64 overflow wraps around silently
# True

M_obj = np.array([[1, 1], [1, 0]], dtype=object)       # arbitrary-precision Python ints
print(np.linalg.matrix_power(M_obj, 100)[0, 1])
# 354224848179261915075

phi = (1 + np.sqrt(5)) / 2
n = np.arange(10)
binet = np.rint(phi ** n / np.sqrt(5)).astype(np.int64)  # vectorized closed form
print(binet.tolist())
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

F(93) = 12,200,160,415,121,876,738 is larger than the int64 maximum 9,223,372,036,854,775,807, so the wrapped result is negative.

Common Pitfalls

  • Silent integer overflow beyond F(92).

  • Trusting Binet’s formula in float64 for large n.

  • Off-by-one errors between F(0) = 0 and F(1) = 1 conventions.

Interview Tips

  • Derive the matrix identity and mention O(log n) repeated squaring.

  • Discuss overflow, object dtype and Binet’s formula.

51

How do you replace array values above a threshold?

The quickest in-place way is boolean-mask assignment, a[a > threshold] = value, which overwrites every element above the threshold; to get a new array instead, use np.where(a > threshold, value, a), and when the replacement is the threshold itself (capping), use np.minimum(a, threshold) or np.clip(a, None, threshold). All of these are vectorized and run in compiled code. np.clip(a, low, high) caps both ends at once (for example clipping gradients, pixel values or outliers), np.where can use another array as the replacement (np.where(a > t, b, a), element by element), and thresholds can broadcast, so a vector of per-column thresholds caps each feature of a matrix separately. Watch the dtype: assigning 2.5 into an integer array silently truncates to 2, and values are compared after any implicit casting. NaNs never compare greater than anything, so they pass through unchanged; handle them separately with np.isnan. A common ML use is winsorizing outliers at a percentile (np.clip(x, *np.percentile(x, [1, 99]))) and binarizing predictions with (probs > 0.5).astype(int).

Read full answer

Applies toNumPy 2.x

Options

Goal Code In place?
Replace with a value a[a > t] = v Yes
Replace, keep original np.where(a > t, v, a) No
Cap at the threshold np.minimum(a, t) or np.clip(a, None, t) No (or out=a)
Cap both ends np.clip(a, lo, hi) No (or out=a)
Binarize (a > t).astype(int) No

Code Example

import numpy as np

a = np.array([1, 5, 8, 3, 10])
print(np.where(a > 5, 5, a).tolist(), np.minimum(a, 5).tolist(), np.clip(a, 2, 6).tolist())
# [1, 5, 5, 3, 5] [1, 5, 5, 3, 5] [2, 5, 6, 3, 6]

b = a.copy()
b[b > 5] = 0                              # in place
print(b.tolist())
# [1, 5, 0, 3, 0]

X = np.array([[1.0, 50.0], [7.0, 20.0], [3.0, 90.0]])
caps = np.array([5.0, 60.0])              # one threshold per column, broadcast
print(np.minimum(X, caps).tolist())
# [[1.0, 50.0], [5.0, 20.0], [3.0, 60.0]]

ints = np.array([1, 9])
ints[ints > 5] = 2.5                      # truncated to int
print(ints.tolist())
# [1, 2]

f = np.array([1.0, np.nan, 7.0])
print(np.where(f > 5, 5.0, f).tolist())   # NaN is not greater than 5
# [1.0, nan, 5.0]

probs = np.array([0.2, 0.51, 0.97])
print((probs > 0.5).astype(int).tolist())
# [0, 1, 1]

Common Pitfalls

  • Truncation when assigning floats into integer arrays.

  • Forgetting that NaNs are not replaced.

  • Modifying the original when a copy was needed (np.where doesn’t modify; mask assignment does).

Interview Tips

  • Show mask assignment, np.where and np.clip.

  • Mention per-column thresholds, dtype truncation and NaN behaviour.

52

How do you implement a rolling window calculation in NumPy?

Use numpy.lib.stride_tricks.sliding_window_view(x, window) to create a read-only view of every window, an array of shape (n − w + 1, w) for 1-D data in which row i holds x[i:i + w], and then apply any reduction along the last axis: .mean(axis=-1), .std(axis=-1), .max(axis=-1), np.median(..., axis=-1) or your own vectorized function. The view costs no extra memory because consecutive rows share data through strides, so creating it is instant; only the reduction does the n × w work. For multi-dimensional data pass axis= to roll along one axis only (for example time in a (time, features) matrix, giving shape (n − w + 1, features, w)), use slicing like [::step] for strided or non-overlapping windows, and pad the input first if you want an output of the original length. For rolling sums and means on very long series with large windows, the cumulative-sum difference trick is O(n) instead of O(n × w). For labelled time series with dates, irregular timestamps or missing values, pandas rolling is more convenient, and scipy.ndimage offers fast rolling filters such as uniform_filter1d and maximum_filter1d.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np
from numpy.lib.stride_tricks import sliding_window_view

x = np.array([1.0, 3.0, 2.0, 5.0, 4.0])
win = sliding_window_view(x, 3)
print(win.tolist())
# [[1.0, 3.0, 2.0], [3.0, 2.0, 5.0], [2.0, 5.0, 4.0]]
print(win.max(axis=-1).tolist(), win.min(axis=-1).tolist(), np.round(win.mean(axis=-1), 3).tolist())
# [3.0, 5.0, 5.0] [1.0, 2.0, 2.0] [2.0, 3.333, 3.667]
print(np.median(win, axis=-1).tolist(), np.round(win.std(axis=-1), 3).tolist())
# [2.0, 3.0, 4.0] [0.816, 1.247, 1.247]

X = np.arange(20, dtype=float).reshape(10, 2)     # 10 time steps, 2 features
wX = sliding_window_view(X, 4, axis=0)
print(wX.shape, wX.mean(axis=-1)[0].tolist())
# (7, 2, 4) [3.0, 4.0]

print(sliding_window_view(np.arange(8), 2)[::2].tolist())   # non-overlapping windows
# [[0, 1], [2, 3], [4, 5], [6, 7]]

padded = np.pad(x, (2, 0), mode="edge")           # trailing window, same length as x
print(sliding_window_view(padded, 3).mean(axis=-1).round(3).tolist())
# [1.0, 1.667, 2.0, 3.333, 3.667]

zscore = (x[2:] - win.mean(axis=-1)) / win.std(axis=-1)    # latest value vs its window
print(zscore.round(3).tolist())
# [0.0, 1.336, 0.267]

The second window [3, 2, 5] has mean 3.333 and population std 1.247, so its last value 5 has a z-score of (5 − 3.333) / 1.247 ≈ 1.336.

Common Pitfalls

  • Writing to the view (it is read-only) or copying it, which multiplies memory by w.

  • Expecting the output to have the input’s length; pad if needed.

  • Off-by-one alignment between window results and timestamps.

Interview Tips

  • Show sliding_window_view plus a reduction along the last axis.

  • Mention the axis argument, stepping, padding and the cumsum trick.

53

How do you prepare image data for a CNN with NumPy?

Images loaded from disk are typically uint8 arrays of shape (height, width, channels) with values 0–255, and preparing them for a CNN means making every image the same size, stacking them into a batch array, converting to float32, scaling to [0, 1] (divide by 255) and usually standardizing each channel with dataset means and standard deviations, then putting the axes in the order the framework expects: (N, H, W, C) channels-last for TensorFlow/Keras or (N, C, H, W) channels-first for PyTorch via np.transpose(batch, (0, 3, 1, 2)). Grayscale images need an explicit channel axis (img[..., None]), and RGBA images may need the alpha channel dropped. Labels become an integer vector or a one-hot matrix (np.eye(num_classes)[labels]). Simple augmentations can be done with array operations: horizontal flips (img[:, ::-1]), random crops by slicing, brightness changes by scaling and clipping, and noise by adding random values. Compute normalization statistics on the training set only, keep data in float32 rather than float64 to halve memory, and make arrays contiguous (np.ascontiguousarray) after transposes or flips before handing them to frameworks. Resizing itself is better done with Pillow, OpenCV or the framework’s own utilities than with NumPy.

Read full answer

Applies toNumPy 2.x

Pipeline

Step NumPy code
Stack equal-size images np.stack(images) → (N, H, W, C)
Grayscale channel axis img[..., None]
To float and [0, 1] batch.astype(np.float32) / 255.0
Standardize per channel (batch - mean) / std, mean/std shape (C,)
Channels-first np.transpose(batch, (0, 3, 1, 2))
One-hot labels np.eye(C)[labels]
Flip / crop img[:, ::-1], img[y:y+h, x:x+w]

Code Example

import numpy as np

rng = np.random.default_rng(42)
images = [rng.integers(0, 256, size=(32, 32, 3), dtype=np.uint8) for _ in range(8)]
labels = rng.integers(0, 10, size=8)

batch = np.stack(images).astype(np.float32) / 255.0          # (N, H, W, C) in [0, 1]
mean = batch.mean(axis=(0, 1, 2))                             # per-channel, from training data
std = batch.std(axis=(0, 1, 2))
batch = (batch - mean) / std
print(batch.shape, batch.dtype, np.allclose(batch.mean(axis=(0, 1, 2)), 0, atol=1e-5))
# (8, 32, 32, 3) float32 True

nchw = np.ascontiguousarray(np.transpose(batch, (0, 3, 1, 2)))   # PyTorch layout
print(nchw.shape, nchw.flags["C_CONTIGUOUS"])
# (8, 3, 32, 32) True

one_hot = np.eye(10, dtype=np.float32)[labels]
print(one_hot.shape, one_hot.sum(axis=1).tolist() == [1.0] * 8)
# (8, 10) True

gray = rng.integers(0, 256, size=(28, 28), dtype=np.uint8)
print(gray[..., None].shape)
# (28, 28, 1)

def augment(img, rng):
    if rng.random() < 0.5:
        img = img[:, ::-1]                                    # horizontal flip
    y, x = rng.integers(0, 5, size=2)
    img = img[y:y + 28, x:x + 28]                             # random 28x28 crop
    return np.clip(img * rng.uniform(0.8, 1.2), 0.0, 1.0)     # brightness jitter

aug = augment(images[0].astype(np.float32) / 255.0, rng)
print(aug.shape, float(aug.min()) >= 0.0, float(aug.max()) <= 1.0)
# (28, 28, 3) True True

Common Pitfalls

  • Feeding uint8 data or forgetting to scale, which slows or breaks training.

  • Using the wrong channel order for the framework.

  • Computing normalization statistics on test data.

  • Silent float64 conversion doubling memory (img / 255 creates float64; cast first).

Interview Tips

  • Walk through stack → float32 → scale → normalize → layout → labels.

  • Mention augmentation with slicing and train-only statistics.

54

How do you run a Monte Carlo simulation with NumPy?

A Monte Carlo simulation estimates a quantity by drawing many random samples and averaging an outcome over them, and NumPy makes it fast by generating all samples at once with a seeded generator (rng = np.random.default_rng(42)) and evaluating the outcome with vectorized operations instead of a Python loop. The classic example estimates π: draw a million points uniformly in the unit square, compute the fraction inside the quarter circle x² + y² ≤ 1 (which approaches π/4) and multiply by 4. The same pattern estimates integrals (the average of f(U) for uniform U over [a, b], times b − a), probabilities (the mean of a boolean event array), expected values and risk measures in finance (simulate many price paths, then take the mean or a percentile of the payoff), and uncertainty in models (bootstrap resampling, propagating input distributions). The error shrinks like 1/√n, so 100 times more samples give only 10 times more accuracy; always report a standard error (std / sqrt(n)) or a confidence interval with the estimate. For very large simulations, process samples in chunks to bound memory, use rng.spawn for independent parallel streams, and consider variance-reduction techniques such as antithetic variates.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

rng = np.random.default_rng(42)
n = 1_000_000

pts = rng.random((n, 2))
inside = (pts ** 2).sum(axis=1) <= 1.0
pi_hat = 4 * inside.mean()
se = 4 * inside.std() / np.sqrt(n)
print(abs(pi_hat - np.pi) < 4 * se, round(float(se), 4))
# True 0.0016

u = rng.random(n)
integral = np.mean(u ** 2)                     # integral of x^2 on [0, 1] = 1/3
print(abs(integral - 1 / 3) < 0.002)
# True

dice = rng.integers(1, 7, size=(n, 2))         # P(sum of two dice = 7) = 1/6
print(abs(np.mean(dice.sum(axis=1) == 7) - 1 / 6) < 0.002)
# True

# Price paths with geometric Brownian motion and a call option payoff
S0, mu, sigma, T, steps, paths = 100.0, 0.05, 0.2, 1.0, 252, 20_000
dt = T / steps
z = rng.standard_normal((paths, steps))
log_paths = np.cumsum((mu - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * z, axis=1)
ST = S0 * np.exp(log_paths[:, -1])
payoff = np.maximum(ST - 105.0, 0.0)
price = np.exp(-mu * T) * payoff.mean()
print(round(float(price)), np.percentile(ST, 5) < S0 < np.percentile(ST, 95))
# 8 True

With n = 1,000,000 the π estimate’s standard error is 4 × sqrt(p(1 − p) / n) ≈ 0.0016 for p = π/4. The option example uses the risk-neutral drift equal to the rate (5 percent); its Black–Scholes value is about 8.0, and with 20,000 paths the simulated price usually lands within about 0.2 of it.

Common Pitfalls

  • Looping in Python over samples instead of vectorizing.

  • Reporting an estimate without its standard error.

  • Running out of memory with huge sample arrays; use chunks.

  • Unseeded or shared generators making runs irreproducible.

Interview Tips

  • Explain sample → evaluate → average, and the 1/√n error.

  • Show the π example and one practical application.

55

How can NumPy be used for audio processing?

Digital audio is just a 1-D array of samples (or a (samples, channels) array for stereo) taken at a fixed sample rate such as 16,000 or 44,100 per second, so NumPy can generate, mix and analyse it directly: create tones with np.sin(2 * np.pi * f * t), mix by adding arrays, change volume by multiplying, measure loudness with the RMS np.sqrt(np.mean(x**2)) or in decibels, find frequencies with the FFT (np.fft.rfft and np.fft.rfftfreq), and split the signal into overlapping frames with sliding_window_view to compute a spectrogram. Typical preprocessing for speech or audio ML models uses exactly these steps: convert integer PCM (int16, range ±32767) to float32 in [−1, 1], resample to a common rate, normalize peak or RMS level, trim silence with an energy threshold, frame the signal (for example 25 ms windows every 10 ms), apply a window function (np.hanning) to reduce spectral leakage, take FFT magnitudes and then mel filter banks or MFCCs. NumPy does the math; reading and writing files is done with scipy.io.wavfile, soundfile or librosa, and libraries like librosa and torchaudio provide ready-made, NumPy-compatible features such as mel spectrograms. Watch for clipping when mixing (values beyond ±1) and convert back to int16 carefully.

Read full answer

Applies toNumPy 2.x

Common Operations

Task NumPy
Generate a tone np.sin(2*np.pi*f*t)
Mix / gain a + b, 0.5 * a
Loudness (RMS, dB) np.sqrt(np.mean(x**2)), 20*np.log10(rms)
Spectrum np.fft.rfft, np.fft.rfftfreq
Framing sliding_window_view(x, n)[::hop]
Window np.hanning(n)
PCM conversion x.astype(np.float32) / 32768

Code Example

import numpy as np
from numpy.lib.stride_tricks import sliding_window_view

sr = 8000                                     # samples per second
t = np.arange(sr) / sr                        # 1 second
tone = np.sin(2 * np.pi * 440 * t) + 0.5 * np.sin(2 * np.pi * 1000 * t)

spec = np.abs(np.fft.rfft(tone))
freqs = np.fft.rfftfreq(len(tone), d=1 / sr)
top2 = freqs[np.argsort(spec)[-2:]]
print(sorted(top2.tolist()))
# [440.0, 1000.0]

rms = np.sqrt(np.mean(np.sin(2 * np.pi * 440 * t) ** 2))
print(round(float(rms), 4), round(float(20 * np.log10(rms)), 2), "dBFS")
# 0.7071 -3.01 dBFS

peak_norm = tone / np.max(np.abs(tone))      # avoid clipping before saving
pcm = (peak_norm * 32767).astype(np.int16)
print(pcm.dtype, int(np.abs(pcm).max()))
# int16 32767

frame, hop = 200, 80                          # 25 ms frames every 10 ms
frames = sliding_window_view(tone, frame)[::hop] * np.hanning(frame)
spectrogram = np.abs(np.fft.rfft(frames, axis=1))
print(frames.shape, spectrogram.shape)
# (98, 200) (98, 101)

rng = np.random.default_rng(42)
noisy = np.concatenate([np.zeros(800), tone[:4000], np.zeros(800)]) + rng.normal(0, 0.01, 5600)
energy = (sliding_window_view(noisy, frame)[::hop] ** 2).mean(axis=1)
voiced = np.flatnonzero(energy > 0.01)
print(voiced[0] * hop / sr, (voiced[-1] * hop + frame) / sr)   # trim silence: start and end in seconds
# 0.08 0.615

With 8,000 samples at 8 kHz, the FFT bins are 1 Hz apart, so the tones land exactly on bins 440 and 1000. There are (8000 − 200) // 80 + 1 = 98 frames. The silence-trimming times depend on the threshold and on where frames overlap the edges.

Common Pitfalls

  • Mixing signals without normalizing and causing clipping when converting to int16.

  • Forgetting the window function, which smears the spectrum.

  • Mismatched sample rates between files or models.

  • Treating stereo (samples, 2) arrays as mono.

Interview Tips

  • Describe audio as sampled arrays and show generation, FFT and RMS.

  • Mention framing, windows, spectrograms and libraries like librosa.

56

How do you implement gradient descent with NumPy?

Gradient descent repeatedly moves parameters a small step against the gradient of the loss, w ← w − η ∇L(w), and with NumPy the gradient for a whole dataset is a few vectorized lines; for linear regression with mean squared error, the prediction is X @ w + b, the error is pred − y, and the gradients are dw = 2 * X.T @ err / n and db = 2 * err.mean(). A loop of a few hundred such updates with a suitable learning rate η recovers the true coefficients. The same pattern works for logistic regression (sigmoid predictions, gradient X.T @ (p − y) / n) and, with more code, for neural networks. Practical details: standardize features so one learning rate works for all of them, choose η carefully (too large diverges, too small crawls), stop when the loss or gradient norm stops changing, and use mini-batches (stochastic gradient descent) for large datasets by shuffling indices with rng.permutation each epoch and iterating over slices. Verifying the analytic gradient against a finite-difference approximation is a good habit. Extensions such as momentum, Adam and learning-rate schedules are small modifications of the update line; in production, frameworks like PyTorch or JAX compute gradients automatically.

Read full answer

Applies toNumPy 2.x

Code Example

import numpy as np

rng = np.random.default_rng(42)
n = 200
X = rng.normal(size=(n, 2))
y = X @ np.array([3.0, -2.0]) + 1.0 + rng.normal(0, 0.1, n)

w, b, lr = np.zeros(2), 0.0, 0.1
for step in range(500):
    err = X @ w + b - y
    w -= lr * 2 * X.T @ err / n
    b -= lr * 2 * err.mean()
print(np.round(w, 1).tolist(), round(b, 1), round(float(np.mean((X @ w + b - y) ** 2)), 3) < 0.02)
# [3.0, -2.0] 1.0 True

def loss(w_):
    return np.mean((X @ w_ - y) ** 2)

w0 = np.array([0.5, 0.5])                                # finite-difference gradient check
analytic = 2 * X.T @ (X @ w0 - y) / n
eps = 1e-6
numeric = np.array([(loss(w0 + eps * e) - loss(w0 - eps * e)) / (2 * eps) for e in np.eye(2)])
print(np.allclose(analytic, numeric, atol=1e-4))
# True

w_sgd, batch = np.zeros(2), 32                           # mini-batch SGD
for epoch in range(30):
    idx = rng.permutation(n)
    for start in range(0, n, batch):
        bi = idx[start:start + batch]
        err = X[bi] @ w_sgd - (y[bi] - 1.0)               # bias known here, for brevity
        w_sgd -= 0.05 * 2 * X[bi].T @ err / len(bi)
print(np.round(w_sgd, 1).tolist())
# [3.0, -2.0]

x = 0.0                                                   # 1-D: minimize (x - 3)^2
for _ in range(50):
    x -= 0.1 * 2 * (x - 3)
print(round(x, 4))
# 3.0

In the 1-D example each step multiplies the distance to 3 by 0.8, so after 50 steps x = 3 × (1 − 0.8⁵⁰) ≈ 2.99996.

Common Pitfalls

  • A learning rate that is too large (loss explodes) or too small.

  • Unscaled features making convergence slow.

  • Gradient sign or shape mistakes; check against finite differences.

  • Forgetting to shuffle in mini-batch SGD.

Interview Tips

  • Write the update rule and the vectorized MSE gradient.

  • Discuss learning rate, feature scaling, SGD and gradient checks.

57

How do you prepare data for model training with NumPy?

Preparing data means turning raw inputs into a numeric feature matrix X of shape (n_samples, n_features) and a target vector y, splitting them into training, validation and test sets with a shuffled (and seeded) permutation of indices, handling missing values, encoding categories, scaling features with statistics computed on the training split only, and finally serving the data in mini-batches. With NumPy each step is short: rng.permutation(n) gives shuffled indices for the split, X[idx] and y[idx] keep features and labels aligned, np.nanmean with np.where(np.isnan(X), ...) imputes, np.unique(cats, return_inverse=True) turns category strings into integer codes and np.eye(k)[codes] into one-hot columns, (X - mu) / sigma standardizes with training mu and sigma, and slicing a permutation in steps of the batch size yields batches. For classification, keep class proportions similar across splits (stratify by splitting each class’s indices separately) and check the class balance with np.bincount. Convert to float32 to match most frameworks, and keep the fitted statistics (means, standard deviations, category lists) so exactly the same transformation can be applied at prediction time. In real projects, scikit-learn’s train_test_split, pipelines and transformers automate this and prevent leakage, but they implement the same NumPy operations.

Read full answer

Applies toNumPy 2.x

Steps

Step NumPy
Shuffle and split idx = rng.permutation(n); X[idx[:k]]
Impute np.where(np.isnan(X), np.nanmean(X, 0), X)
Encode categories np.unique(c, return_inverse=True), np.eye(k)[codes]
Scale (train stats) (X - mu) / sigma
Check balance np.bincount(y)
Batches for i in range(0, n, bs): idx[i:i+bs]

Code Example

import numpy as np

rng = np.random.default_rng(42)
n = 100
num = rng.normal(50, 10, size=(n, 2))
num[rng.random((n, 2)) < 0.05] = np.nan                     # some missing values
city = rng.choice(["Delhi", "Pune", "Agra"], size=n)
y = rng.integers(0, 2, size=n)

idx = rng.permutation(n)
tr, te = idx[:80], idx[80:]

mu = np.nanmean(num[tr], axis=0)                            # statistics from training rows only
num_f = np.where(np.isnan(num), mu, num)
sigma = num_f[tr].std(axis=0)
num_s = (num_f - mu) / sigma

cats, codes = np.unique(city[tr], return_inverse=True)      # categories learned on training data
lookup = {c: i for i, c in enumerate(cats)}
codes_all = np.array([lookup[c] for c in city])
onehot = np.eye(len(cats))[codes_all]

X = np.hstack([num_s, onehot]).astype(np.float32)
X_train, y_train, X_test, y_test = X[tr], y[tr], X[te], y[te]
print(X_train.shape, X_test.shape, cats.tolist(), np.isnan(X).any())
# (80, 5) (20, 5) ['Agra', 'Delhi', 'Pune'] False
print(np.allclose(X_train[:, :2].mean(axis=0), 0, atol=0.05), np.bincount(y_train).sum())
# True 80

batches = [rng.permutation(80)[i:i + 32] for i in range(0, 80, 32)]
print([len(b) for b in batches])
# [32, 32, 16]

The training means are exactly 0 (up to float32 rounding), because the missing training values were filled with the training mean itself.

Common Pitfalls

  • Fitting scalers, imputers or encoders on the full data (leakage).

  • Shuffling X and y separately and misaligning them.

  • Unseen categories at prediction time; decide how to handle them.

  • Unstratified splits for imbalanced classes.

Interview Tips

  • Walk through split → impute → encode → scale → batch.

  • Emphasize training-only statistics and aligned shuffling.

58

How do you handle NaN and infinite values in NumPy?

First detect them with np.isnan(a), np.isinf(a) (or np.isposinf/np.isneginf) and np.isfinite(a), which return boolean masks; then either ignore them with the NaN-aware functions (np.nanmean, np.nansum, np.nanmax, np.nanstd, np.nanpercentile), remove them with a[np.isfinite(a)], or replace them with np.nan_to_num(a, nan=0.0, posinf=..., neginf=...) or np.where(np.isnan(a), fill, a). NaN (“not a number”) and ±inf are IEEE floating-point values produced by invalid or overflowing operations: 0/0, inf − inf and np.sqrt(-1) give NaN, while 1/0, np.log(0) (−inf) and overflow such as np.exp(1000) give infinities. NumPy emits a RuntimeWarning rather than raising, which you can change with np.errstate(divide="raise", invalid="raise") to catch the bug at its source. NaN has unusual semantics: it is not equal to anything, including itself (np.nan == np.nan is False), so always use np.isnan; comparisons with NaN are False, so it slips through threshold filters; and it propagates through arithmetic and ordinary reductions, so one NaN makes a whole sum NaN. Integer arrays cannot hold NaN at all. In ML, NaN losses usually come from log(0), division by zero or exploding values; fixes include clipping probabilities (np.clip(p, 1e-12, 1 - 1e-12)), stable formulations (log-sum-exp) and checking inputs with np.isfinite(X).all().

Read full answer

Applies toNumPy 2.x

Toolkit

Task Function
Detect np.isnan, np.isinf, np.isfinite
Ignore in statistics np.nanmean, np.nansum, np.nanmax …
Remove a[np.isfinite(a)]
Replace np.nan_to_num, np.where
Raise at the source np.errstate(invalid="raise", divide="raise")
Check a dataset np.isfinite(X).all()

Code Example

import numpy as np

with np.errstate(divide="ignore", invalid="ignore"):
    a = np.array([1.0, 0.0, -1.0, 4.0]) / np.array([0.0, 0.0, 0.0, 2.0])
print(a.tolist())
# [inf, nan, -inf, 2.0]
print(np.isnan(a).tolist(), np.isinf(a).tolist(), np.isfinite(a).tolist())
# [False, True, False, False] [True, False, True, False] [False, False, False, True]

print(np.nan == np.nan, np.isnan(np.nan))
# False True

data = np.array([1.0, np.nan, 3.0, np.inf])
print(np.sum(data), np.nansum(data), data[np.isfinite(data)].mean())
# nan inf 2.0
print(np.nan_to_num(data, nan=0.0, posinf=99.0).tolist())
# [1.0, 0.0, 3.0, 99.0]

try:
    with np.errstate(divide="raise"):
        np.log(np.array([0.0]))
except FloatingPointError as exc:
    print("caught:", exc)
# caught: divide by zero encountered in log

p = np.array([0.0, 0.5, 1.0])
y = np.array([0, 1, 1])
pc = np.clip(p, 1e-12, 1 - 1e-12)                          # avoids log(0)
bce = -np.mean(y * np.log(pc) + (1 - y) * np.log(1 - pc))
print(np.isfinite(bce), round(float(bce), 3))
# True 0.231

print(np.array([5.0, np.nan]) > 3)                        # NaN fails every comparison
# [ True False]

np.nansum ignores NaN but not infinity, so the sum is inf. The loss is −(ln 1 + ln 0.5 + ln 1) / 3 ≈ 0.231, with tiny corrections from the clipping.

Common Pitfalls

  • Testing with == np.nan.

  • One NaN silently poisoning a whole reduction or a training run.

  • Replacing NaN with 0 without thinking about what 0 means for the feature.

  • Expecting nansum to ignore infinities.

Interview Tips

  • Show detection, NaN-aware reductions and replacement.

  • Explain where NaN/inf come from and how errstate helps.

59

How do you deal with missing data in NumPy arrays?

NumPy represents missing values in float arrays as np.nan (integer arrays can’t hold NaN, so they need a sentinel value, a float conversion or a masked array), and the main strategies are: measure the problem with np.isnan(X).sum(axis=0) per column, drop rows or columns with missing values via a boolean mask such as X[~np.isnan(X).any(axis=1)], ignore them in calculations with the np.nan* functions, or impute them. Simple imputation replaces each NaN with its column’s mean or median, done in one line with np.where(np.isnan(X), np.nanmean(X, axis=0), X), because the (n_features,) vector of column means broadcasts against the matrix. Other options are a constant, forward-fill for time series (vectorized with np.maximum.accumulate over the indices of valid values), linear interpolation with np.interp, or masked arrays (np.ma.masked_invalid) that carry the mask through computations. For ML, compute imputation statistics on the training set only, consider adding a binary “was missing” indicator feature because missingness itself can be informative, and remember that dropping rows can bias the data if values aren’t missing at random. For richer tools (KNN or iterative imputation, mixed types), use pandas or scikit-learn’s SimpleImputer, KNNImputer and IterativeImputer.

Read full answer

Applies toNumPy 2.x

Strategies

Strategy NumPy
Count np.isnan(X).sum(axis=0)
Drop rows X[~np.isnan(X).any(axis=1)]
Ignore np.nanmean, np.nanstd, …
Mean/median impute np.where(np.isnan(X), np.nanmean(X, 0), X)
Forward fill indices + np.maximum.accumulate
Interpolate np.interp(missing_x, valid_x, valid_y)
Indicator np.isnan(X).astype(int)

Code Example

import numpy as np

X = np.array([[1.0, np.nan, 3.0],
              [4.0, 5.0, np.nan],
              [7.0, 8.0, 9.0]])
print(np.isnan(X).sum(axis=0).tolist())
# [0, 1, 1]
print(X[~np.isnan(X).any(axis=1)].tolist())               # drop incomplete rows
# [[7.0, 8.0, 9.0]]

col_mean = np.nanmean(X, axis=0)
print(col_mean.tolist())
# [4.0, 6.5, 6.0]
print(np.where(np.isnan(X), col_mean, X).tolist())
# [[1.0, 6.5, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
print(np.where(np.isnan(X), np.nanmedian(X, axis=0), X)[0, 1])
# 6.5

missing_flag = np.isnan(X).astype(int)                    # keep the information that a value was missing
print(np.hstack([np.where(np.isnan(X), col_mean, X), missing_flag]).shape)
# (3, 6)

s = np.array([1.0, np.nan, np.nan, 4.0, np.nan])
idx = np.where(~np.isnan(s), np.arange(len(s)), 0)
print(s[np.maximum.accumulate(idx)].tolist())             # forward fill
# [1.0, 1.0, 1.0, 4.0, 4.0]

valid = ~np.isnan(s)
interp = s.copy()
interp[~valid] = np.interp(np.flatnonzero(~valid), np.flatnonzero(valid), s[valid])
print(interp.tolist())                                    # linear interpolation, edge value held at the end
# [1.0, 2.0, 3.0, 4.0, 4.0]

Common Pitfalls

  • Imputing with statistics from the full dataset, including test data.

  • Comparing with == np.nan instead of np.isnan.

  • Dropping too many rows and biasing the data.

  • Forward-fill when the first value is missing (it stays NaN in this method).

Interview Tips

  • List drop, ignore and impute, with the one-line mean imputation.

  • Mention indicators, train-only statistics and when to use pandas or scikit-learn.

60

How do you count unique values in a NumPy array?

Use np.unique(a, return_counts=True), which returns the sorted unique values and, in a second array, how many times each one appears; for example [3, 1, 2, 3, 3, 1] gives values [1, 2, 3] and counts [2, 1, 3]. The number of distinct values is len(np.unique(a)), and the most frequent value is values[np.argmax(counts)]. np.unique can also return return_index (the first position of each value) and return_inverse (for each element, the index of its value in the unique array, which is a handy way to encode categories as integers). With axis=0 it finds unique rows of a 2-D array, for example duplicate records. NumPy 2.0 added array-API functions with named results: np.unique_values, np.unique_counts (fields values and counts), np.unique_inverse and np.unique_all. For small non-negative integers such as class labels, np.bincount(a) is faster and returns a count for every integer from 0 to the maximum, including zeros for missing classes (use minlength to fix the length). np.unique sorts, so it is O(n log n); NaN values are collapsed into a single NaN by default since NumPy 1.21 (equal_nan=True).

Read full answer

Applies toNumPy 2.x

Functions

Goal Code
Values and counts np.unique(a, return_counts=True)
Named results (2.0+) np.unique_counts(a)
Number of distinct values len(np.unique(a))
Most common value vals[np.argmax(counts)]
Integer codes np.unique(a, return_inverse=True)[1]
Unique rows np.unique(X, axis=0)
Counts of labels 0..k np.bincount(a, minlength=k)

Code Example

import numpy as np

a = np.array([3, 1, 2, 3, 3, 1])
vals, counts = np.unique(a, return_counts=True)
print(vals.tolist(), counts.tolist(), len(vals), vals[np.argmax(counts)])
# [1, 2, 3] [2, 1, 3] 3 3
print(dict(zip(vals.tolist(), counts.tolist())))
# {1: 2, 2: 1, 3: 3}

res = np.unique_counts(a)                                 # NumPy 2.0+
print(res.values.tolist(), res.counts.tolist())
# [1, 2, 3] [2, 1, 3]

labels = np.array([0, 2, 2, 1, 2, 0])
print(np.bincount(labels).tolist(), np.bincount(labels, minlength=5).tolist())
# [2, 1, 3] [2, 1, 3, 0, 0]

colors = np.array(["red", "blue", "red", "green"])
names, codes = np.unique(colors, return_inverse=True)
print(names.tolist(), codes.tolist())
# ['blue', 'green', 'red'] [2, 0, 2, 1]

X = np.array([[1, 2], [3, 4], [1, 2]])
rows, row_counts = np.unique(X, axis=0, return_counts=True)
print(rows.tolist(), row_counts.tolist())
# [[1, 2], [3, 4]] [2, 1]

print(np.unique(np.array([1.0, np.nan, np.nan])).tolist())
# [1.0, nan]

Common Pitfalls

  • Forgetting that np.unique sorts (it doesn’t keep first-appearance order; use return_index and sort by it).

  • np.bincount on negative or non-integer values (it raises an error).

  • Large integer ranges with bincount creating huge arrays.

Interview Tips

  • Answer with return_counts=True and show the most common value.

  • Mention bincount, return_inverse, unique rows and NumPy 2’s unique_counts.

61

What issues arise when importing large datasets into NumPy?

The main problems are memory, speed and data fidelity: a NumPy array must fit in RAM as one contiguous block of a single dtype, text parsers like np.loadtxt and especially np.genfromtxt are slow and use several times the final array size while parsing, default dtypes (float64, int64, or wide Unicode strings) can multiply memory needs, and real files contain missing values, mixed types, bad rows and encoding problems that NumPy’s simple loaders handle poorly. A CSV with 100 million rows and 10 numeric columns is 8 GB as float64 before counting temporary copies, so loading it naively can trigger a MemoryError or heavy swapping. Mitigations: choose compact dtypes up front (dtype=np.float32, small integer types), load only the needed columns (usecols) and rows (max_rows, skiprows), read in chunks and process or aggregate each chunk, convert text once into a binary format (.npy, Parquet, HDF5) and later memory-map it with np.load(..., mmap_mode="r") so only accessed pages are read, and use pandas read_csv (much faster C or pyarrow engine, chunksize, NA handling) followed by .to_numpy(). For data that never fits in memory, use Dask, Zarr or database queries. Also check the result: dtype, shape, NaN counts and value ranges, since silent parsing problems (a header line read as NaN, commas inside quotes, locale decimal separators) are common.

Read full answer

Applies toNumPy 2.x

Issues and Fixes

Issue Fix
Doesn’t fit in RAM Smaller dtypes, chunks, memory mapping, Dask
Slow text parsing pandas read_csv, convert once to .npy/Parquet
Temporary copies while parsing Binary formats, chunked reading
Mixed types / strings Structured dtypes, pandas, separate columns
Missing values genfromtxt(filling_values=...), pandas NA handling
Silent parsing errors Validate shape, dtype, NaNs and ranges
Object dtype from ragged data Fix input; never load pickles from untrusted files

Code Example

import tempfile
from pathlib import Path

import numpy as np

rng = np.random.default_rng(42)
data = rng.normal(size=(10_000, 5))

with tempfile.TemporaryDirectory() as tmp:
    csv = Path(tmp) / "data.csv"
    np.savetxt(csv, data, delimiter=",", fmt="%.6f", header="a,b,c,d,e", comments="")

    two_cols = np.loadtxt(csv, delimiter=",", skiprows=1, usecols=(0, 2), dtype=np.float32)
    print(two_cols.shape, two_cols.dtype, two_cols.nbytes)
    # (10000, 2) float32 80000

    total, count = np.zeros(5), 0
    with open(csv) as f:
        next(f)                                                # skip header
        while True:
            chunk = np.loadtxt(f, delimiter=",", max_rows=2_000, ndmin=2)
            if chunk.size == 0:
                break
            total += chunk.sum(axis=0)
            count += len(chunk)
    print(count, np.allclose(total / count, data.mean(axis=0), atol=1e-6))
    # 10000 True

    npy = Path(tmp) / "data.npy"
    np.save(npy, data.astype(np.float32))                      # convert once to binary
    mm = np.load(npy, mmap_mode="r")                           # pages read only when accessed
    print(type(mm).__name__, mm[:3].shape, bool(np.isclose(mm[:, 0].mean(), data[:, 0].mean(), atol=1e-5)))
    # memmap (3, 5) True

print(f"{100_000_000 * 10 * 8 / 1e9:.0f} GB as float64, {100_000_000 * 10 * 4 / 1e9:.0f} GB as float32")
# 8 GB as float64, 4 GB as float32

Calling np.loadtxt repeatedly on the same open file with max_rows reads the file chunk by chunk; recent NumPy versions return an empty array (possibly with a warning) at the end.

Common Pitfalls

  • Loading everything with genfromtxt and default float64.

  • Ignoring header rows, quoted fields or locale decimal commas.

  • Converting large mixed-type tables to object arrays.

  • Not validating NaN counts and shapes after loading.

Interview Tips

  • Cover memory, speed, dtypes and data quality problems.

  • Recommend binary formats, memory mapping, chunking and pandas for parsing.

62

How do you resolve a MemoryError with very large NumPy arrays?

A MemoryError (in NumPy 2.x the message reads “Unable to allocate X GiB for an array with shape … and data type …”) means NumPy could not get one contiguous block of the requested size, so the fix is to need less memory at once: compute the required size first (np.prod(shape) * np.dtype(dtype).itemsize), use smaller dtypes, avoid hidden temporary arrays, process the data in chunks, keep it on disk with memory mapping, or switch to sparse or out-of-core tools. Common causes are accidental broadcasting (a (n,) array combined with an (n, 1) array creates n × n elements), all-pairs computations such as distance matrices, default float64 when float32 suffices, long expressions that allocate a temporary for each step, fancy indexing and astype making full copies, and keeping references to big intermediates. Concrete remedies: use dtype=np.float32 (half the memory) or small integer types; rewrite expressions with in-place operations and out=; compute reductions over chunks and combine the partial results; load data with np.load(..., mmap_mode="r") or np.memmap; use scipy.sparse when most values are zero; free large arrays with del once they are no longer needed; and for genuinely larger-than-memory work, use Dask, Zarr, or a GPU or cluster with more memory. A 64-bit Python build is required for arrays over about 2 GB, and on Linux the operating system’s overcommit settings can make allocation fail earlier or later than expected.

Read full answer

Applies toNumPy 2.x

Remedies

Cause Remedy
float64 everywhere float32 / small ints
Temporaries in expressions In-place ops, out=
Whole dataset at once Chunked processing
Data larger than RAM mmap_mode, np.memmap, Dask, Zarr
Mostly zeros scipy.sparse
All-pairs computations Blocks of rows, approximate methods
Accidental broadcasting Check shapes before operating

Code Example

import numpy as np

def required_gib(shape, dtype=np.float64):
    return np.prod(shape, dtype=np.float64) * np.dtype(dtype).itemsize / 2**30

print(round(required_gib((100_000, 100_000)), 1), round(required_gib((100_000, 100_000), np.float32), 1))
# 74.5 37.2

try:
    np.zeros((10**7, 10**7))                               # 800 TB: fails on any normal machine
except MemoryError as exc:
    print("MemoryError:", "Unable to allocate" in str(exc))
# MemoryError: True

y = np.ones(5)
pred = np.ones((5, 1))
print((y - pred).shape, (y - pred.ravel()).shape)         # accidental (n, n) vs intended (n,)
# (5, 5) (5,)

rng = np.random.default_rng(42)
X = rng.normal(size=(20_000, 8)).astype(np.float32)

def chunked_mean_std(X, chunk=4096):                       # one pass, bounded memory
    n, s, ss = 0, np.zeros(X.shape[1]), np.zeros(X.shape[1])
    for start in range(0, len(X), chunk):
        c = X[start:start + chunk].astype(np.float64)
        n += len(c)
        s += c.sum(axis=0)
        ss += (c ** 2).sum(axis=0)
    mean = s / n
    return mean, np.sqrt(ss / n - mean ** 2)

m, sd = chunked_mean_std(X)
print(np.allclose(m, X.mean(axis=0), atol=1e-5), np.allclose(sd, X.std(axis=0), atol=1e-4))
# True True

def nearest_in_blocks(A, B, block=1000):                   # avoids an n x m distance matrix
    out = np.empty(len(A), dtype=np.int64)
    for i in range(0, len(A), block):
        d = ((A[i:i + block, None, :] - B[None, :, :]) ** 2).sum(axis=2)
        out[i:i + block] = d.argmin(axis=1)
    return out

print(nearest_in_blocks(X[:3000], X[:500])[:3].tolist())  # the first 3 points are in B, so they match themselves
# [0, 1, 2]

The sum-of-squares formula for the variance can lose precision when the mean is large compared with the spread; Welford’s algorithm or chunk-wise variance merging is more robust.

Common Pitfalls

  • Catching the error instead of reducing memory needs.

  • Hidden copies from astype, fancy indexing and np.concatenate in loops.

  • Assuming del frees memory while views of the array still exist.

  • Allocating huge arrays with np.empty and relying on lazy allocation behaviour of the OS.

Interview Tips

  • Start by estimating size, then list dtype, chunking, memory mapping, sparse and Dask.

  • Mention accidental broadcasting and temporary arrays as common culprits.

63

What are NumPy polynomial objects and how are they used?

NumPy’s polynomial objects, in the numpy.polynomial package, are classes such as Polynomial, Chebyshev, Legendre, Laguerre, Hermite and HermiteE that represent a polynomial by its coefficients in a given basis and support evaluation by calling the object (p(x)), arithmetic (+, -, *, //, %, **), calculus (deriv, integ), root finding (roots), least-squares fitting (Polynomial.fit) and conversion between bases (convert, cast). Coefficients are stored in increasing order of degree, so Polynomial([1, 2, 3]) means 1 + 2x + 3x², the opposite of the legacy np.poly1d and np.polyfit/np.polyval functions, which use decreasing order; NumPy recommends the numpy.polynomial package for new code. A distinctive feature is the domain and window: Polynomial.fit maps the data’s x-range to [−1, 1] internally, which makes high-degree fits much better conditioned, so the stored coef are for the scaled variable and you call .convert() to get coefficients in the original x. Uses include curve fitting and trend lines, smooth interpolation, feature engineering, numerical analysis (Chebyshev approximations of functions, Gaussian quadrature points with legendre.leggauss) and signal processing.

Read full answer

Applies toNumPy 2.x

Main Features

Feature Example
Create P([1, 2, 3]) = 1 + 2x + 3x²
Evaluate p(2), p(np.array([...]))
Arithmetic p + q, p * q, divmod(p, q), p ** 2
Calculus p.deriv(), p.integ()
Roots p.roots()
Fit P.fit(x, y, deg), then .convert()
Other bases Chebyshev, Legendre, Hermite
From roots P.fromroots([2, -3])

Code Example

import numpy as np
from numpy.polynomial import Chebyshev, Polynomial as P

p = P([1, 2, 3])                                   # 1 + 2x + 3x^2
print(p(2), p(np.array([0, 1])).tolist())
# 17.0 [1.0, 6.0]
print(p.deriv().coef.tolist(), p.integ().coef.tolist())
# [2.0, 6.0] [0.0, 1.0, 1.0, 1.0]

q = P([-6, 1, 1])                                  # x^2 + x - 6 = (x + 3)(x - 2)
print(np.round(np.sort(q.roots()), 6).tolist(), P.fromroots([2, -3]).coef.tolist())
# [-3.0, 2.0] [-6.0, 1.0, 1.0]

x = np.arange(10.0)
y = 2 * x**2 - 3 * x + 1
fit = P.fit(x, y, deg=2)
print(np.round(fit.convert().coef, 6).tolist(), fit.domain.tolist())
# [1.0, -3.0, 2.0] [0.0, 9.0]

legacy = np.poly1d([3, 2, 1])                      # decreasing order: 3x^2 + 2x + 1
print(legacy(2), np.polyval([3, 2, 1], 2))
# 17 17

c = Chebyshev.fit(np.linspace(-1, 1, 50), np.cos(np.linspace(-1, 1, 50)), deg=6)
print(abs(c(0.3) - np.cos(0.3)) < 1e-6)
# True

The legacy poly1d returns 17 for p(2) = 3·4 + 2·2 + 1 with integer coefficients, while Polynomial stores float coefficients.

Common Pitfalls

  • Mixing coefficient orders between numpy.polynomial (increasing) and poly1d/polyfit (decreasing).

  • Reading fit.coef directly instead of fit.convert().coef.

  • Overfitting with high-degree polynomials.

  • Using roots of high-degree polynomials, which are numerically sensitive.

Interview Tips

  • Describe the classes, increasing coefficient order and the operations.

  • Explain fit with domain scaling and convert(), and the legacy API difference.

64

How does NumPy's C-API contribute to its performance?

NumPy is a C library with a Python interface: the ndarray object, its memory buffer, dtype machinery, ufunc loops, sorting, indexing and linear-algebra wrappers are written in C (with some C++ for SIMD-optimized kernels), and the C-API is the set of functions and macros (PyArray_SimpleNew, PyArray_DATA, PyArray_DIMS, PyArray_STRIDES, PyUFunc_FromFuncAndData and many more) through which NumPy’s own code and third-party C extensions create and manipulate arrays directly in memory. Performance comes from the fact that once a Python call such as a + b reaches C, the inner loop runs over raw typed memory with no Python objects, reference counting or dynamic dispatch per element; NumPy selects a type-specific loop once (for example float64 + float64), uses CPU-specific SIMD versions chosen at runtime (SSE, AVX2, AVX-512, NEON), and releases the global interpreter lock in many operations so other threads can run. The same C-API lets SciPy, scikit-learn, pandas, OpenCV, Cython and many other packages operate on NumPy arrays without copying, which is why the whole scientific Python stack can share data efficiently; the buffer protocol and array_interface / DLPack offer zero-copy exchange with other libraries. For extension authors, NumPy 2.0 changed the ABI: packages compiled against NumPy 1.x must be rebuilt, while packages compiled against 2.x also run on 1.x (back to the configured minimum), and the API removed or hid some old internals.

Read full answer

Applies toNumPy 2.x

How the C Layer Helps

Mechanism Effect
Typed contiguous buffers No per-element Python objects
Type-specific ufunc inner loops Dispatch once per call, not per element
Runtime SIMD dispatch Uses the best CPU instructions available
BLAS/LAPACK bindings Fast, multithreaded linear algebra
GIL released in loops Threads can run concurrently
Public C-API, buffer protocol Zero-copy sharing with other libraries

Code Example

A minimal C extension function that sums a float64 array through the C-API (simplified; module setup omitted):

#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#include <Python.h>
#include <numpy/arrayobject.h>

static PyObject *fast_sum(PyObject *self, PyObject *args) {
    PyObject *obj;
    if (!PyArg_ParseTuple(args, "O", &obj)) return NULL;
    PyArrayObject *arr = (PyArrayObject *)PyArray_FROMANY(
        obj, NPY_DOUBLE, 1, 1, NPY_ARRAY_IN_ARRAY);   /* contiguous float64, 1-D */
    if (arr == NULL) return NULL;
    const double *data = (const double *)PyArray_DATA(arr);
    npy_intp n = PyArray_SIZE(arr);
    double total = 0.0;
    Py_BEGIN_ALLOW_THREADS                            /* release the GIL for the loop */
    for (npy_intp i = 0; i < n; i++) total += data[i];
    Py_END_ALLOW_THREADS
    Py_DECREF(arr);
    return PyFloat_FromDouble(total);
}
/* In the module init function: import_array(); */

From Python you can see the same raw pointer and layout information that C code uses:

import numpy as np

a = np.arange(6, dtype=np.float64)
info = a.__array_interface__
print(info["typestr"], info["shape"], info["strides"], isinstance(info["data"][0], int))
# <f8 (6,) None True
print(a.ctypes.data == info["data"][0], a.flags["C_CONTIGUOUS"])
# True True
print(int(np.__version__.split(".")[0]) >= 2)
# True (on NumPy 2.x)

strides is None in the array interface when the array is C-contiguous.

Common Pitfalls

  • Assuming C extensions are automatically faster; per-element calls back into Python remove the benefit.

  • Ignoring non-contiguous input in C code (use PyArray_FROMANY flags or handle strides).

  • Shipping extensions built against NumPy 1.x to NumPy 2.x users without rebuilding.

Interview Tips

  • Explain typed buffers, compiled inner loops, SIMD dispatch, BLAS and GIL release.

  • Mention the ecosystem benefit and the NumPy 2 ABI change.

65

What is a stride trick in NumPy?

A stride trick creates a new view of an existing array by choosing a shape and strides that reinterpret the same memory in a different way, so data appears repeated, overlapping or rearranged without being copied; NumPy provides them in numpy.lib.stride_tricks as sliding_window_view (safe overlapping windows), as_strided (any shape and strides you specify) and broadcast_to (repetition with a zero stride). The key idea is that an element’s address is base + Σ index × stride, so making the stride of a new “window” axis equal to the stride of the original axis makes consecutive windows overlap by all but one element, and a stride of 0 makes every index along an axis point to the same data. This turns rolling statistics, 1-D and 2-D convolutions (the im2col technique), Hankel and Toeplitz matrices, n-gram extraction from token arrays and block or patch views of images into single vectorized operations. The tricks are cheap in memory only while you keep the view; operations that copy it (or reductions over it) still touch every virtual element. as_strided performs no bounds checks, so mistakes read arbitrary memory and can crash the interpreter, and writing into overlapping views gives confusing results; prefer sliding_window_view, pass writeable=False to as_strided, and test against a loop version.

Read full answer

Applies toNumPy 2.x

Common Stride Tricks

Trick Tool Example use
Sliding windows sliding_window_view(x, w) Rolling mean, n-grams
2-D patches sliding_window_view(img, (k, k)) Convolution (im2col)
Repeat without copying broadcast_to, zero stride Tiling a row
Hankel / Toeplitz Windows over a 1-D sequence Signal processing, AR models
Arbitrary view as_strided(x, shape, strides) Advanced, use with care

Code Example

import numpy as np
from numpy.lib.stride_tricks import as_strided, sliding_window_view

x = np.arange(1, 6)                                   # [1, 2, 3, 4, 5]
hankel = sliding_window_view(x, 3)
print(hankel.tolist(), hankel.strides)
# [[1, 2, 3], [2, 3, 4], [3, 4, 5]] (8, 8)

c, r = np.array([1, 2, 3]), np.array([1, 4, 5])       # Toeplitz: first column c, first row r
vals = np.concatenate([c[::-1], r[1:]])               # [3, 2, 1, 4, 5]
toeplitz = sliding_window_view(vals, 3)[::-1]
print(toeplitz.tolist())
# [[1, 4, 5], [2, 1, 4], [3, 2, 1]]

rep = as_strided(x, shape=(3, 5), strides=(0, x.strides[0]), writeable=False)
print(rep.tolist())
# [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]

tokens = np.array([7, 3, 9, 3, 9, 1])
bigrams = sliding_window_view(tokens, 2)
pairs, counts = np.unique(bigrams, axis=0, return_counts=True)
print(pairs[counts.argmax()].tolist(), counts.max())  # most frequent bigram
# [3, 9] 2

img = np.arange(16).reshape(4, 4)
blocks = img.reshape(2, 2, 2, 2).swapaxes(1, 2)       # non-overlapping 2x2 blocks via strides
print(blocks[1, 0].tolist(), blocks.reshape(2, 2, 4).max(axis=2).tolist())
# [[8, 9], [12, 13]] [[5, 7], [13, 15]]
print(np.shares_memory(img, blocks))
# True

Common Pitfalls

  • Wrong strides in as_strided, which read out-of-bounds memory.

  • Writing to overlapping or zero-stride views.

  • Copying a window view and multiplying memory by the window size.

Interview Tips

  • Explain the address formula and how overlapping and zero strides work.

  • Give examples (rolling windows, im2col, Toeplitz) and the safety advice.

66

How do you perform polynomial operations with NumPy?

NumPy offers two APIs: the recommended numpy.polynomial package (the Polynomial class and its siblings, with coefficients in increasing order of degree) and the legacy functions np.polyval, np.polyfit, np.roots, np.polyadd, np.polymul, np.polyder, np.polyint and the np.poly1d class (coefficients in decreasing order). With either, you can evaluate a polynomial at many points at once, add, subtract and multiply polynomials (multiplication is convolution of the coefficient arrays), divide with quotient and remainder, differentiate and integrate, find roots (computed as eigenvalues of the companion matrix) and build a polynomial from its roots, and fit polynomials to data by least squares. With the class API these are natural operators: p + q, p * q, divmod(p, q), p ** 3, p.deriv(m), p.integ(lbnd=...), p.roots() and P.fit(x, y, deg). Typical uses include polynomial regression and trend fitting, smoothing, interpolation, characteristic polynomials in linear algebra and filter design in signal processing. For numerical stability, prefer Polynomial.fit (which scales x to [−1, 1]) over np.polyfit for higher degrees, keep degrees low, and check fits on held-out points.

Read full answer

Applies toNumPy 2.x

Operation Reference

Operation numpy.polynomial Legacy
Evaluate p(x) np.polyval(c, x)
Add / multiply p + q, p * q np.polyadd, np.polymul
Divide divmod(p, q) np.polydiv
Derivative / integral p.deriv(), p.integ() np.polyder, np.polyint
Roots p.roots() np.roots(c)
From roots P.fromroots(r) np.poly(r)
Fit P.fit(x, y, d).convert() np.polyfit(x, y, d)

Code Example

import numpy as np
from numpy.polynomial import Polynomial as P

p = P([1, 1])                                        # 1 + x
q = P([1, -1])                                       # 1 - x
print((p + q).coef.tolist(), (p * q).coef.tolist(), (p ** 2).coef.tolist())
# [2.0] [1.0, 0.0, -1.0] [1.0, 2.0, 1.0]

quot, rem = divmod(P([-1, 0, 1]), P([-1, 1]))        # (x^2 - 1) / (x - 1)
print(quot.coef.tolist(), rem.coef.tolist())
# [1.0, 1.0] [0.0]

f = P([0, 0, 3])                                     # 3x^2
print(f.integ(lbnd=0)(2) - f.integ(lbnd=0)(0))       # definite integral from 0 to 2 = 8
# 8.0

print(np.polymul([1, 1], [1, -1]).tolist(), np.polyder([3, 0, 0]).tolist(), np.round(np.roots([1, -3, 2]), 6).tolist())
# [1, 0, -1] [6, 0] [2.0, 1.0]

rng = np.random.default_rng(42)
x = np.linspace(0, 5, 40)
y = 0.5 * x**3 - 2 * x + 1 + rng.normal(0, 0.1, x.size)
coef_new = P.fit(x, y, 3).convert().coef
coef_old = np.polyfit(x, y, 3)
print(np.round(coef_new, 1).tolist(), np.allclose(coef_new, coef_old[::-1]))
# [1.0, -2.0, 0.0, 0.5] True

grid = np.array([0.0, 1.0, 2.0])
print(P(coef_new)(grid).round(1).tolist())           # evaluate on many points at once
# [1.0, -0.5, 1.0]

With noise of 0.1, the fitted coefficients round back to the true values 1, −2, 0 and 0.5 (the x² term may round to −0.0); evaluating the true polynomial gives 1, −0.5 and 1 at x = 0, 1, 2.

Common Pitfalls

  • Mixing increasing (numpy.polynomial) and decreasing (legacy) coefficient order.

  • Using np.polyfit for high degrees (ill-conditioned).

  • Forgetting the integration constant (integ takes k and lbnd).

  • Expecting exact roots for high-degree or clustered-root polynomials.

Interview Tips

  • Show arithmetic, calculus, roots and fitting with the class API.

  • Point out the coefficient-order difference with the legacy API.

67

What is the role of the NumPy nditer object?

np.nditer is NumPy’s general multi-dimensional iterator: it visits every element of one or more arrays, applying broadcasting between them, choosing an efficient memory order, handling type casting and buffering, and optionally allocating and writing output arrays, which is the same machinery NumPy’s ufuncs use internally in C. In Python you use it when you need element-level access that isn’t easily vectorized: by default it iterates in memory order (order="K"), so a transposed array is visited in storage order rather than logical order; op_flags=["readwrite"] lets you modify elements in place via x[...] = value inside a with block; flags=["multi_index"] exposes the current n-dimensional index; flags=["external_loop"] yields 1-D chunks instead of scalars, cutting Python overhead; and passing several arrays iterates over them in broadcast lockstep, with None operands allocated as outputs. In pure Python, nditer is still a Python-level loop and therefore slow, so it is mostly a teaching tool, a way to prototype logic that will be moved to Cython (where nditer-style iteration becomes fast) or a convenience for code that must handle arbitrary dimensions and broadcasting. For most tasks vectorized expressions, np.ndindex, np.ndenumerate or np.broadcast are simpler.

Read full answer

Applies toNumPy 2.x

Options

Option Effect
order="K" (default), "C", "F" Memory, row-major or column-major order
op_flags=["readwrite"] Modify elements in place
flags=["multi_index"] Access it.multi_index
flags=["external_loop"] Yield 1-D chunks
Several operands Broadcast together
None operand + ["writeonly", "allocate"] Allocate an output
flags=["buffered"], op_dtypes=[...] Iterate with casting

Code Example

import numpy as np

a = np.arange(6).reshape(2, 3)
print([int(x) for x in np.nditer(a)], [int(x) for x in np.nditer(a.T)])   # memory order for both
# [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5]
print([int(x) for x in np.nditer(a, order="F")])
# [0, 3, 1, 4, 2, 5]

with np.nditer(a, op_flags=["readwrite"]) as it:
    for x in it:
        x[...] = x * 10
print(a.tolist())
# [[0, 10, 20], [30, 40, 50]]

it = np.nditer(a, flags=["multi_index"])
print([(it.multi_index, int(v)) for v in it][:3])
# [((0, 0), 0), ((0, 1), 10), ((0, 2), 20)]

col = np.array([[1], [2]])
row = np.array([100, 200, 300])
print([(int(x), int(y)) for x, y in np.nditer([col, row])][:4])   # broadcast together
# [(1, 100), (1, 200), (1, 300), (2, 100)]

def add_with_output(x, y):
    it = np.nditer([x, y, None], op_flags=[["readonly"], ["readonly"], ["writeonly", "allocate"]])
    with it:
        for xi, yi, out in it:
            out[...] = xi + yi
        return it.operands[2]

print(add_with_output(col, row).tolist())
# [[101, 201, 301], [102, 202, 302]]

print([chunk.shape for chunk in np.nditer(np.ones((3, 4)), flags=["external_loop"])])
# [(12,)]

With external_loop, a contiguous array is delivered as one 1-D chunk of all 12 elements.

Common Pitfalls

  • Expecting nditer loops to be fast in pure Python.

  • Forgetting x[...] = (plain x = ... only rebinds the name) or the with block for writes.

  • Being surprised by memory-order iteration on transposed arrays.

Interview Tips

  • Describe broadcasting, order, read-write and multi-index features.

  • Say it underlies ufuncs and is most useful with Cython or for generic code.

68

How does NumPy integrate with pandas and Matplotlib?

NumPy is the shared foundation: pandas stores most columns of a DataFrame or Series as NumPy arrays (or NumPy-compatible extension arrays) and converts both ways cheaply, and Matplotlib accepts NumPy arrays (or anything convertible to them) as the input to every plot, converting data internally with np.asarray. With pandas, df.to_numpy() (or series.to_numpy()) returns the values as an array, possibly without copying when all columns share one dtype, and pd.DataFrame(arr, columns=...) wraps an array in a labelled table; NumPy ufuncs applied to a Series or DataFrame (np.log(df["price"]), np.sqrt(df)) return pandas objects with the index preserved, and functions like np.where work on Series values. Watch dtypes when converting: mixed column types produce an object array, nullable pandas types (Int64, boolean) need to_numpy(dtype=float, na_value=np.nan), and pandas 2.x with the copy-on-write mode may return read-only views. With Matplotlib, you compute data with NumPy (x = np.linspace(...), y = np.sin(x)), pass arrays to ax.plot, ax.scatter, ax.hist or ax.imshow (a 2-D or (H, W, 3) array becomes an image), and use NumPy results such as np.histogram with ax.stairs. Scikit-learn, SciPy, seaborn, OpenCV and deep-learning frameworks use the same arrays, so NumPy is the common data format across the stack.

Read full answer

Applies toNumPy 2.x, pandas 2.x, Matplotlib 3.x

Integration Points

Library NumPy ↔ library
pandas df.to_numpy(), pd.DataFrame(arr), ufuncs on Series keep the index
Matplotlib Arrays in plot, scatter, hist, imshow; np.histogram + stairs
scikit-learn Fits on arrays, returns arrays
PyTorch / JAX torch.from_numpy, jnp.asarray, np.asarray(tensor)

Code Example

import io

import numpy as np
import pandas as pd
from matplotlib.figure import Figure

df = pd.DataFrame({"price": [100.0, 110.0, 121.0], "qty": [1, 2, 3]}, index=["a", "b", "c"])
arr = df.to_numpy()
print(arr.dtype, arr.shape)
# float64 (3, 2)

log_price = np.log(df["price"])                     # ufunc on a Series keeps the index
print(type(log_price).__name__, log_price.index.tolist())
# Series ['a', 'b', 'c']
df["big"] = np.where(df["price"] > 105, "yes", "no")
print(df["big"].tolist(), df.to_numpy().dtype)      # mixed types make an object array
# ['no', 'yes', 'yes'] object

nullable = pd.Series([1, None, 3], dtype="Int64")
print(nullable.to_numpy(dtype=float, na_value=np.nan).tolist())
# [1.0, nan, 3.0]

back = pd.DataFrame(np.arange(6).reshape(3, 2), columns=["x", "y"])
print(back.sum().tolist())
# [6, 9]

x = np.linspace(0, 2 * np.pi, 100)
fig = Figure()                                      # no GUI backend needed
ax = fig.subplots()
ax.plot(x, np.sin(x), label="sin")
counts, edges = np.histogram(np.random.default_rng(42).normal(size=500), bins=20)
ax.stairs(counts / counts.max(), edges, label="hist (scaled)")
ax.legend()
buf = io.BytesIO()
fig.savefig(buf, format="png")
print(buf.getbuffer().nbytes > 0)
# True

Common Pitfalls

  • Getting object arrays from mixed-type DataFrames.

  • Losing the index when converting to NumPy and then misaligning results.

  • Nullable pandas dtypes that don’t convert cleanly without na_value.

  • Plotting huge arrays point by point; downsample first.

Interview Tips

  • Explain NumPy as the common data layer and show to_numpy and ufuncs on Series.

  • Show computing with NumPy and plotting with Matplotlib.

69

How do you use NumPy arrays with Cython for speed?

Cython compiles Python-like code with optional C type declarations into a C extension, and the standard way to work with NumPy arrays in it is through typed memoryviews such as double[:] or double[:, ::1] (a C-contiguous 2-D array), which give direct, bounds-checkable access to the array’s buffer so that explicit loops compile into plain C loops with no Python overhead per element. You declare loop variables and accumulators with C types (cdef Py_ssize_t i, cdef double total), optionally disable safety checks for the hot function with the @cython.boundscheck(False) and @cython.wraparound(False) decorators once the code is correct, release the GIL with with nogil: (or use prange from cython.parallel with OpenMP for multithreading), allocate outputs with NumPy (np.empty) and return them as arrays. Build the module with a setup.py or pyproject.toml using cythonize, or interactively in Jupyter with %%cython; NumPy 2.x requires Cython 3 for building extensions that use NumPy’s C-API. The speed-up matters only where NumPy can’t vectorize well: loops with dependencies between iterations (recurrences, dynamic programming), complex per-element branching, or algorithms that would otherwise create huge temporary arrays; for work that NumPy already vectorizes, Cython adds little. Numba’s @njit is a lighter alternative that compiles NumPy-using Python functions just in time without a build step.

Read full answer

Applies toNumPy 2.x, Cython 3.x

Key Ingredients

Ingredient Purpose
Typed memoryviews double[:, ::1] Fast buffer access
cdef typed variables C-speed loop counters and accumulators
boundscheck(False), wraparound(False) Remove per-access checks (after testing)
with nogil / prange Release GIL, parallel loops
cythonize in the build Compile .pyx to an extension
np.empty for outputs Allocate result arrays

Code Example

A Cython module (ewma.pyx) computing an exponentially weighted moving average, a recurrence that NumPy can’t vectorize directly:

# cython: language_level=3
import numpy as np
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
def ewma(double[::1] x, double alpha):
    cdef Py_ssize_t i, n = x.shape[0]
    out = np.empty(n, dtype=np.float64)
    cdef double[::1] o = out
    if n == 0:
        return out
    o[0] = x[0]
    with nogil:
        for i in range(1, n):
            o[i] = alpha * x[i] + (1.0 - alpha) * o[i - 1]
    return out

Build configuration and a pure-Python reference to test against:

# setup.py
from setuptools import setup
from Cython.Build import cythonize
import numpy as np

setup(ext_modules=cythonize("ewma.pyx"), include_dirs=[np.get_include()])
import numpy as np

def ewma_reference(x, alpha):
    out = np.empty_like(x, dtype=np.float64)
    out[0] = x[0]
    for i in range(1, len(x)):
        out[i] = alpha * x[i] + (1 - alpha) * out[i - 1]
    return out

print(ewma_reference(np.array([1.0, 2.0, 3.0]), 0.5).tolist())
# [1.0, 1.5, 2.25]
# After building, `from ewma import ewma` gives the same result, typically tens of times faster on large arrays.

include_dirs=[np.get_include()] is needed only when the module uses NumPy’s C-API (cimport numpy); this example uses memoryviews, so it would build without it.

Common Pitfalls

  • Untyped variables, which keep Python overhead (use cython -a to see the annotated “yellow” lines).

  • Disabling bounds checks before the code is correct.

  • Passing non-contiguous arrays to double[::1] (raises an error; use np.ascontiguousarray or double[:]).

  • Rewriting already vectorized NumPy code in Cython for no gain.

Interview Tips

  • Explain typed memoryviews, cdef types, directives and building.

  • Say when Cython helps (loops NumPy can’t vectorize) and mention Numba.

70

How is NumPy used with JAX for accelerated ML?

JAX provides jax.numpy (usually imported as jnp), a near drop-in reimplementation of the NumPy API that runs on CPUs, GPUs and TPUs through the XLA compiler and adds composable function transformations: jax.grad for automatic differentiation, jax.jit for compiling functions into fused, optimized kernels, jax.vmap for automatic vectorization over a batch axis and jax.pmap/sharding for multi-device parallelism. Code written in NumPy style (jnp.dot, jnp.mean, broadcasting, slicing) often ports by changing np to jnp, and NumPy arrays convert in both directions (jnp.asarray(np_array) moves data to the device, np.asarray(jax_array) brings it back). The important differences are: JAX arrays are immutable, so in-place updates become functional updates like x.at[i].set(v) or x.at[i].add(v); randomness uses explicit keys (key = jax.random.key(42), split with jax.random.split) instead of a global state; the default floating type is float32 (enable 64-bit with jax.config.update("jax_enable_x64", True)); functions under jit must be pure and traceable, so Python control flow that depends on array values needs jax.lax.cond/jax.lax.fori_loop or jnp.where, and shapes must be static; and computation is dispatched asynchronously, so benchmarks need .block_until_ready(). A common workflow keeps data loading and preprocessing in NumPy, while the model, loss, gradients and optimizer run in JAX (with libraries such as Flax or Equinox for models and Optax for optimizers).

Read full answer

Applies toJAX 0.4+, NumPy 2.x

NumPy vs JAX

NumPy JAX
Hardware CPU CPU, GPU, TPU
Mutation x[0] = 1 x = x.at[0].set(1)
Randomness default_rng(seed) Explicit keys, split
Default float float64 float32
Gradients Manual jax.grad
Compilation Precompiled C jax.jit via XLA
Batching Broadcasting Broadcasting + jax.vmap
Execution Eager Asynchronous dispatch

Code Example

import jax
import jax.numpy as jnp
import numpy as np

x_np = np.array([1.0, 2.0, 3.0])
x = jnp.asarray(x_np)                                  # NumPy -> JAX (float32 by default)
print(x.dtype, type(np.asarray(x)).__name__)
# float32 ndarray

y = x.at[0].set(10.0)                                  # functional update; x is unchanged
print(x.tolist(), y.tolist())
# [1.0, 2.0, 3.0] [10.0, 2.0, 3.0]

def loss(w, X, t):
    return jnp.mean((X @ w - t) ** 2)

grad_fn = jax.jit(jax.grad(loss))                      # compiled gradient
rng = np.random.default_rng(42)
X = jnp.asarray(rng.normal(size=(100, 3)), dtype=jnp.float32)
t = X @ jnp.array([1.0, -2.0, 0.5])
w = jnp.zeros(3)
for _ in range(200):
    w = w - 0.1 * grad_fn(w, X, t)
print(np.round(np.asarray(w), 2).tolist())
# [1.0, -2.0, 0.5]

print(jax.grad(lambda v: jnp.sum(v ** 2))(x).tolist())  # d/dv sum(v^2) = 2v
# [2.0, 4.0, 6.0]

per_example = jax.vmap(lambda xi: jnp.dot(xi, xi))(X[:4])  # vectorize a per-sample function
print(per_example.shape)
# (4,)

key = jax.random.key(42)
k1, k2 = jax.random.split(key)
noise = jax.random.normal(k1, (2, 3))
print(noise.shape, bool(jnp.all(noise == jax.random.normal(k1, (2, 3)))))   # same key, same numbers
# (2, 3) True

Common Pitfalls

  • Trying in-place assignment on JAX arrays.

  • Reusing the same random key for different draws.

  • Python if on traced values inside jit (use jnp.where or lax.cond).

  • Timing without block_until_ready() and frequent host–device transfers.

  • Unexpected float32 precision when porting NumPy code.

Interview Tips

  • Describe jnp as NumPy on accelerators plus grad, jit and vmap.

  • List the differences: immutability, explicit PRNG keys, float32 default and pure functions.

Sign in to keep reading

You've opened 2 full answers. Sign in to read all 70 NumPy answers, mark what you know and pick up where you left off.

Report a mistake

What's wrong?

All Machine Learning & Data Science subjects