<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CreateMLComponents",
  "identifier" : "/documentation/CreateMLComponents/ClassificationMetrics",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Create ML Components"
    ],
    "preciseIdentifier" : "s:18CreateMLComponents21ClassificationMetricsV"
  },
  "title" : "ClassificationMetrics"
}
-->

# ClassificationMetrics

Classification metrics.

```
struct ClassificationMetrics<Label> where Label : Hashable
```

## Topics

### Creating the distribution

[`init<Predicted, Correct>(Predicted, Correct)`](/documentation/CreateMLComponents/ClassificationMetrics/init(_:_:))

Creates classification metrics for predicted and ground truth labels.

[`init()`](/documentation/CreateMLComponents/ClassificationMetrics/init())

Creates empty classification metrics.

[`init(_:)`](/documentation/CreateMLComponents/ClassificationMetrics/init(_:))

Creates classification metrics for a sequence of predicted and ground truth label pairs.

[`init(some Sequence<(predicted: Label, label: Label)>, labels: Set<Label>)`](/documentation/CreateMLComponents/ClassificationMetrics/init(_:labels:))

Creates classification metrics for a sequence of predicted and ground truth label pairs.

[`init<Predicted, Correct>(predicted: Predicted, groundTruth: Correct, labels: Set<Label>)`](/documentation/CreateMLComponents/ClassificationMetrics/init(predicted:groundTruth:labels:))

Creates classification metrics for predicted and ground truth labels.

### Getting the properties

[`var accuracy: Double`](/documentation/CreateMLComponents/ClassificationMetrics/accuracy)

The number of correctly classified examples out of the total number of examples.

[`var exampleCount: Int`](/documentation/CreateMLComponents/ClassificationMetrics/exampleCount)

The number of examples used to compute the metrics.

[`var labels: Set<Label>`](/documentation/CreateMLComponents/ClassificationMetrics/labels)

The set of labels.

[`var restrictToKnownLabels: Bool`](/documentation/CreateMLComponents/ClassificationMetrics/restrictToKnownLabels)

A Boolean value indicating whether to restrict metrics to labels in the labels set.

### Computing and scoring

[`func makeConfusionMatrix() -> MLShapedArray<Float>`](/documentation/CreateMLComponents/ClassificationMetrics/makeConfusionMatrix())

Computes the confusion matrix.

[`func precisionScore(label: Label) -> Double`](/documentation/CreateMLComponents/ClassificationMetrics/precisionScore(label:))

Computes the precision score for a class label.

[`func recallScore(label: Label) -> Double`](/documentation/CreateMLComponents/ClassificationMetrics/recallScore(label:))

Computes the recall score for a class label.

[`func count(label: Label) -> Int`](/documentation/CreateMLComponents/ClassificationMetrics/count(label:))

Returns the number of times a label appeared in the ground truth collection.

[`func count(predicted: Label) -> Int`](/documentation/CreateMLComponents/ClassificationMetrics/count(predicted:))

Returns the number of times a label appeared in the predicted collection.

[`func count(predicted: Label, label: Label) -> Int`](/documentation/CreateMLComponents/ClassificationMetrics/count(predicted:label:))

Returns the number of times a predicted, true label pair appeared in the label collections.

[`func trueNegativeCount(of: Label) -> Int`](/documentation/CreateMLComponents/ClassificationMetrics/trueNegativeCount(of:))

Returns the number of times a label was not in the predicted or ground truth collections.

[`func truePositiveCount(of: Label) -> Int`](/documentation/CreateMLComponents/ClassificationMetrics/truePositiveCount(of:))

Returns the number of times the predicted label matched the true label.

[`func falseNegativeCount(of: Label) -> Int`](/documentation/CreateMLComponents/ClassificationMetrics/falseNegativeCount(of:))

Returns the number of times a true label was not predicted.

[`func falsePositiveCount(of: Label) -> Int`](/documentation/CreateMLComponents/ClassificationMetrics/falsePositiveCount(of:))

Returns the number of times the predicted label did not match the true label.

[`func f1Score(label: Label) -> Double`](/documentation/CreateMLComponents/ClassificationMetrics/f1Score(label:))

Computes the F1 score for a class label.

[`func mapLabels<T>((Label) throws -> T) rethrows -> ClassificationMetrics<T>`](/documentation/CreateMLComponents/ClassificationMetrics/mapLabels(_:))

Returns new classification metrics where the labels are the result of applying a transformation.

### Updating the metrics

[`func add(some Sequence<(predicted: Label, label: Label)>)`](/documentation/CreateMLComponents/ClassificationMetrics/add(_:))

Updates the metrics with more predicted and ground truth label pairs.

[`func add(predicted: some Sequence<Label>, groundTruth: some Sequence<Label>)`](/documentation/CreateMLComponents/ClassificationMetrics/add(predicted:groundTruth:))

Updates the metrics with more predicted and ground truth labels.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

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)