我正在寻找一种加速此算法的方法。加速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