array class is the fundamental data structure in MLX. It represents a multi-dimensional array with a specific data type and shape.
Array Construction
array
Construct an MLX array from Python values or iterables.The data to construct the array from. Can be a Python scalar, list, tuple, or numpy array.
The data type of the array. If not specified, the dtype is inferred from the input data.
The constructed MLX array.
Array Attributes
array.dtype
The data type of the array elements.The data type of the array.
array.shape
The dimensions of the array as a tuple.A tuple containing the size of each dimension.
array.ndim
The number of dimensions of the array.The number of dimensions.
array.size
The total number of elements in the array.The total number of elements.
array.itemsize
The size of one array element in bytes.The size in bytes of one element.
array.nbytes
The total number of bytes used by the array data.The total size in bytes.
array.T
The transposed array (reverses all dimensions).The transposed array.
Array Methods
array.astype
Cast the array to a different data type.The target data type.
The stream on which to schedule the operation.
A new array with the specified dtype.
array.item
Extract the scalar value from a single-element array.The scalar value. Raises an error if the array has more than one element.
array.tolist
Convert the array to a Python list.The array as a nested Python list.
array.reshape
Reshape the array to a new shape without changing its data.The new shape. One dimension can be -1, which will be inferred from the array size.
The stream on which to schedule the operation.
The reshaped array.
array.flatten
Return a flattened (1D) copy of the array.The first axis to flatten.
The last axis to flatten.
The stream on which to schedule the operation.
The flattened array.
array.squeeze
Remove singleton dimensions from the array.Axis or axes to squeeze. If not specified, all singleton dimensions are removed.
The stream on which to schedule the operation.
The squeezed array.
array.transpose
Permute the dimensions of the array.The new order of dimensions. If not specified, reverses all dimensions.
The stream on which to schedule the operation.
The transposed array.
array.swapaxes
Swap two axes of the array.The first axis to swap.
The second axis to swap.
The stream on which to schedule the operation.
The array with swapped axes.
array.moveaxis
Move an axis to a new position.The original position of the axis to move.
The destination position for the axis.
The stream on which to schedule the operation.
The array with moved axis.
Reduction Methods
array.sum
Sum of array elements along specified axes.The axis or axes along which to sum. If not specified, sums all elements.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The sum along the specified axes.
array.mean
Compute the mean of array elements along specified axes.The axis or axes along which to compute the mean.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The mean along the specified axes.
array.var
Compute the variance of array elements along specified axes.The axis or axes along which to compute the variance.
If True, the reduced axes are kept as dimensions with size 1.
Delta degrees of freedom for the divisor (N - ddof).
The stream on which to schedule the operation.
The variance along the specified axes.
array.std
Compute the standard deviation of array elements along specified axes.The axis or axes along which to compute the standard deviation.
If True, the reduced axes are kept as dimensions with size 1.
Delta degrees of freedom for the divisor (N - ddof).
The stream on which to schedule the operation.
The standard deviation along the specified axes.
array.max
Return the maximum value along specified axes.The axis or axes along which to find the maximum.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The maximum values along the specified axes.
array.min
Return the minimum value along specified axes.The axis or axes along which to find the minimum.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The minimum values along the specified axes.
array.prod
Return the product of array elements along specified axes.The axis or axes along which to compute the product.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The product along the specified axes.
array.all
Test whether all array elements along specified axes evaluate to True.The axis or axes along which to test.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
Boolean array indicating if all elements are True.
array.any
Test whether any array element along specified axes evaluates to True.The axis or axes along which to test.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
Boolean array indicating if any elements are True.
array.argmax
Return the indices of the maximum values along specified axes.The axis along which to find the indices.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The indices of the maximum values.
array.argmin
Return the indices of the minimum values along specified axes.The axis along which to find the indices.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The indices of the minimum values.
Element-wise Mathematical Methods
array.abs
Compute the absolute value element-wise.The stream on which to schedule the operation.
The absolute values.
array.sqrt
Compute the square root element-wise.The stream on which to schedule the operation.
The square roots.
array.rsqrt
Compute the reciprocal square root element-wise (1/sqrt(x)).The stream on which to schedule the operation.
The reciprocal square roots.
array.square
Compute the square element-wise.The stream on which to schedule the operation.
The squares.
array.exp
Compute the exponential (e^x) element-wise.The stream on which to schedule the operation.
The exponentials.
array.log
Compute the natural logarithm element-wise.The stream on which to schedule the operation.
The natural logarithms.
array.log2
Compute the base-2 logarithm element-wise.The stream on which to schedule the operation.
The base-2 logarithms.
array.log10
Compute the base-10 logarithm element-wise.The stream on which to schedule the operation.
The base-10 logarithms.
array.log1p
Compute log(1 + x) element-wise.The stream on which to schedule the operation.
The log(1 + x) values.
array.sin
Compute the sine element-wise.The stream on which to schedule the operation.
The sines.
array.cos
Compute the cosine element-wise.The stream on which to schedule the operation.
The cosines.
array.round
Round to the nearest integer element-wise.Number of decimal places to round to.
The stream on which to schedule the operation.
The rounded values.
array.reciprocal
Compute the reciprocal (1/x) element-wise.The stream on which to schedule the operation.
The reciprocals.
Cumulative Methods
array.cumsum
Compute the cumulative sum along an axis.The axis along which to compute the cumulative sum.
If True, compute the cumulative sum in reverse.
If True, include the current element in the sum.
The stream on which to schedule the operation.
The cumulative sum.
array.cumprod
Compute the cumulative product along an axis.The axis along which to compute the cumulative product.
If True, compute the cumulative product in reverse.
If True, include the current element in the product.
The stream on which to schedule the operation.
The cumulative product.
array.cummax
Compute the cumulative maximum along an axis.The axis along which to compute the cumulative maximum.
If True, compute the cumulative maximum in reverse.
If True, include the current element in the maximum.
The stream on which to schedule the operation.
The cumulative maximum.
array.cummin
Compute the cumulative minimum along an axis.The axis along which to compute the cumulative minimum.
If True, compute the cumulative minimum in reverse.
If True, include the current element in the minimum.
The stream on which to schedule the operation.
The cumulative minimum.
array.logsumexp
Compute the log-sum-exp along specified axes.The axis or axes along which to compute the log-sum-exp.
If True, the reduced axes are kept as dimensions with size 1.
The stream on which to schedule the operation.
The log-sum-exp along the specified axes.
array.logcumsumexp
Compute the cumulative log-sum-exp along an axis.The axis along which to compute the cumulative log-sum-exp.
If True, compute in reverse order.
If True, include the current element.
The stream on which to schedule the operation.
The cumulative log-sum-exp.
Complex Number Methods
array.real
Return the real part of a complex array.The real part of the array.
array.imag
Return the imaginary part of a complex array.The imaginary part of the array.
array.conj
Return the complex conjugate element-wise.The stream on which to schedule the operation.
The complex conjugate.
Other Methods
array.split
Split an array into sub-arrays along an axis.Number of equal splits, or indices where to split.
The axis along which to split.
The stream on which to schedule the operation.
List of sub-arrays.
array.diagonal
Return specified diagonal from the array.Offset of the diagonal from the main diagonal.
First axis.
Second axis.
The stream on which to schedule the operation.
The diagonal elements.
array.diag
Extract diagonal or construct diagonal array.The diagonal offset.
The stream on which to schedule the operation.
Extracted diagonal or diagonal matrix.
array.view
Create a view of the array with a different data type.The target data type for the view.
The stream on which to schedule the operation.
A view of the array with the specified dtype.
array.at
Advanced indexing interface for in-place updates.An indexing interface that supports methods like
add, subtract, multiply, divide, and maximum, minimum for in-place-style updates.