【发布时间】:2020-06-05 07:41:04
【问题描述】:
下午好,
我已经开发了这个 R 函数,它可以对桶中的数据进行哈希处理:
# The used packages
library("pacman")
pacman::p_load(dplyr, tidyr, devtools, MASS, pracma, mvtnorm, interval, intervals)
pacman::p_load(sprof, RDocumentation, helpRFunctions, foreach , philentropy , Rcpp , RcppAlgos)
hash<-function(v,p){
if(dot(v,p)>0) return(1) else (0) }
LSH_Band<-function(data,K ){
# We retrieve numerical columns of data
t<-list.df.var.types(data)
df.r<-as.matrix(data[c(t$numeric,t$Intervals)])
n=nrow(df.r)
# we create K*K matrice using normal law
rn=array(rnorm(K*K,0,1),c(K,K))
# we create K*K matrice of integers using uniform law , integrs are unique in each column
rd=unique.array(array(unique(ceiling(runif(K*K,0,ncol(df.r)))),c(K,K)))
buckets<-array(NA,c(K,n))
for (i in 1:K) {
for (j in 1:n) {
buckets[i,j]<-hash(df.r[j,][rd[,i]],rn[,i])
}
}
return(buckets)
}
> df.r
age height salaire.1 salaire.2
1 27 180 0 5000
2 26 178 0 5000
3 30 190 7000 10000
4 31 185 7000 10000
5 31 187 7000 10000
6 38 160 10000 15000
7 39 158 10000 15000
> LSH_Band(df.r, 3 )
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1 1 1 1 1 1 1
[2,] 1 1 0 0 0 0 0
[3,] 0 0 0 0 0 0 0
点函数是两个向量的标量积。
- 我的 Lsh 函数需要一行我的数据,然后它需要一部分
使用
df.r[j,][rd[,i]]获得的行。df.r[j,]是 j-éme 行的 数据。 rd[,i]: rd 是一个 K*K 矩阵,由 1 到 ncol(df.r) 之间的整数组成,矩阵的每一列只包含唯一的整数。rn[,i]: rn 是一个 K*K 矩阵,包含 N(0,1) 定律的值。在结果表中,观察以列表示。我将有 k 行。对于最后一行,我将计算
df.r[j,][rd[,K]]和rn[,K]之间的标量积。如果标量积为正,我将获得 1。rd[,K]和rn[,K]将仅用于结果表中的最后一行以及该行中的所有观察值。
我的问题:
是否用 lapply 函数 用变量 i 和 j 替换 循环 ?
我的真实数据会很大,这就是我问这个问题的原因。
谢谢!
【问题讨论】:
-
您的代码不可重现;请包含所有代码(对于函数
dot、list.df.var.type),以便我们可以根据您提供的示例数据重现您的预期输出。 -
@ Maurits Evers ,我添加了使用过的包。
-
dot和list.df.var.type是 pracma 和 helpRFunctions 包中的函数 -
有什么建议吗?对于高 n 值,for 循环会使我的代码变慢
标签: r