> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Npyフォーマットのドキュメント

# Npy

| 入力 | 出力 | 別名 |
| -- | -- | -- |
| ✔  | ✔  |    |

<div id="description">
  ## 説明
</div>

`Npy` フォーマットは、`.npy` ファイル内の NumPy 配列を ClickHouse に読み込むために設計されています。
NumPy のファイルフォーマットは、数値データの配列を効率的に格納するためのバイナリフォーマットです。
インポート時には、ClickHouse は最上位の次元を、単一のカラムを持つ行の配列として扱います。

以下の表は、サポートされている Npy のデータ型と、それに対応する ClickHouse の型を示しています。

<div id="data_types-matching">
  ## データ型の対応
</div>

| Npy データ型 (`INSERT`) | ClickHouse のデータ型                                    | Npy データ型 (`SELECT`) |
| ------------------- | --------------------------------------------------- | ------------------- |
| `i1`                | [Int8](/ja/reference/data-types/int-uint)           | `i1`                |
| `i2`                | [Int16](/ja/reference/data-types/int-uint)          | `i2`                |
| `i4`                | [Int32](/ja/reference/data-types/int-uint)          | `i4`                |
| `i8`                | [Int64](/ja/reference/data-types/int-uint)          | `i8`                |
| `u1`, `b1`          | [UInt8](/ja/reference/data-types/int-uint)          | `u1`                |
| `u2`                | [UInt16](/ja/reference/data-types/int-uint)         | `u2`                |
| `u4`                | [UInt32](/ja/reference/data-types/int-uint)         | `u4`                |
| `u8`                | [UInt64](/ja/reference/data-types/int-uint)         | `u8`                |
| `f2`, `f4`          | [Float32](/ja/reference/data-types/float)           | `f4`                |
| `f8`                | [Float64](/ja/reference/data-types/float)           | `f8`                |
| `S`, `U`            | [String](/ja/reference/data-types/string)           | `S`                 |
|                     | [FixedString](/ja/reference/data-types/fixedstring) | `S`                 |

<div id="example-usage">
  ## 使用例
</div>

<div id="saving-an-array-in-npy-format-using-python">
  ### Pythonで配列を .npy 形式で保存する
</div>

```Python theme={null}
import numpy as np
arr = np.array([[[1],[2],[3]],[[4],[5],[6]]])
np.save('example_array.npy', arr)
```

<div id="reading-a-numpy-file-in-clickhouse">
  ### ClickHouseでNumPyファイルを読み込む
</div>

```sql title="Query" theme={null}
SELECT *
FROM file('example_array.npy', Npy)
```

```response title="Response" theme={null}
┌─array─────────┐
│ [[1],[2],[3]] │
│ [[4],[5],[6]] │
└───────────────┘
```

<div id="selecting-data">
  ### データの取得
</div>

`clickhouse-client` を使用すると、次のコマンドで ClickHouse テーブルからデータを取得し、Npy フォーマットのファイルに保存できます。

```bash theme={null}
$ clickhouse-client --query="SELECT {column} FROM {some_table} FORMAT Npy" > {filename.npy}
```

<div id="format-settings">
  ## フォーマット設定
</div>
