2017-09-20 57 views
0

的離羣值數據集的異常值可以通過單連接方法輕鬆識別。現在我想自動刪除異常值。我的想法是刪除超過指定距離值的數據。這裏是我的代碼以mtcars的示例數據:在聚類分析中自動刪除計算的凝聚層次聚類數據

library(cluster) 
library(dendextend) 
cluster<-agnes(mtcars,stand=FALSE,method="single") 
dend = as.dendrogram(cluster) 

Plot你可以看到生成的樹狀圖。最後4輛車(「Duster 360」,「Camaro Z28」,「Ford Pantera L」,「Maserati Bora」)被識別爲異常值,因此我想刪除它們的孔行(數據集mtcars)。我怎樣才能自動做到這一點?例如。刪除高度超過70的行?我嘗試了很多可能性來刪除異常值,但它們似乎不適用於我的數據。

非常感謝!

+2

我不認爲它是如此簡單,你建議。你說:「最後4輛車...被識別爲異常值」。我會告訴你,瑪莎拉蒂是一個異常值,但你爲什麼說其他三個是異常值,而不是3個小團? – G5W

+0

也許異常是錯誤的詞。我想刪除距離高於70的數據。 – Jules

回答

0

試試這個:

# your code 
library(cluster) 
cluster<-agnes(mtcars,stand=FALSE,method="single") 
dend = as.dendrogram(cluster) 
plot(dend) 

#new code  
hclu <- as.hclust(cluster) # convert to list that cutree() understands 
groupindexes <- cutree(hclu, h = 70) # cut at height 70 - creates 3 groups/branches 
mtcars[groupindexes != 1,] # "outliers" - not in group 1 but in groups 2 and 3 
mtcars[groupindexes == 1,] # all but the 4 "outliers" 

結果1 - 的 「異常值」:

   mpg cyl disp hp drat wt qsec vs am gear carb 
Duster 360  14.3 8 360 245 3.21 3.57 15.84 0 0 3 4 
Camaro Z28  13.3 8 350 245 3.73 3.84 15.41 0 0 3 4 
Ford Pantera L 15.8 8 351 264 4.22 3.17 14.50 0 1 5 4 
Maserati Bora 15.0 8 301 335 3.54 3.57 14.60 0 1 5 8 

結果2:

     mpg cyl disp hp drat wt qsec vs am gear carb 
Mazda RX4   21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 
Mazda RX4 Wag  21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 
Datsun 710   22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 
(....and ~30 other rows ....) 
+0

非常感謝! – Jules

0

如果您的「規則」是鏈接距離,那麼您基本上重新創建了最近鄰居異常值檢測,這是數據挖掘中較老的異常值方法之一。

Ramaswamy,Sridhar,Rajeev Rastogi和Kyuseok Shim。 「用於從大型數據集中挖掘異常值的高效算法。」 ACM Sigmod記錄。卷。 29.No.ACM,2000.

除了與AGNES的單鏈路需要O(n 3)時間,但索引可以在O(n log n)中做kNN異常值。