叢集分析的資料前處理

Python 中的叢集分析

Shaumik Daityari

Business Analyst

為什麼叢集前要做資料準備?

  • 變數單位不可比(產品尺寸以 cm、價格以 $)
  • 相同單位的變數尺度與變異差很大(穀片與旅遊支出)
  • 原始資料會讓叢集結果產生偏誤
  • 叢集可能嚴重依賴單一變數
  • 解法:各變數正規化
Python 中的叢集分析

資料正規化

正規化:將資料重新縮放,使標準差為 1 的過程

x_new = x / std_dev(x)

from scipy.cluster.vq import whiten
data = [5, 1, 3, 3, 2, 3, 3, 8, 1, 2, 2, 3, 5]
scaled_data = whiten(data)
print(scaled_data)
[2.73, 0.55, 1.64, 1.64, 1.09, 1.64, 1.64, 4.36, 0.55, 1.09, 1.09, 1.64, 2.73]
Python 中的叢集分析

示例:資料正規化

# Import plotting library
from matplotlib import pyplot as plt

# Initialize original, scaled data
plt.plot(data, 
         label="original")
plt.plot(scaled_data, 
         label="scaled")
# Show legend and display plot
plt.legend()
plt.show()

Python 中的叢集分析

接下來:自己動手做練習

Python 中的叢集分析

Preparing Video For Download...