視覺化叢集

Python 中的叢集分析

Shaumik Daityari

Business Analyst

為何要視覺化叢集?

  • 了解形成的叢集意義
  • 作為叢集驗證的額外步驟
  • 發現資料中的趨勢
Python 中的叢集分析

seaborn 簡介

  • seaborn:以 matplotlib 為基礎的 Python 資料視覺化函式庫
  • 美觀且易於調整,比 matplotlib 更好上手!
  • 提供方便的函式,讓資料分析情境下的視覺化更容易
  • 叢集用途:圖表的 hue 參數
Python 中的叢集分析

用 matplotlib 視覺化叢集

from matplotlib import pyplot as plt
df = pd.DataFrame({'x': [2, 3, 5, 6, 2],
                   'y': [1, 1, 5, 5, 2],
                   'labels': ['A', 'A', 'B', 'B', 'A']})

colors = {'A':'red', 'B':'blue'}
df.plot.scatter(x='x', y='y', c=df['labels'].apply(lambda x: colors[x])) plt.show()
Python 中的叢集分析

用 seaborn 視覺化叢集

from matplotlib import pyplot as plt
import seaborn as sns
df = pd.DataFrame({'x': [2, 3, 5, 6, 2],
                   'y': [1, 1, 5, 5, 2],
                   'labels': ['A', 'A', 'B', 'B', 'A']})

sns.scatterplot(x='x', y='y', hue='labels', data=df) plt.show()
Python 中的叢集分析

兩種視覺化方式的比較

matplotlib 圖

seaborn 圖

Python 中的叢集分析

下一步:動手做幾個視覺化

Python 中的叢集分析

Preparing Video For Download...