# KMeansND - 圖表函數

KMeansND() 會套用 K 平均演算法叢集以評估圖表的列，而對於每個圖表列，會針對此資料點指派到的叢集顯示叢集 ID。叢集演算法使用的欄由參數 coordinate\_1 和 coordinate\_2, 等來判定，最多 n 欄。這些都是彙總。建立的叢集數量由 num\_clusters 參數判定。

KMeansND 每個資料點傳回一個值。傳回的值是雙值，也是對應於每個資料點指派到的叢集的整數值。

語法：

KMeansND(num\_clusters, num\_iter, coordinate\_1, coordinate\_2 [,coordinate\_3 [, ...]])

傳回的資料類型： 雙值

引數：

引數

| 引數 | 描述 |
| --- | --- |
| num\_clusters | 指定叢集數量的整數。 |
| num\_iter | 叢集反覆項目的數量與重新初始化的叢集中心。 |
| coordinate\_1 | 計算第一個座標的彙總，通常是 x 軸 (屬於可從圖表製作的散佈圖)。其他參數會計算第二、第三和第四個座標等。 |

[](https://help.qlik.com/en-US/cloud-services/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/RelationalFunctions/KMeansND.htm)

#### 範例：圖表運算式

在此範例中，我們使用 Iris 資料集建立散佈圖，然後使用 KMeans 依運算式為資料著色。

我們也會建立 num\_clusters 引數的變數，然後使用變數輸入方塊來變更叢集的數量。

此外，我們會建立 num\_iter 引數的變數，然後使用第二變數輸入方塊來變更反覆項目的數量。

Iris 資料集公開用於各種格式。我們已提供資料作為要在 Qlik Sense 中使用資料載入編輯器載入的內嵌表格。請注意，在此範圍中，我們已將 Id 欄新增至資料表格。

[Iris 資料集：Qlik Sense 中資料載入編輯器的內嵌載入](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/RelationalFunctions/KMeansND.htm#IrisData)

在 Qlik Sense 中載入資料後，我們進行下列事項：

1. 將散佈圖圖表拖曳至新的工作表。將圖表命名為花瓣 (依運算式著色)。
2. 建立變數以指定叢集的數量。對於變數名稱，輸入 KmeansPetalClusters。對於變數定義，輸入 =2。
3. 建立變數以指定反覆項目的數量。對於變數名稱，輸入 KmeansNumberIterations。對於變數定義，輸入 =1。
4. 設定圖表的資料：
   1. 在維度之下，在泡泡球選擇欄位的 id。輸入標籤的叢集 ID。

   2. 在量值之下，為 X 軸的運算式選擇 Sum([petal.length])。

   3. 在量值之下，為 Y 軸的運算式選擇 Sum([petal.width])。

      花瓣 (依運算式著色) 圖表的資料設定

      花瓣 (依運算式著色) 圖表的資料設定

      花瓣 (依運算式著色)花瓣 (依運算式著色) 圖表的資料設定![Configure Data for Petal (color by expression) chart](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/ui_KmeansData.png)

      資料點繪製於圖表上。

      花瓣 (依運算式著色) 圖表的資料點

      花瓣 (依運算式著色) 圖表的資料點

      花瓣 (依運算式著色)花瓣 (依運算式著色) 圖表的資料點![Data points on Petal (color by expression) chart.](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/ui_KmeansChartDataPoints.png)
5. 設定圖表的外觀：
   1. 在色彩和圖例之下，為色彩選擇 自訂。

   2. 選擇依運算式為圖表著色。

   3. 在運算式輸入下列內容：kmeansnd($(KmeansPetalClusters),$(KmeansNumberIterations), Sum([petal.length]), Sum([petal.width]),Sum([sepal.length]), Sum([sepal.width]))

      請注意，KmeansPetalClusters 是我們設定為 2 的變數。KmeansNumberIterations 是我們設定為 1 的變數。

      或者，請輸入下列內容：kmeansnd(2, 2, Sum([petal.length]), Sum([petal.width]),Sum([sepal.length]), Sum([sepal.width]))

   4. 取消選取運算式為色彩代碼核取方塊。

   5. 在標籤輸入下列內容：叢集 ID

      花瓣 (依運算式著色) 圖表的外觀設定

      花瓣 (依運算式著色) 圖表的外觀設定

      花瓣 (依運算式著色)花瓣 (依運算式著色) 圖表的外觀設定![花瓣 (依運算式著色) 圖表的外觀](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/ui_KmeansAppearanceND_269x1115.png)

      圖表上的兩個叢集依 KMeans 運算式著色。

      叢集依花瓣 (依運算式著色) 圖表上的運算式著色

      叢集依花瓣 (依運算式著色) 圖表上的運算式著色

      叢集依花瓣 (依運算式著色)花瓣 (依運算式著色) 圖表上的運算式著色![叢集依花瓣 (依運算式著色) 圖表上的運算式著色。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/ui_KmeansChartColorByExpressionND.png)
6. 為叢集數量新增一個變數輸入方塊。
   1. 在資產面板中，開啟圖表。

   2. 在儀表板物件之下，將變數輸入方塊拖曳到工作表上。

      如果我們無法存取儀表板搭售，我們仍然可以使用我們建立的變數，或直接在運算式中作為整數來變更叢集數量。

   3. 在外觀下，按一下一般。

   4. 輸入下列內容作為標題：叢集

   5. 按一下變數。

   6. 選擇下列變數作為名稱：KmeansPetalClusters。

   7. 選擇滑桿作為顯示為。

   8. 選擇值，並視需要設定，

      叢集變數輸入框的外觀

      叢集變數輸入框的外觀

      叢集叢集變數輸入框的外觀 ![叢集變數輸入框的外觀](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/ui_KmeansVariableInputBox.png)
7. 為反覆項目數量新增變數輸入方塊。
   1. 將變數輸入方塊拖曳到工作表。
   2. 在外觀之下，選擇一般。
   3. 在標題輸入下列內容：反覆項目
   4. 在外觀之下，選擇變數。
   5. 在名稱之下選擇下列變數：KmeansNumberIterations。
   6. 按需求進行其他設定。

   我們現在可以使用變數輸入方塊中的滑桿來變更叢集和反覆項目的數量。

   叢集依花瓣 (依運算式著色) 圖表上的運算式著色

   叢集依花瓣 (依運算式著色) 圖表上的運算式著色

   叢集依花瓣 (依運算式著色)花瓣 (依運算式著色) 圖表上的運算式著色 ![叢集依花瓣 (依運算式著色) 圖表上的運算式著色。](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/ui_KmeansChartColorByExpressionVariableChangeND.png)

自動叢集

KMeans 函數使用稱為深度差 (DeD) 的方法支援自動叢集。若使用者對叢集數量設定 0，會判定該資料集的最佳叢集數量。請注意，若沒有明確以整數傳回叢集數量 (k)，則會在 KMeans 演算法內計算。例如，若對 KmeansPetalClusters 的值在函數中指定 0，或透過變數輸入方塊設定 0，則會根據最佳叢集數量為資料集自動計算叢集指派。 就 Iris 資料集而言，若對叢集數量選取 0，則演算法會為此資料集判定 (自動叢集) 最佳叢集數量 (3)。

KMeans 深度差方法可在 (k) 設定為 0 時判定最佳叢集數量。

KMeans 深度差方法可在 (k) 設定為 0 時判定最佳叢集數量。

KMeans 深度差方法可在 (k) 設定為 0 時判定最佳叢集數量。 ![KMeans 深度差方法可判定最佳叢集數量](https://help.qlik.com/zh-TW/cloud-services/Subsystems/Hub/Content/Resources/Images/ui_KmeansNdDed.png)

#### Iris 資料集：Qlik Sense 中資料載入編輯器的內嵌載入

```
IrisData:
Load * Inline [
sepal.length, sepal.width, petal.length, petal.width, variety, id
5.1, 3.5, 1.4, 0.2, Setosa, 1
4.9, 3, 1.4, 0.2, Setosa, 2
4.7, 3.2, 1.3, 0.2, Setosa, 3
4.6, 3.1, 1.5, 0.2, Setosa, 4
5, 3.6, 1.4, 0.2, Setosa, 5
5.4, 3.9, 1.7, 0.4, Setosa, 6
4.6, 3.4, 1.4, 0.3, Setosa, 7
5, 3.4, 1.5, 0.2, Setosa, 8
4.4, 2.9, 1.4, 0.2, Setosa, 9
4.9, 3.1, 1.5, 0.1, Setosa, 10
5.4, 3.7, 1.5, 0.2, Setosa, 11
4.8, 3.4, 1.6, 0.2, Setosa, 12
4.8, 3, 1.4, 0.1, Setosa, 13
4.3, 3, 1.1, 0.1, Setosa, 14
5.8, 4, 1.2, 0.2, Setosa, 15
5.7, 4.4, 1.5, 0.4, Setosa, 16
5.4, 3.9, 1.3, 0.4, Setosa, 17
5.1, 3.5, 1.4, 0.3, Setosa, 18
5.7, 3.8, 1.7, 0.3, Setosa, 19
5.1, 3.8, 1.5, 0.3, Setosa, 20
5.4, 3.4, 1.7, 0.2, Setosa, 21
5.1, 3.7, 1.5, 0.4, Setosa, 22
4.6, 3.6, 1, 0.2, Setosa, 23
5.1, 3.3, 1.7, 0.5, Setosa, 24
4.8, 3.4, 1.9, 0.2, Setosa, 25
5, 3, 1.6, 0.2, Setosa, 26
5, 3.4, 1.6, 0.4, Setosa, 27
5.2, 3.5, 1.5, 0.2, Setosa, 28
5.2, 3.4, 1.4, 0.2, Setosa, 29
4.7, 3.2, 1.6, 0.2, Setosa, 30
4.8, 3.1, 1.6, 0.2, Setosa, 31
5.4, 3.4, 1.5, 0.4, Setosa, 32
5.2, 4.1, 1.5, 0.1, Setosa, 33
5.5, 4.2, 1.4, 0.2, Setosa, 34
4.9, 3.1, 1.5, 0.1, Setosa, 35
5, 3.2, 1.2, 0.2, Setosa, 36
5.5, 3.5, 1.3, 0.2, Setosa, 37
4.9, 3.1, 1.5, 0.1, Setosa, 38
4.4, 3, 1.3, 0.2, Setosa, 39
5.1, 3.4, 1.5, 0.2, Setosa, 40
5, 3.5, 1.3, 0.3, Setosa, 41
4.5, 2.3, 1.3, 0.3, Setosa, 42
4.4, 3.2, 1.3, 0.2, Setosa, 43
5, 3.5, 1.6, 0.6, Setosa, 44
5.1, 3.8, 1.9, 0.4, Setosa, 45
4.8, 3, 1.4, 0.3, Setosa, 46
5.1, 3.8, 1.6, 0.2, Setosa, 47
4.6, 3.2, 1.4, 0.2, Setosa, 48
5.3, 3.7, 1.5, 0.2, Setosa, 49
5, 3.3, 1.4, 0.2, Setosa, 50
7, 3.2, 4.7, 1.4, Versicolor, 51
6.4, 3.2, 4.5, 1.5, Versicolor, 52
6.9, 3.1, 4.9, 1.5, Versicolor, 53
5.5, 2.3, 4, 1.3, Versicolor, 54
6.5, 2.8, 4.6, 1.5, Versicolor, 55
5.7, 2.8, 4.5, 1.3, Versicolor, 56
6.3, 3.3, 4.7, 1.6, Versicolor, 57
4.9, 2.4, 3.3, 1, Versicolor, 58
6.6, 2.9, 4.6, 1.3, Versicolor, 59
5.2, 2.7, 3.9, 1.4, Versicolor, 60
5, 2, 3.5, 1, Versicolor, 61
5.9, 3, 4.2, 1.5, Versicolor, 62
6, 2.2, 4, 1, Versicolor, 63
6.1, 2.9, 4.7, 1.4, Versicolor, 64
5.6, 2.9, 3.6, 1.3, Versicolor, 65
6.7, 3.1, 4.4, 1.4, Versicolor, 66
5.6, 3, 4.5, 1.5, Versicolor, 67
5.8, 2.7, 4.1, 1, Versicolor, 68
6.2, 2.2, 4.5, 1.5, Versicolor, 69
5.6, 2.5, 3.9, 1.1, Versicolor, 70
5.9, 3.2, 4.8, 1.8, Versicolor, 71
6.1, 2.8, 4, 1.3, Versicolor, 72
6.3, 2.5, 4.9, 1.5, Versicolor, 73
6.1, 2.8, 4.7, 1.2, Versicolor, 74
6.4, 2.9, 4.3, 1.3, Versicolor, 75
6.6, 3, 4.4, 1.4, Versicolor, 76
6.8, 2.8, 4.8, 1.4, Versicolor, 77
6.7, 3, 5, 1.7, Versicolor, 78
6, 2.9, 4.5, 1.5, Versicolor, 79
5.7, 2.6, 3.5, 1, Versicolor, 80
5.5, 2.4, 3.8, 1.1, Versicolor, 81
5.5, 2.4, 3.7, 1, Versicolor, 82
5.8, 2.7, 3.9, 1.2, Versicolor, 83
6, 2.7, 5.1, 1.6, Versicolor, 84
5.4, 3, 4.5, 1.5, Versicolor, 85
6, 3.4, 4.5, 1.6, Versicolor, 86
6.7, 3.1, 4.7, 1.5, Versicolor, 87
6.3, 2.3, 4.4, 1.3, Versicolor, 88
5.6, 3, 4.1, 1.3, Versicolor, 89
5.5, 2.5, 4, 1.3, Versicolor, 90
5.5, 2.6, 4.4, 1.2, Versicolor, 91
6.1, 3, 4.6, 1.4, Versicolor, 92
5.8, 2.6, 4, 1.2, Versicolor, 93
5, 2.3, 3.3, 1, Versicolor, 94
5.6, 2.7, 4.2, 1.3, Versicolor, 95
5.7, 3, 4.2, 1.2, Versicolor, 96
5.7, 2.9, 4.2, 1.3, Versicolor, 97
6.2, 2.9, 4.3, 1.3, Versicolor, 98
5.1, 2.5, 3, 1.1, Versicolor, 99
5.7, 2.8, 4.1, 1.3, Versicolor, 100
6.3, 3.3, 6, 2.5, Virginica, 101
5.8, 2.7, 5.1, 1.9, Virginica, 102
7.1, 3, 5.9, 2.1, Virginica, 103
6.3, 2.9, 5.6, 1.8, Virginica, 104
6.5, 3, 5.8, 2.2, Virginica, 105
7.6, 3, 6.6, 2.1, Virginica, 106
4.9, 2.5, 4.5, 1.7, Virginica, 107
7.3, 2.9, 6.3, 1.8, Virginica, 108
6.7, 2.5, 5.8, 1.8, Virginica, 109
7.2, 3.6, 6.1, 2.5, Virginica, 110
6.5, 3.2, 5.1, 2, Virginica, 111
6.4, 2.7, 5.3, 1.9, Virginica, 112
6.8, 3, 5.5, 2.1, Virginica, 113
5.7, 2.5, 5, 2, Virginica, 114
5.8, 2.8, 5.1, 2.4, Virginica, 115
6.4, 3.2, 5.3, 2.3, Virginica, 116
6.5, 3, 5.5, 1.8, Virginica, 117
7.7, 3.8, 6.7, 2.2, Virginica, 118
7.7, 2.6, 6.9, 2.3, Virginica, 119
6, 2.2, 5, 1.5, Virginica, 120
6.9, 3.2, 5.7, 2.3, Virginica, 121
5.6, 2.8, 4.9, 2, Virginica, 122
7.7, 2.8, 6.7, 2, Virginica, 123
6.3, 2.7, 4.9, 1.8, Virginica, 124
6.7, 3.3, 5.7, 2.1, Virginica, 125
7.2, 3.2, 6, 1.8, Virginica, 126
6.2, 2.8, 4.8, 1.8, Virginica, 127
6.1, 3, 4.9, 1.8, Virginica, 128
6.4, 2.8, 5.6, 2.1, Virginica, 129
7.2, 3, 5.8, 1.6, Virginica, 130
7.4, 2.8, 6.1, 1.9, Virginica, 131
7.9, 3.8, 6.4, 2, Virginica, 132
6.4, 2.8, 5.6, 2.2, Virginica, 133
6.3, 2.8, 5.1, 1.5, Virginica, 134
6.1, 2.6, 5.6, 1.4, Virginica, 135
7.7, 3, 6.1, 2.3, Virginica, 136
6.3, 3.4, 5.6, 2.4, Virginica, 137
6.4, 3.1, 5.5, 1.8, Virginica, 138
6, 3, 4.8, 1.8, Virginica, 139
6.9, 3.1, 5.4, 2.1, Virginica, 140
6.7, 3.1, 5.6, 2.4, Virginica, 141
6.9, 3.1, 5.1, 2.3, Virginica, 142
5.8, 2.7, 5.1, 1.9, Virginica, 143
6.8, 3.2, 5.9, 2.3, Virginica, 144
6.7, 3.3, 5.7, 2.5, Virginica, 145
6.7, 3, 5.2, 2.3, Virginica, 146
6.3, 2.5, 5, 1.9, Virginica, 147
6.5, 3, 5.2, 2, Virginica, 148
6.2, 3.4, 5.4, 2.3, Virginica, 149
5.9, 3, 5.1, 1.8, Virginica, 150
];
```