はじめに
Google Colabを学びたいのStep7です。クラスター分析を学んでいきます!!
成果物
ソースコード
# ライブラリのインポート
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
# 寿司ネタのラベル
labels = ['Salmon', 'Tuna', 'Fatty Tuna', 'Eel', 'Shrimp', 'Octopus',
'Egg', 'Cucumber Roll', 'Squid', 'Mackerel', 'Horse Mackerel', 'Sea Urchin']
# 寿司ネタごとの特徴量(適当な仮データ)
# 特徴例:脂っこさ, 甘さ, 柔らかさ(0〜10のスケール)
data = np.array([
[8, 4, 7], # Salmon
[7, 3, 8], # Tuna
[9, 5, 9], # Fatty Tuna
[6, 6, 6], # Eel
[4, 7, 5], # Shrimp
[3, 2, 6], # Octopus
[2, 8, 3], # Egg
[1, 3, 2], # Cucumber Roll
[3, 4, 4], # Squid
[6, 2, 5], # Mackerel
[5, 3, 4], # Horse Mackerel
[7, 9, 6], # Sea Urchin
])
# 階層クラスタリング(Ward法)
linked = linkage(data, method='ward')
# デンドログラムを描画
plt.figure(figsize=(12, 6))
dendrogram(linked,
labels=labels,
orientation='top',
distance_sort='descending',
show_leaf_counts=True)
plt.title("Sushi Ingredient Hierarchical Clustering")
plt.xlabel("Sushi Types")
plt.ylabel("Distance")
plt.grid(True)
plt.show()