For the most part, indexing an MLX array works the same as indexing a NumPyDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ml-explore/mlx/llms.txt
Use this file to discover all available pages before exploring further.
ndarray. However, there are some important differences to be aware of.
Basic Indexing
You can use regular integers and slices to index arrays:Multi-dimensional Arrays
For multi-dimensional arrays, the... or Ellipsis syntax works as in NumPy:
Creating New Axes
You can index withNone to create a new axis:
Array Indexing
You can also use an array to index another array:slice, ..., and array indices works just as in NumPy.
Differences from NumPy
MLX has limited support for operations where output shapes are dependent on input data. Other examples of these types of operations which MLX does not yet support includenumpy.nonzero and the single input version of numpy.where.
In-Place Updates
In-place updates to indexed arrays are possible in MLX:Unlike NumPy, slicing an array creates a copy, not a view. So mutating it does not mutate the original array:
Nondeterministic Updates
Unlike NumPy, updates to the same location are nondeterministic:Transformations with In-Place Updates
Transformations of functions which use in-place updates are allowed and work as expected:dfdx will have the correct gradient: zeros at idx and ones elsewhere.
Boolean Mask Assignment
MLX supports boolean indices using NumPy syntax. A mask must already be abool_ MLX array or a NumPy ndarray with dtype=bool.
True entry in the mask. For non-scalar assignments, updates must provide at least as many elements as there are True entries in mask.
Boolean Mask Semantics
Boolean masks follow NumPy semantics:- The mask shape must match the shape of the axes it indexes exactly. The only exception is a scalar boolean mask, which broadcasts to the full array.
- Any axes not covered by the mask are taken in full.
(10, 10) applies to the first two axes, so a[mask] selects the 1-D slices a[i, j, :] where mask[i, j] is True. Shapes such as (1, 10, 10) or (10, 10, 1) do not match the indexed axes and therefore raise errors.