[英]Is there any sparse support for dist function in R?
有沒有人聽說過任何與 R 中的dist{stats}
函數相同的包或功能,它創建了
距離矩陣,通過使用指定的距離度量來計算數據矩陣的行之間的距離,
但是將一個散列矩陣作為輸入?
我的 data.frame(名為dataCluster
)有dataCluster
:7000 X 10000 並且幾乎是 99% 稀疏。 在不稀疏的常規形式中,此功能似乎不會停止工作......
h1 <- hclust( dist( dataCluster ) , method = "complete" )
沒有答案的類似問題: Sparse Matrix as input to Hierarchical clustering in R
它接受來自Matrix
包的稀疏矩陣(文檔中不清楚),還可以進行交叉距離,輸出Matrix
和dist
對象等等。
但是,默認的距離度量是'cosine'
,因此如果需要,請務必指定method = 'euclidean'
。
**更新:**實際上你可以很容易地做 qlcMatrix 所做的事情:
sparse.cos <- function(x, y = NULL, drop = TRUE){
if(!is.null(y)){
if(class(x) != "dgCMatrix" || class(y) != "dgCMatrix") stop ("class(x) or class(y) != dgCMatrix")
if(drop == TRUE) colnames(x) <- rownames(x) <- colnames(y) <- rownames(y) <- NULL
crossprod(
tcrossprod(
x,
Diagonal(x = as.vector(crossprod(x ^ 2, rep(1, x@Dim[1]))) ^ -0.5)
),
tcrossprod(
y,
Diagonal(x = as.vector(crossprod(y ^ 2, rep(1, x@Dim[1]))) ^ -0.5))
)
)
} else {
if(class(x) != "dgCMatrix") stop ("class(x) != dgCMatrix")
if(drop == TRUE) colnames(x) <- rownames(X) <- NULL
crossprod(
tcrossprod(
x,
Diagonal(x = as.vector(crossprod(x ^ 2, rep(1, nrow(x)))) ^ -0.5))
)
}
}
我發現上述和qlcMatrix::cosSparse
之間的性能沒有顯着差異。
qlcMatrix::cosSparse
比wordspace::dist.matrix
更快,當數據 > 50% 稀疏或在輸入矩陣的最長邊(即高格式)上計算相似性時。
wordspace::dist.matrix
與qlcMatrix::cosSparse
在不同稀疏度(10%、50%、90% 或 99% 稀疏)的寬矩陣 (1000 x 5000) 上的性能,以計算 1000 x 1000 相似度:
# M1 is 10% sparse, M99 is 99% sparse
set.seed(123)
M10 <- rsparsematrix(5000, 1000, density = 1)
M50 <- rsparsematrix(5000, 1000, density = 0.5)
M90 <- rsparsematrix(5000, 1000, density = 0.1)
M99 <- rsparsematrix(5000, 1000, density = 0.01)
tM10 <- t(M10)
tM50 <- t(M50)
tM90 <- t(M90)
tM99 <- t(M99)
benchmark(
"cosSparse: 10% sparse" = cosSparse(M10),
"cosSparse: 50% sparse" = cosSparse(M50),
"cosSparse: 90% sparse" = cosSparse(M90),
"cosSparse: 99% sparse" = cosSparse(M99),
"wordspace: 10% sparse" = dist.matrix(tM10, byrow = TRUE),
"wordspace: 50% sparse" = dist.matrix(tM50, byrow = TRUE),
"wordspace: 90% sparse" = dist.matrix(tM90, byrow = TRUE),
"wordspace: 99% sparse" = dist.matrix(tM99, byrow = TRUE),
replications = 2, columns = c("test", "elapsed", "relative"))
這兩個函數具有相當的可比性,wordspace 在較低稀疏度時略微領先,但在高度稀疏度時絕對不是:
test elapsed relative
1 cosSparse: 10% sparse 15.83 527.667
2 cosSparse: 50% sparse 4.72 157.333
3 cosSparse: 90% sparse 0.31 10.333
4 cosSparse: 99% sparse 0.03 1.000
5 wordspace: 10% sparse 15.23 507.667
6 wordspace: 50% sparse 4.28 142.667
7 wordspace: 90% sparse 0.36 12.000
8 wordspace: 99% sparse 0.09 3.000
如果我們翻轉計算以計算 5000 x 5000 矩陣,則:
benchmark(
"cosSparse: 50% sparse" = cosSparse(tM50),
"cosSparse: 90% sparse" = cosSparse(tM90),
"cosSparse: 99% sparse" = cosSparse(tM99),
"wordspace: 50% sparse" = dist.matrix(M50, byrow = TRUE),
"wordspace: 90% sparse" = dist.matrix(M90, byrow = TRUE),
"wordspace: 99% sparse" = dist.matrix(M99, byrow = TRUE),
replications = 1, columns = c("test", "elapsed", "relative"))
現在 cosSparse 的競爭優勢變得非常明顯:
test elapsed relative
1 cosSparse: 50% sparse 10.58 151.143
2 cosSparse: 90% sparse 1.44 20.571
3 cosSparse: 99% sparse 0.07 1.000
4 wordspace: 50% sparse 11.41 163.000
5 wordspace: 90% sparse 2.39 34.143
6 wordspace: 99% sparse 0.64 9.143
效率的變化在 50% 稀疏度下不是很顯着,但是在 90% 稀疏度下,詞空間慢 1.6 倍,而在 99% 稀疏度下,它慢了近 10 倍!
將此性能與方陣進行比較:
M50.square <- rsparsematrix(1000, 1000, density = 0.5)
tM50.square <- t(M50.square)
M90.square <- rsparsematrix(1000, 1000, density = 0.1)
tM90.square <- t(M90.square)
benchmark(
"cosSparse: square, 50% sparse" = cosSparse(M50.square),
"wordspace: square, 50% sparse" = dist.matrix(tM50.square, byrow = TRUE),
"cosSparse: square, 90% sparse" = cosSparse(M90.square),
"wordspace: square, 90% sparse" = dist.matrix(tM90.square, byrow = TRUE),
replications = 5, columns = c("test", "elapsed", "relative"))
cosSparse 在稀疏度為 50% 時略快,在稀疏度為 90% 時幾乎快兩倍!
test elapsed relative
1 cosSparse: square, 50% sparse 2.12 9.217
3 cosSparse: square, 90% sparse 0.23 1.000
2 wordspace: square, 50% sparse 2.15 9.348
4 wordspace: square, 90% sparse 0.40 1.739
注意, wordspace::dist.matrix
具有多個邊緣的情況下的檢查比qlcMatrix::cosSparse
和通過還允許並行openmp
在R.另外, wordspace::dist.matrix
支撐歐幾里德和傑卡德距離度量,雖然這些是遠慢。 該軟件包中還內置了許多其他方便的功能。
也就是說,如果您只需要余弦相似度,並且您的矩陣 > 50% 稀疏,並且您正在計算高大的方式, cosSparse
應該是首選工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.