【发布时间】:2016-03-20 19:02:12
【问题描述】:
我在具有 16 GB RAM 的机器上运行 R 3.2.3。我有一个 3,00,000 行 x 12 列的大矩阵。我想在 R 中使用层次聚类算法,所以在我这样做之前,我正在尝试创建一个距离矩阵。由于数据是混合类型的,我对不同的类型使用不同的矩阵。我收到有关内存分配的错误:
df <- as.data.frame(matrix(rnorm(36*10^5), nrow = 3*10^5))
d1=as.dist(distm(df[,c(1:2)])/10^5)
d2=dist(df[,c(3:8)], method = "euclidean")
d3= hamming.distance(df[,c(9:12)]%>%as.matrix(.))%>%as.dist(.)
我收到以下错误
> d1=as.dist(distm(df1[,c(1:2)])/10^5)
Error: cannot allocate vector of size 670.6 Gb
In addition: Warning messages:
1: In matrix(0, ncol = n, nrow = n) :
Reached total allocation of 16070Mb: see help(memory.size)
2: In matrix(0, ncol = n, nrow = n) :
Reached total allocation of 16070Mb: see help(memory.size)
3: In matrix(0, ncol = n, nrow = n) :
Reached total allocation of 16070Mb: see help(memory.size)
4: In matrix(0, ncol = n, nrow = n) :
Reached total allocation of 16070Mb: see help(memory.size)
> d2=dist(df1[,c(3:8)], method = "euclidean")
Error: cannot allocate vector of size 335.3 Gb
In addition: Warning messages:
1: In dist(df1[, c(3:8)], method = "euclidean") :
Reached total allocation of 16070Mb: see help(memory.size)
2: In dist(df1[, c(3:8)], method = "euclidean") :
Reached total allocation of 16070Mb: see help(memory.size)
3: In dist(df1[, c(3:8)], method = "euclidean") :
Reached total allocation of 16070Mb: see help(memory.size)
4: In dist(df1[, c(3:8)], method = "euclidean") :
Reached total allocation of 16070Mb: see help(memory.size)
> d3= hamming.distance(df1[,c(9:12)]%>%as.matrix(.))%>%as.dist(.)
Error: cannot allocate vector of size 670.6 Gb
In addition: Warning messages:
1: In matrix(0, nrow = nrow(x), ncol = nrow(x)) :
Reached total allocation of 16070Mb: see help(memory.size)
2: In matrix(0, nrow = nrow(x), ncol = nrow(x)) :
Reached total allocation of 16070Mb: see help(memory.size)
3: In matrix(0, nrow = nrow(x), ncol = nrow(x)) :
Reached total allocation of 16070Mb: see help(memory.size)
4: In matrix(0, nrow = nrow(x), ncol = nrow(x)) :
Reached total allocation of 16070Mb: see help(memory.size)
【问题讨论】:
-
您不需要一起处理所有数据,这将消耗您的所有内存并出错。考虑逐批处理它们,例如每次 10000 个向量。
-
但是在聚类中,我们需要计算一行到所有其他行的距离。那么批量计算在这里有什么帮助呢?
-
是的,但您可以进行最终缩减以选择最小/最大值。这有意义吗?高效计算距离可以参考here。
-
通过选择最小/最大减少??对不起,我不明白。更详细的见解可能会有所帮助。
-
更新一个答案,因为我需要多写几个字,你清楚吗?谢谢。
标签: r matrix distance hierarchical-clustering