尺度的影響
你已經學到,當某個變數的量級遠大於資料裡其他變數時,它可能會不成比例地影響觀測值之間計算出的距離。讓我們用 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')
___