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

> 機械学習関数に関するドキュメント

# 機械学習関数

<div id="evalmlmethod">
  ## evalMLMethod
</div>

学習済みの回帰モデルを使用した予測には、`evalMLMethod` 関数を使用します。詳しくは `linearRegression` のリンクを参照してください。

<div id="stochasticlinearregression">
  ## stochasticLinearRegression
</div>

[stochasticLinearRegression](/docs/ja/reference/functions/aggregate-functions/stochasticLinearRegression) 集約関数は、線形モデルと MSE 損失関数を用いた確率的勾配降下法を実装します。新しいデータに対する予測には `evalMLMethod` を使用します。

<div id="stochasticlogisticregression">
  ## stochasticLogisticRegression
</div>

[stochasticLogisticRegression](/docs/ja/reference/functions/aggregate-functions/stochasticLogisticRegression) 集約関数は、二値分類問題に対する確率的勾配降下法を実装します。新しいデータに対する予測には `evalMLMethod` を使用します。

<div id="naivebayesclassifier">
  ## naiveBayesClassifier
</div>

N-gram とラプラス平滑化を用いた Naive Bayes モデルで入力テキストを分類します。使用する前に、ClickHouse でモデルを設定しておく必要があります。

**構文**

```sql theme={null}
naiveBayesClassifier(model_name, input_text);
```

**引数**

* `model_name` — 事前設定済みモデルの名前。[String](/docs/ja/reference/data-types/string)
  モデルは ClickHouse の設定ファイルで定義されている必要があります (以下を参照) 。
* `input_text` — 分類対象のテキスト。[String](/docs/ja/reference/data-types/string)
  入力は、指定されたとおりにそのまま処理されます (大文字・小文字や句読点は保持されます) 。

**戻り値**

* 予測されたクラス ID を符号なし整数で返します。[UInt32](/docs/ja/reference/data-types/int-uint)
  クラス ID は、モデル構築時に定義されたカテゴリに対応します。

**例**

言語検出モデルを使用してテキストを分類します：

```sql theme={null}
SELECT naiveBayesClassifier('language', 'How are you?');
```

```response theme={null}
┌─naiveBayesClassifier('language', 'How are you?')─┐
│ 0                                                │
└──────────────────────────────────────────────────┘
```

*結果の `0` は英語、`1` はフランス語を表す場合があります。どのクラスが何を意味するかは、学習データによって異なります。*

***

<div id="implementation-details">
  ### 実装の詳細
</div>

