<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLShapedArray",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "s:6CoreML13MLShapedArrayV"
  },
  "title" : "MLShapedArray"
}
-->

# MLShapedArray

A machine learning collection type that stores scalar values in a multidimensional array.

```
struct MLShapedArray<Scalar> where Scalar : MLShapedArrayScalar
```

## Overview

A shaped array is a multidimensional array type that’s the Swift counterpart to [`MLMultiArray`](/documentation/CoreML/MLMultiArray). [`MLShapedArray`](/documentation/CoreML/MLShapedArray) is one of the underlying types of `MLFeatureValue` that stores scalar values. You can convert a shaped array to an [`MLMultiArray`](/documentation/CoreML/MLMultiArray) with its [`init(_:)`](/documentation/CoreML/MLMultiArray/init(_:)-wk41) initializer, and convert back to a shaped array with its [`init(_:)`](/documentation/CoreML/MLShapedArray/init(_:)) initializer. All elements in an [`MLShapedArray`](/documentation/CoreML/MLShapedArray) are of the same type, and that type must conform to [`MLShapedArrayScalar`](/documentation/CoreML/MLShapedArrayScalar):

- <doc://com.apple.documentation/documentation/Swift/Int32>
- <doc://com.apple.documentation/documentation/Swift/Float>
- <doc://com.apple.documentation/documentation/Swift/Double>

Each dimension in a shaped array is typically significant or meaningful. For example, a model could have an input that accepts images as a three-dimensional array of pixels, C x H x W. The first dimension, *C*,_ _represents the number of color channels, and the second and third dimensions, *H* and *W*, represent the image’s height and width, respectively. The number of dimensions and size of each dimension define the shaped array’s *shape*.

> Note:
> Some models use a one-dimensional multiarray for an input or output. This type of shaped array is conceptually identical to a conventional <doc://com.apple.documentation/documentation/Swift/Array>.

A shaped array’s [`shape`](/documentation/CoreML/MLMultiArray/shape) property is an integer array in which each element defines the size of the corresponding dimension. To inspect the shape and constraints of a model’s multiarray input or output feature:

1. Access the model’s [`modelDescription`](/documentation/CoreML/MLModel/modelDescription) property.
2. Find the multiarray input or output feature in the model description’s [`inputDescriptionsByName`](/documentation/CoreML/MLModelDescription/inputDescriptionsByName) or [`outputDescriptionsByName`](/documentation/CoreML/MLModelDescription/outputDescriptionsByName) property, respectively.
3. Access the feature description’s [`multiArrayConstraint`](/documentation/CoreML/MLFeatureDescription/multiArrayConstraint) property.
4. Inspect the multiarray constraint’s [`shape`](/documentation/CoreML/MLMultiArrayConstraint/shape) and [`shapeConstraint`](/documentation/CoreML/MLMultiArrayConstraint/shapeConstraint).

## Topics

### Creating a shaped array

[`init(scalar:)`](/documentation/CoreML/MLShapedArray/init(scalar:))

Creates a shaped array with exactly one value and zero dimensions.

[`init(scalars:shape:)`](/documentation/CoreML/MLShapedArray/init(scalars:shape:))

Initialize with a sequence and the shape.

[`init(mutating:shape:)`](/documentation/CoreML/MLShapedArray/init(mutating:shape:))

Creates a new `MLShapedArray` using a pixel buffer as the backing storage.

### Creating a shaped array from another type

[`init(_:)`](/documentation/CoreML/MLShapedArray/init(_:))

[`init(concatenating:alongAxis:)`](/documentation/CoreML/MLShapedArray/init(concatenating:alongAxis:))

Merges a sequence of shaped arrays into one shaped array along an axis.

### Creating a shaped array with pointers to memory

[`init(unsafeUninitializedShape:initializingWith:)`](/documentation/CoreML/MLShapedArray/init(unsafeUninitializedShape:initializingWith:))

Creates a shaped array from a shape and a closure that initializes its memory.

### Creating a shaped array from data

[`init(data:shape:)`](/documentation/CoreML/MLShapedArray/init(data:shape:))

Creates a shaped array from a block of data and a shape.

[`init(data:shape:strides:)`](/documentation/CoreML/MLShapedArray/init(data:shape:strides:))

Creates a shaped array from a block of data, a shape, and strides.

### Shaping the array

[`changingLayout(to:)`](/documentation/CoreML/MLShapedArray/changingLayout(to:))

Returns a copy with the specified buffer layout.

[`expandingShape(at:)`](/documentation/CoreML/MLShapedArray/expandingShape(at:))

Returns a new shaped array with expanded dimensions.

[`reshaped(to:)`](/documentation/CoreML/MLShapedArray/reshaped(to:))

Returns a new reshaped shaped array.

[`squeezingShape()`](/documentation/CoreML/MLShapedArray/squeezingShape())

Returns a new squeezed shaped array.

[`transposed()`](/documentation/CoreML/MLShapedArray/transposed())

Returns a new transposed shaped array.

[`transposed(permutation:)`](/documentation/CoreML/MLShapedArray/transposed(permutation:))

Returns a transposed shaped array using a custom permutation.

### Reading and writing the pixel buffer

[`withMutablePixelBufferIfAvailable(_:)`](/documentation/CoreML/MLShapedArray/withMutablePixelBufferIfAvailable(_:))

Writes to the underlying pixel buffer.

[`withPixelBufferIfAvailable(_:)`](/documentation/CoreML/MLShapedArray/withPixelBufferIfAvailable(_:))

Reads the underlying pixel buffer.

### Modifying a shaped array

[`withUnsafeMutableShapedBufferPointer(using:_:)`](/documentation/CoreML/MLShapedArray/withUnsafeMutableShapedBufferPointer(using:_:))

Calls the given closure with a pointer to the array’s mutable storage that has a
specified buffer layout.

### Encoding and decoding

[`init(from:)`](/documentation/CoreML/MLShapedArray/init(from:))

Creates a shaped array from a decoder.

[`encode(to:)`](/documentation/CoreML/MLShapedArray/encode(to:))

Encode a shaped array.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)