聚类分析的数据准备

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...