ML Foundations

Why numpy.take flattens by default

When axis is omitted, numpy.take indexes the flattened array. Pass an axis to select along rows or columns without crossing row boundaries.

Example results for Why numpy.take flattens by default. default values: 30, 40
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

The default indices address a flattened view

`numpy.take(a, indices)` uses `axis=None` by default. In that mode, NumPy treats the input as a flattened one-dimensional sequence before selecting the requested positions. For a two-row array, consecutive positions can therefore cross from the end of one row to the start of the next. [1]

In the example, index 2 selects `30` and index 3 selects `40`. Those values sit on different rows in the original matrix, so the result has shape `(2,)`, not a two-dimensional slice. The example asserts both the values and shape to make the default explicit. [1]

References: [1]

Use axis to preserve the other dimensions

Set `axis=1` to choose positions within each row. The example selects columns 2 and 0, producing row values 30 then 10, and 60 then 40, with shape `(2, 2)`. Set `axis=0` when the indices refer to rows. [1, 2]

The index list applies to the chosen axis while the other dimensions remain in the result. This is useful when a list of feature or time-step positions should be applied consistently across every sample. Check the intended axis in the input shape before using the output in a model or a downstream matrix operation. [2]

Executed with NumPy; assertions passed.

"""Compare numpy.take's flattened default with selection along an axis."""
import numpy as np

matrix = np.array([[10, 20, 30], [40, 50, 60]])
flattened = np.take(matrix, [2, 3])
columns = np.take(matrix, [2, 0], axis=1)

print("default values:", flattened.tolist())
print("default shape:", flattened.shape)
print("axis=1 values:", columns.tolist())
print("axis=1 shape:", columns.shape)

assert flattened.tolist() == [30, 40]
assert columns.tolist() == [[30, 10], [60, 40]]

Recorded output

default values: [30, 40]
default shape: (2,)
axis=1 values: [[30, 10], [60, 40]]
axis=1 shape: (2, 2)

References: [1], [2]

Choose the operation that matches the indexing intent

Use `take` without an axis when positions are intentionally measured in flattened order. Use an explicit axis when positions mean “these rows” or “these columns.” Ordinary array indexing can express more complex selections, such as different indices per row; `take_along_axis` is designed for that separate case. [1, 2]

This guide covers integer positions and a two-dimensional dense array. It does not discuss out-of-range modes, masked arrays, repeated-index updates, or performance. The executed example demonstrates only the default flattening and one axis-specific selection. [1]

References: [1], [2]

Sources

  1. NumPy take API
  2. NumPy indexing basics