| Title: | Computes Proximity in Large Matrices |
|---|---|
| Description: | Computes proximity between rows or columns of large matrices efficiently in C++. Functions are optimised for large matrices using the Armadillo and Intel TBB libraries. Among various built-in similarity/distance measures, computation of correlation, cosine similarity, Dice coefficient and Euclidean distance is particularly fast. |
| Authors: | Kohei Watanabe [cre, aut, cph] (ORCID: <https://orcid.org/0000-0001-6519-5265>), Robrecht Cannoodt [aut] (ORCID: <https://orcid.org/0000-0003-3641-729X>) |
| Maintainer: | Kohei Watanabe <[email protected]> |
| License: | GPL-3 |
| Version: | 0.6.0 |
| Built: | 2026-10-30 08:56:06 UTC |
| Source: | https://github.com/cran/proxyC |
Produce the same result as apply(x, 2, max), apply(x, 1, max),
apply(x, 2, min) and apply(x, 1, min), respectively, but ignore NA.
colMaxs(x, use_nan = FALSE) rowMaxs(x, use_nan = FALSE) colMins(x, use_nan = FALSE) rowMins(x, use_nan = FALSE)colMaxs(x, use_nan = FALSE) rowMaxs(x, use_nan = FALSE) colMins(x, use_nan = FALSE) rowMins(x, use_nan = FALSE)
x |
a base::matrix or Matrix::Matrix object. |
use_nan |
if |
a numeric vector
mt <- Matrix::rsparsematrix(100, 100, 0.9) colMaxs(mt) apply(mt, 2, max) # the samemt <- Matrix::rsparsematrix(100, 100, 0.9) colMaxs(mt) apply(mt, 2, max) # the same
Produce the same result as apply(x, 2, sd) and apply(x, 1, sd), respectively.
without coercing matrix to dense matrix. Values are not identical to
sd() because of the floating point precision issue in C++.
colSds(x, use_nan = FALSE) rowSds(x, use_nan = FALSE)colSds(x, use_nan = FALSE) rowSds(x, use_nan = FALSE)
x |
a base::matrix or Matrix::Matrix object. |
use_nan |
if |
a numeric vector
mt <- Matrix::rsparsematrix(100, 100, 0.9) colSds(mt) apply(mt, 2, sd) # the samemt <- Matrix::rsparsematrix(100, 100, 0.9) colSds(mt) apply(mt, 2, sd) # the same
Produce the same result as applying sum(x == 0) to each row or column.
colZeros(x) rowZeros(x)colZeros(x) rowZeros(x)
x |
a base::matrix or Matrix::Matrix object. |
an integer vector
mt <- Matrix::rsparsematrix(100, 100, 0.9) colZeros(mt) apply(mt, 2, function(x) sum(x == 0)) # the samemt <- Matrix::rsparsematrix(100, 100, 0.9) colZeros(mt) apply(mt, 2, function(x) sum(x == 0)) # the same
Compute the (transposed) cross-product of large sparse matrices using the same
infrastructure as simil() and dist().
crossprod(x, y = NULL, min_prod = NULL, sparse = TRUE, digits = 14) tcrossprod(x, y = NULL, min_prod = NULL, sparse = TRUE, digits = 14)crossprod(x, y = NULL, min_prod = NULL, sparse = TRUE, digits = 14) tcrossprod(x, y = NULL, min_prod = NULL, sparse = TRUE, digits = 14)
x |
a base::matrix or Matrix::Matrix object. Only sparse matrices are covered to the Matrix::CsparseMatrix internally. |
y |
if a base::matrix or Matrix::Matrix object is provided, proximity
between documents or features in |
min_prod |
the minimum product to be recorded. |
sparse |
if |
digits |
determines rounding of small values towards zero. Use primarily to correct floating point errors. Rounding is performed in C++ in a similar way as base::zapsmall. |
a Matrix::Matrix object
Create a pattern matrix for simil() or dist() to enable masked similarity computation.
If the matrix is passed to the function, it computes similarity scores only for cells with TRUE.
mask(x, y = NULL)mask(x, y = NULL)
x |
a numeric or character vector matched against each other. |
y |
a numeric or character vector matched against |
a sparse logical matrix with TRUE for matched pairs.
mt1 <- Matrix::rsparsematrix(100, 6, 1.0) colnames(mt1) <- c("a", "a", "d", "d", "e", "e") mt2 <- Matrix::rsparsematrix(100, 5, 1.0) colnames(mt2) <- c("a", "b", "c", "d", "e") (msk <- mask(colnames(mt1), colnames(mt2))) simil(mt1, mt2, margin = 2, mask = msk, drop0 = TRUE)mt1 <- Matrix::rsparsematrix(100, 6, 1.0) colnames(mt1) <- c("a", "a", "d", "d", "e", "e") mt2 <- Matrix::rsparsematrix(100, 5, 1.0) colnames(mt2) <- c("a", "b", "c", "d", "e") (msk <- mask(colnames(mt1), colnames(mt2))) simil(mt1, mt2, margin = 2, mask = msk, drop0 = TRUE)
Find column or row indices of maximum values in matrices. Works in a
similar way as base::max.col() but without conversion of sparse matrices
to dense matrices.
maxCol(x, ties_method = c("random", "first", "last"), use_nan = FALSE) maxRow(x, ties_method = c("random", "first", "last"), use_nan = FALSE)maxCol(x, ties_method = c("random", "first", "last"), use_nan = FALSE) maxRow(x, ties_method = c("random", "first", "last"), use_nan = FALSE)
x |
a base::matrix or Matrix::Matrix object. |
ties_method |
specify how to handle ties. If "random", choose an index randomly within the ties; if "first" or "last" returns the index of the first or the last tie elements, respectively. |
use_nan |
if |
an integer vector
mt <- Matrix::rsparsematrix(100, 100, 0.9) maxCol(mt, ties_method = "first") max.col(mt, ties.method = "first") # the samemt <- Matrix::rsparsematrix(100, 100, 0.9) maxCol(mt, ties_method = "first") max.col(mt, ties.method = "first") # the same
Fast similarity/distance computation function for large matrices using multiple
processors. To save computation time and storage, it can also apply arbitrary
threshold (min_simil) or rank (rank) to the similarity/distance scores and
return them in a sparse matrix.
simil( x, y = NULL, margin = 1, method = c("cosine", "correlation", "dice", "edice", "jaccard", "ejaccard", "fjaccard", "hamann", "faith", "simple matching"), mask = NULL, min_simil = NULL, rank = NULL, drop0 = FALSE, diag = FALSE, use_nan = NULL, sparse = TRUE, digits = 14 ) dist( x, y = NULL, margin = 1, method = c("euclidean", "chisquared", "kullback", "jeffreys", "jensen", "manhattan", "maximum", "canberra", "minkowski", "hamming"), mask = NULL, p = 2, smooth = 0, drop0 = FALSE, diag = FALSE, use_nan = NULL, sparse = TRUE, digits = 14 )simil( x, y = NULL, margin = 1, method = c("cosine", "correlation", "dice", "edice", "jaccard", "ejaccard", "fjaccard", "hamann", "faith", "simple matching"), mask = NULL, min_simil = NULL, rank = NULL, drop0 = FALSE, diag = FALSE, use_nan = NULL, sparse = TRUE, digits = 14 ) dist( x, y = NULL, margin = 1, method = c("euclidean", "chisquared", "kullback", "jeffreys", "jensen", "manhattan", "maximum", "canberra", "minkowski", "hamming"), mask = NULL, p = 2, smooth = 0, drop0 = FALSE, diag = FALSE, use_nan = NULL, sparse = TRUE, digits = 14 )
x |
a base::matrix or Matrix::Matrix object. Only sparse matrices are covered to the Matrix::CsparseMatrix internally. |
y |
if a base::matrix or Matrix::Matrix object is provided, proximity
between documents or features in |
margin |
integer indicating margin of similarity/distance computation. 1 indicates rows or 2 indicates columns. |
method |
method to compute similarity or distance |
mask |
a pattern matrix created using |
min_simil |
the minimum similarity value to be recorded. |
rank |
an integer value specifying top-n most similarity values to be recorded. |
drop0 |
if |
diag |
if |
use_nan |
if |
sparse |
if |
digits |
determines rounding of small values towards zero. Use primarily to correct floating point errors. Rounding is performed in C++ in a similar way as base::zapsmall. |
p |
weight for Minkowski distance. |
smooth |
adds a fixed value to all the cells to avoid division by zero.
Only used when |
Similarity:
cosine: cosine similarity
correlation: Pearson's correlation
jaccard: Jaccard coefficient
ejaccard: the real value version of jaccard
fjaccard: Fuzzy Jaccard coefficient
dice: Dice coefficient
edice: the real value version of dice
hamann: Hamann similarity
faith: Faith similarity
simple matching: the percentage of common elements
Distance:
euclidean: Euclidean distance
chisquared: chi-squared distance
kullback: Kullback–Leibler divergence
jeffreys: Jeffreys divergence
jensen: Jensen–Shannon divergence
manhattan: Manhattan distance
maximum: the largest difference between values
canberra: Canberra distance
minkowski: Minkowski distance
hamming: Hamming distance
See the vignette for how the similarity and distance are computed:
vignette("measures", package = "proxyC")
It performs parallel computing using Intel oneAPI Threads Building Blocks.
The number of threads for parallel computing should be specified via
options(proxyC.threads) before calling the functions. If the value is -1,
all the available threads will be used. Unless the option is used, the
number of threads will be limited by the environmental variables
(OMP_THREAD_LIMIT or RCPP_PARALLEL_NUM_THREADS) to comply with CRAN
policy and offer backward compatibility.
a Matrix::Matrix object
zapsmall
mt <- Matrix::rsparsematrix(100, 100, 0.01) simil(mt, method = "cosine")[1:5, 1:5] mt <- Matrix::rsparsematrix(100, 100, 0.01) dist(mt, method = "euclidean")[1:5, 1:5]mt <- Matrix::rsparsematrix(100, 100, 0.01) simil(mt, method = "cosine")[1:5, 1:5] mt <- Matrix::rsparsematrix(100, 100, 0.01) dist(mt, method = "euclidean")[1:5, 1:5]