> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# confusion_matrix()

export const GitHubLink = ({url, compact = false}) => <a href={url} target="_blank" rel="noopener noreferrer" className={compact ? "source-link" : "github-source-link"}>
    {compact ? "View source" : <>
    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0024 12c0-6.63-5.37-12-12-12z" />
    </svg>
    GitHub source
      </>}
  </a>;

<GitHubLink compact url="https://github.com/wandb/wandb/blob/main/wandb/plot/confusion_matrix.py#L16" />

<h2 id="function-wandbplotconfusion_matrix">
  <Badge color="blue" size="lg" shape="rounded">関数</Badge> wandb.plot.confusion\_matrix()
</h2>

確率または予測のシーケンスから混同行列を構築します。

```python theme={"system"}
probs: 'Sequence[Sequence[float]] | None' = None,
y_true: 'Sequence[T] | None' = None,
preds: 'Sequence[T] | None' = None,
class_names: 'Sequence[str] | None' = None,
title: 'str' = 'Confusion Matrix Curve',
split_table: 'bool' = False
```

<h2 id="args">
  引数
</h2>

<ResponseField name="probs" type="Sequence[Sequence[float]] | None">
  各クラスの予測確率のシーケンス。シーケンスの形状は (N, K) でなければなりません。ここで N はサンプル数、K はクラス数です。提供された場合、`preds` は提供しないでください。
</ResponseField>

<ResponseField name="y_true" type="Sequence[T] | None">
  正解ラベルのシーケンス。
</ResponseField>

<ResponseField name="preds" type="Sequence[T] | None">
  予測クラスラベルのシーケンス。提供された場合、`probs` は提供しないでください。
</ResponseField>

<ResponseField name="class_names" type="Sequence[str] | None">
  クラス名のシーケンス。提供されない場合、クラス名は "Class\_1"、"Class\_2" などとして定義されます。
</ResponseField>

<ResponseField name="title" type="str">
  混同行列チャートのタイトル。
</ResponseField>

<ResponseField name="split_table" type="bool">
  表を W\&B UI の別セクションに分割するかどうか。`True` の場合、表は「Custom Chart Tables」という名のセクションに表示されます。デフォルトは `False` です。
</ResponseField>

<h2 id="returns">
  戻り値
</h2>

`CustomChart`: W\&B にログできるカスタム チャート オブジェクトです。チャートをログするには、このオブジェクトを `wandb.log()` に渡します。

<h2 id="raises">
  送出される例外
</h2>

* `ValueError`: `probs` と `preds` の両方が指定された場合、または予測の数と true labels の数が等しくない場合。予測された一意のクラスの数がクラス名の数を超える場合、または一意の true labels の数がクラス名の数を超える場合。
* `wandb.Error`: numpy がインストールされていない場合。

<h2 id="examples">
  サンプル
</h2>

野生動物の分類用にランダムな確率を使って混同行列を
ログする例：

```python theme={"system"}
import numpy as np
import wandb

# 野生動物のクラス名を定義します
wildlife_class_names = ["Lion", "Tiger", "Elephant", "Zebra"]

# 正解ラベルをランダムに生成します（10 サンプルに対して 0～3）
wildlife_y_true = np.random.randint(0, 4, size=10)

# 各クラスの確率をランダムに生成します（10 サンプル x 4 クラス）
wildlife_probs = np.random.rand(10, 4)
wildlife_probs = np.exp(wildlife_probs) / np.sum(
    np.exp(wildlife_probs),
    axis=1,
    keepdims=True,
)

# W&B run を初期化し、混同行列をログします
with wandb.init(project="wildlife_classification") as run:
    confusion_matrix = wandb.plot.confusion_matrix(
        probs=wildlife_probs,
        y_true=wildlife_y_true,
        class_names=wildlife_class_names,
        title="Wildlife Classification Confusion Matrix",
    )
    run.log({"wildlife_confusion_matrix": confusion_matrix})
```

この例では、ランダムな確率を使用して混同行列を
生成します。

シミュレーションしたモデルの予測と85%の
精度で混同行列をログします：

```python theme={"system"}
import numpy as np
import wandb

# 野生動物のクラス名を定義します
wildlife_class_names = ["Lion", "Tiger", "Elephant", "Zebra"]

# 200 枚の動物画像の正解ラベルをシミュレートします（不均衡な分布）
wildlife_y_true = np.random.choice(
    [0, 1, 2, 3],
    size=200,
    p=[0.2, 0.3, 0.25, 0.25],
)

# 精度 85% のモデル予測をシミュレートします
wildlife_preds = [
    y_t
    if np.random.rand() < 0.85
    else np.random.choice([x for x in range(4) if x != y_t])
    for y_t in wildlife_y_true
]

# W&B run を初期化し、混同行列をログします
with wandb.init(project="wildlife_classification") as run:
    confusion_matrix = wandb.plot.confusion_matrix(
        preds=wildlife_preds,
        y_true=wildlife_y_true,
        class_names=wildlife_class_names,
        title="Simulated Wildlife Classification Confusion Matrix",
    )
    run.log({"wildlife_confusion_matrix": confusion_matrix})
```

この例では、予測を精度 85% でシミュレートして
混同行列を生成します。