**アルゴリズム**
[こちら](https://web.stanford.edu/~jurafsky/slp3/4.pdf)に基づく N-gram の確率を用い、未出現の N-gram に対応するため、[ラプラス平滑化](https://en.wikipedia.org/wiki/Additive_smoothing)を適用した Naive Bayes 分類アルゴリズムを使用します。

**主な機能**

* 任意のサイズの N-gram をサポート
* 3 つのトークン化モード:
  * `byte`: 生のバイト列を対象に処理します。各バイトが 1 つのトークンになります。
  * `codepoint`: UTF‑8 からデコードされた Unicode スカラー値を対象に処理します。各コードポイントが 1 つのトークンになります。
  * `token`: Unicode の空白文字の連続 (正規表現 `\s+`) で分割します。トークンは空白以外の連続した部分文字列で、句読点が隣接している場合はその句読点もトークンに含まれます (例: `"you?"` は 1 つのトークンです) 。

***

<div id="model-configuration">
  ### モデル設定
</div>

言語検出用の Naive Bayes モデルを作成するためのサンプルソースコードは、[こちら](https://github.com/nihalzp/ClickHouse-NaiveBayesClassifier-Models)で確認できます。

さらに、サンプルモデルと関連する config ファイルは[こちら](https://github.com/nihalzp/ClickHouse-NaiveBayesClassifier-Models/tree/main/models)で公開されています。

以下は、ClickHouse における Naive Bayes モデルの設定例です。

```xml theme={null}
<clickhouse>
    <nb_models>
        <model>
            <name>sentiment</name>
            <path>/etc/clickhouse-server/config.d/sentiment.bin</path>
            <n>2</n>
            <mode>token</mode>
            <alpha>1.0</alpha>
            <priors>
                <prior>
                    <class>0</class>
                    <value>0.6</value>
                </prior>
                <prior>
                    <class>1</class>
                    <value>0.4</value>
                </prior>
            </priors>
        </model>
    </nb_models>
</clickhouse>
```

**設定パラメータ**

| パラメータ      | 説明                                                                                    | 例                                                        | デフォルト |
| ---------- | ------------------------------------------------------------------------------------- | -------------------------------------------------------- | ----- |
| **name**   | 一意のモデル識別子                                                                             | `language_detection`                                     | *必須*  |
| **path**   | モデルのバイナリファイルへの完全パス                                                                    | `/etc/clickhouse-server/config.d/language_detection.bin` | *必須*  |
| **mode**   | トークン化方式:<br />- `byte`: バイト列<br />- `codepoint`: Unicode 文字<br />- `token`: 単語トークン    | `token`                                                  | *必須*  |
| **n**      | N-gram のサイズ (`token` モード) :<br />- `1`=単語 1 語<br />- `2`=単語 2 語の組<br />- `3`=単語 3 語の組 | `2`                                                      | *必須*  |
| **alpha**  | 分類時に、モデルに現れない N-gram に対応するために使用するラプラス平滑化係数                                            | `0.5`                                                    | `1.0` |
| **priors** | クラス確率 (各クラスに属するドキュメントの割合)                                                             | クラス 0 が 60%、クラス 1 が 40%                                  | 均等分布  |

**モデル学習ガイド**

**ファイルフォーマット**
可読形式では、`n=1` かつ `token` モードの場合、モデルは次のようになります。

```text theme={null}
<class_id> <n-gram> <count>
0 excellent 15
1 refund 28
```

`n=3` かつ `codepoint` モードの場合、次のようになります。

```text theme={null}
<class_id> <n-gram> <count>
0 exc 15
1 ref 28
```

人が読める形式は ClickHouse では直接使用されず、以下で説明するバイナリ形式に変換する必要があります。

**バイナリ形式の詳細**
各 N-gram は、次の形式で格納されます。

1. 4 バイトの `class_id` (UInt、リトルエンディアン)
2. 4 バイトの `n-gram` のバイト長 (UInt、リトルエンディアン)
3. 生の `n-gram` バイト列
4. 4 バイトの `count` (UInt、リトルエンディアン)

**前処理の要件**
ドキュメントコーパスからモデルを作成する前に、指定された `mode` と `n` に従って N-gram を抽出できるよう、ドキュメントを前処理する必要があります。以下に、その前処理の手順を示します。

1. **トークン化モードに応じて、各ドキュメントの先頭と末尾に境界マーカーを追加します。**

   * **Byte**: `0x01` (先頭) 、`0xFF` (末尾)
   * **Codepoint**: `U+10FFFE` (先頭) 、`U+10FFFF` (末尾)
   * **Token**: `<s>` (先頭) 、`</s>` (末尾)

   *注:* `(n - 1)` 個のトークンが、ドキュメントの先頭と末尾の両方に追加されます。

2. **`token` モードでの `n=3` の例:**

   * **Document:** `"ClickHouse is fast"`
   * **Processed as:** `<s> <s> ClickHouse is fast </s> </s>`
   * **生成されるトライグラム:**
     * `<s> <s> ClickHouse`
     * `<s> ClickHouse is`
     * `ClickHouse is fast`
     * `is fast </s>`
     * `fast </s> </s>`

`byte` モードおよび `codepoint` モードでのモデル作成を簡単にするには、まず文書をトークン列に分割しておくと便利です (`byte` モードでは `byte` のリスト、`codepoint` モードでは `codepoint` のリスト) 。次に、文書の先頭に `n - 1` 個の開始トークンを、末尾に `n - 1` 個の終了トークンを追加します。最後に、N-gram を生成してシリアライズ済みファイルに書き込みます。

***

{/*AUTOGENERATED_START*/}

<div id="evalmlmethod">
  ## evalMLMethod
</div>

導入バージョン: v20.1.0

学習済みの機械学習モデルを入力された特徴量に適用し、予測を生成します。

**構文**

```sql theme={null}
evalMLMethod(model, x1[, x2, ...])
```

**引数**

* `model` — 学習済みの機械学習モデル。[`AggregateFunctionState`](/docs/ja/reference/data-types/aggregatefunction)
* `x1, x2, ...` — 予測に用いる特徴量の値。[`Float*`](/docs/ja/reference/data-types/float) または [`(U)Int*`](/docs/ja/reference/data-types/int-uint)

**戻り値**

学習済みモデルに基づいて予測された値を返します。[`Float64`](/docs/ja/reference/data-types/float)

**例**

**使用例**

```sql title=Query theme={null}
SELECT
evalMLMethod(model, trip_distance),
total_amount
FROM trips
LEFT JOIN models ON year = toYear(pickup_datetime)
LIMIT 5
```

```response title=Response theme={null}
┌─evalMLMethod(model, trip_distance)─┬─total_amount─┐
│ 8.087692004204174                  │ 5.4          │
│ 7.861181608305352                  │ 4.6          │
│ 26.661544467907536                 │ 23.4         │
│ 8.767223191900637                  │ 5.8          │
│ 10.80581675499003                  │ 9            │
└────────────────────────────────────┴──────────────┘
```

<div id="naivebayesclassifier">
  ## naiveBayesClassifier
</div>

導入バージョン: v25.11.0

[`NAIVE_BAYES`](/docs/ja/reference/statements/create/dictionary/layouts/naive-bayes) Dictionary を使用して入力テキストを分類します。`dictGet(dictionary_name, class_attribute, input_text)` と同じ予測クラス値を返します。ここで、class\_attribute は Dictionary の[レイアウト](/docs/ja/reference/statements/create/dictionary/layouts/naive-bayes#layout-parameters)で設定されたクラスラベル属性の名前です。`dictGet` とは異なり、戻り値の型はクラス属性の宣言型ではなく常に `UInt32` であり、`input_text` は `String` である必要があります (キー型変換は適用されません) 。

<Note>
  この関数は非決定論的です。同じ引数に対して異なる結果を返すことがあります。
</Note>

**構文**

```sql theme={null}
naiveBayesClassifier(dictionary_name, input_text)
```

**引数**

* `dictionary_name` — NAIVE\_BAYES レイアウトの Dictionary 名。[`String`](/docs/ja/reference/data-types/string)
* `input_text` — 分類対象のテキスト。[`String`](/docs/ja/reference/data-types/string)

**戻り値**

予測されたクラスID。[`UInt32`](/docs/ja/reference/data-types/int-uint)

**例**

**テキストを分類**

```sql title=Query theme={null}
SELECT naiveBayesClassifier('model', 'some text');
```

```response title=Response theme={null}
0
```

<div id="naiveBayesClassifierWithAllProbs">
  ## naiveBayesClassifierWithAllProbs
</div>

導入バージョン: v26.7.0

[`NAIVE_BAYES`](/docs/ja/reference/statements/create/dictionary/layouts/naive-bayes) Dictionaryを使用して入力テキストを分類し、すべてのクラスとその確率を、確率の高い順に返します。

<Note>
  この関数は非決定論的です。同じ引数に対して異なる結果を返すことがあります。
</Note>

**構文**

```sql theme={null}
naiveBayesClassifierWithAllProbs(dictionary_name, input_text)
```

**引数**

* `dictionary_name` — NAIVE\_BAYES レイアウトを使用するDictionary名。[`String`](/docs/ja/reference/data-types/string)
* `input_text` — 分類対象のテキスト。[`String`](/docs/ja/reference/data-types/string)

**戻り値**

確率の高い順に並んだ (class\_id, probability) の Tuple からなる Array。[`Array(Tuple(UInt32, Float64))`](/docs/ja/reference/data-types/array)

**例**

**各クラスの確率**

```sql title=Query theme={null}
SELECT naiveBayesClassifierWithAllProbs('model', 'some text');
```

```response title=Response theme={null}
[(0,0.85),(1,0.15)]
```

<div id="naiveBayesClassifierWithProb">
  ## naiveBayesClassifierWithProb
</div>

導入バージョン: v26.7.0

[`NAIVE_BAYES`](/docs/ja/reference/statements/create/dictionary/layouts/naive-bayes) Dictionary を使用して入力テキストを分類し、予測されたクラスとその確率を返します。

<Note>
  この関数は非決定論的です。同じ引数に対しても異なる結果を返す場合があります。
</Note>

**構文**

```sql theme={null}
naiveBayesClassifierWithProb(dictionary_name, input_text)
```

**引数**

* `dictionary_name` — NAIVE\_BAYES レイアウトを持つ Dictionary の名前。[`String`](/docs/ja/reference/data-types/string)
* `input_text` — 分類対象のテキスト。[`String`](/docs/ja/reference/data-types/string)

**戻り値**

(class\_id, probability) から成る Tuple。[`Tuple(UInt32, Float64)`](/docs/ja/reference/data-types/tuple)

**例**

**確率とともに分類**

```sql title=Query theme={null}
SELECT naiveBayesClassifierWithProb('model', 'some text');
```

```response title=Response theme={null}
(0,0.85)
```
