適切なクラスター数(エルボー法)
クラスタリングにおける適切なクラスター数をエルボー法で推定する。
x軸がクラスタ数、y軸がコスト(二乗誤差など)の折線グラフを描く。
クラスタ数(x)を増加 → コスト(y)が減少 の傾きを確認する。
傾きが急激に緩やかになる点(=エルボーの位置)のクラスタ数が効率が良く、適切なクラスター数と考える。

例)Pythonでの上記グラフ描写のサンプルプログラム
# ライブラリのインポート
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
# データセットの作成
iris = load_iris()
X = iris.data
Y = iris.target
# エルボー法の実施
distortions = []
# クラスター数1~10を一気に計算
for i in range(1, 11):
km = KMeans(n_clusters=i)
# クラスタリングの実行
km.fit(X)
# 各々のクラスタでの誤差を算出し、リストに格納
distortions.append(km.inertia_)
# グラフのプロット
plt.plot(range(1, 11), distortions, marker="o")
plt.xticks(np.arange(1, 11, 1))
plt.xlabel("Number_of_clusters")
plt.ylabel("Distortion")
plt.show()