尺度的影响
您已经了解到,当某个变量的取值范围远大于数据中的其他变量时,它可能会不成比例地影响观测之间计算得到的距离。让我们用 trees 数据集的一个样本来实际看看这种情况。
您将使用 scale() 函数,它默认会对列特征进行中心化和标准化。
本练习中的变量包括:
- Girth —— 树干直径(英寸)
- Height —— 树高(英寸)
本练习是课程的一部分
R 中的聚类分析
练习说明
- 计算数据框
three_trees的距离矩阵,并将其保存为dist_trees。 - 创建新变量
scaled_three_trees,将three_trees数据进行中心化与标准化。 - 计算并打印
scaled_three_trees的距离矩阵,保存为dist_scaled_trees。 - 输出
dist_trees与dist_scaled_trees两个矩阵,观察两者中哪一些观测的最小距离发生了变化(提示:它们确实发生了变化)。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Calculate distance for three_trees
dist_trees <- ___
# Scale three trees & calculate the distance
scaled_three_trees <- ___
dist_scaled_trees <- ___
# Output the results of both Matrices
print('Without Scaling')
___
print('With Scaling')
___