

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# k-NN 請求和回應格式
<a name="kNN-inference-formats"></a>

所有 Amazon SageMaker AI 內建的演算法，皆採用[常見的資料格式 - 推論](https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-inference.html)中所述之常見輸入推論格式。此主題包含 SageMaker AI k 近鄰演算法的可用輸出格式清單。

## 輸入：CSV 請求格式
<a name="kNN-input-csv"></a>

content-type：text/csv

```
1.2,1.3,9.6,20.3
```

這會接受 `label_size` 或編碼參數。它假設 `label_size` 為 0 和 UTF-8 編碼。

## 輸入：JSON 請求格式
<a name="kNN-input-json"></a>

content-type：application/json

```
{
  "instances": [
    {"data": {"features": {"values": [-3, -1, -4, 2]}}},
    {"features": [3.0, 0.1, 0.04, 0.002]}]
}
```

## 輸入：JSONLINES 請求格式
<a name="kNN-input-jsonlines"></a>

content-type：application/jsonlines

```
{"features": [1.5, 16.0, 14.0, 23.0]}
{"data": {"features": {"values": [1.5, 16.0, 14.0, 23.0]}}
```

## 輸入：RECORDIO 請求格式
<a name="kNN-input-recordio"></a>

content-type：application/x-recordio-protobuf

```
[
    Record = {
        features = {
            'values': {
                values: [-3, -1, -4, 2]  # float32
            }
        },
        label = {}
    },
    Record = {
        features = {
            'values': {
                values: [3.0, 0.1, 0.04, 0.002]  # float32
            }
        },
        label = {}
    },
]
```

## 輸出：JSON 回應格式
<a name="kNN-output-json"></a>

accept：application/json

```
{
  "predictions": [
    {"predicted_label": 0.0},
    {"predicted_label": 2.0}
  ]
}
```

## 輸出：JSONLINES 回應格式
<a name="kNN-output-jsonlines"></a>

accept：application/jsonlines

```
{"predicted_label": 0.0}
{"predicted_label": 2.0}
```

## 輸出：VERBOSE JSON 回應格式
<a name="KNN-output-verbose-json"></a>

在詳細模式中，API 提供從最小到最大的距離向量排序搜尋結果，以及標籤向量中的對應元素。在這個範例中，k 設為 3。

accept：application/json; verbose=true

```
{
  "predictions": [
    {
        "predicted_label": 0.0,
        "distances": [3.11792408, 3.89746071, 6.32548437],
        "labels": [0.0, 1.0, 0.0]
    },
    {
        "predicted_label": 2.0,
        "distances": [1.08470316, 3.04917915, 5.25393973],
        "labels": [2.0, 2.0, 0.0]
    }
  ]
}
```

## 輸出：RECORDIO-PROTOBUF 回應格式
<a name="kNN-output-recordio-protobuf"></a>

content-type：application/x-recordio-protobuf

```
[
    Record = {
        features = {},
        label = {
            'predicted_label': {
                values: [0.0]  # float32
            }
        }
    },
    Record = {
        features = {},
        label = {
            'predicted_label': {
                values: [2.0]  # float32
            }
        }
    }
]
```

## 輸出：VERBOSE RECORDIO-PROTOBUF 回應格式
<a name="kNN-output-verbose-recordio"></a>

在詳細模式中，API 提供從最小到最大的距離向量排序搜尋結果，以及標籤向量中的對應元素。在這個範例中，k 設為 3。

accept：application/x-recordio-protobuf; verbose=true

```
[
    Record = {
        features = {},
        label = {
            'predicted_label': {
                values: [0.0]  # float32
            },
            'distances': {
                values: [3.11792408, 3.89746071, 6.32548437]  # float32
            },
            'labels': {
                values: [0.0, 1.0, 0.0]  # float32
            }
        }
    },
    Record = {
        features = {},
        label = {
            'predicted_label': {
                values: [0.0]  # float32
            },
            'distances': {
                values: [1.08470316, 3.04917915, 5.25393973]  # float32
            },
            'labels': {
                values: [2.0, 2.0, 0.0]  # float32
            }
        }
    }
]
```

## k-NN 演算法的範例輸出
<a name="kNN-sample-output"></a>

針對迴歸器任務：

```
[06/08/2018 20:15:33 INFO 140026520049408] #test_score (algo-1) : ('mse', 0.013333333333333334)
```

針對分類器任務：

```
[06/08/2018 20:15:46 INFO 140285487171328] #test_score (algo-1) : ('accuracy', 0.98666666666666669)
```