我正在尋找一種加速此算法的方法。加速R算法來計算Hellinger距離的距離矩陣
我的情況如下。我有一個包含6個習慣的25,000個用戶的數據集。我的目標是爲25,000個用戶開發一個分層聚類。我在一個有16個內核,128GB RAM的服務器上運行它。 我花了3周時間才爲在我的服務器上使用6個內核的10,000個用戶計算這個距離矩陣。你可以想象這對我的研究來說太長了。
對於6種習慣中的每一種,我都創建了概率質量分佈(PMF)。每個哈比特人的PMF可能大小(列)不同。一些習慣有10列大約256,全部取決於最不友好行爲的用戶。
我的算法的第一步是開發一個距離矩陣。我使用Hellinger距離來計算距離,這與使用的一些包相反。 cathersian /曼哈頓。我確實需要Hellinger距離,請參閱https://en.wikipedia.org/wiki/Hellinger_distance
我目前嘗試的是通過應用多核處理器加速算法,每個核心都有6種習慣。兩件事情,可能是加快
(1)C實現有益的 - 但我不知道如何做到這一點(我不是一個C程序員),你能幫助我在此C實現,如果這將是有益的? (2)通過自己加入桌子製作一個carthesian產品,並讓所有的行和所有的行進行一次行計算。 R點在例如默認情況下給出了一個錯誤。 data.table。對此有何建議?
還有其他想法嗎?
此致Jurjen
# example for 1 habit with 100 users and a PMF of 5 columns
Habit1<-data.frame(col1=abs(rnorm(100)),
col2=abs(c(rnorm(20),runif(50),rep(0.4,20),sample(seq(0.01,0.99,by=0.01),10))),
col3=abs(c(rnorm(30),runif(30),rep(0.4,10),sample(seq(0.01,0.99,by=0.01),30))),
col4=abs(c(rnorm(10),runif(10),rep(0.4,20),sample(seq(0.01,0.99,by=0.01),60))),
col5=abs(c(rnorm(50),runif(10),rep(0.4,10),sample(seq(0.01,0.99,by=0.01),30))))
# give all users a username same as rowname
rownames(Habit1)<- c(1:100)
# actual calculation
Result<-calculatedistances(Habit1)
HellingerDistance <-function(x){
#takes two equal sized vectors and calculates the hellinger distance between the vectors
# hellinger distance function
return(sqrt(sum(((sqrt(x[1,]) - sqrt(x[2,]))^2)))/sqrt(2))
}
calculatedistances <- function(x){
# takes a dataframe of user IID in the first column and a set of N values per user thereafter
# first set all NA to 0
x[is.na(x)] <- 0
#create matrix of 2 subsets based on rownumber
# 1 first the diagronal with
D<-cbind(matrix(rep(1:nrow(x),each=2),nrow=2),combn(1:nrow(x), 2))
# create a dataframe with hellinger distances
B <<-data.frame(first=rownames(x)[D[1,]],
second=rownames(x)[D[2,]],
distance=apply(D, 2, function(y) HellingerDistance(x[ y,]))
)
# reshape dataframe into a matrix with users on x and y axis
B<<-reshape(B, direction="wide", idvar="second", timevar="first")
# convert wide table to distance table object
d <<- as.dist(B[,-1], diag = FALSE)
attr(d, "Labels") <- B[, 1]
return(d)
}
我建議(1)改變你的矩陣爲'long'格式,(2)使用'data.table'來計算觀察對之間的數據,(3)將結果轉換回'寬'格式的矩陣如有必要。 [這是迄今爲止我發現的使用這種方法計算數據點之間距離的最有效方法](https://stackoverflow.com/questions/36817423/how-to-efficiently-calculate-distance-between-pair- of-coordinates-using-data-tab) –
感謝您的回答,我不完全瞭解您的解決方案,也不是鏈接中的示例。該鏈接顯示空間距離而不是海林格距離的解決方案。 1.數據的長格式就像它在習慣中那樣,你的意思是? 2.如何最好地實現'data.table'來計算觀察對之間的數據? 感謝您的回答 –
R.有一個'hellinger'函數您是否考慮過使用它? – akash87