【问题标题】:parRapply whats wrong?parRapply 怎么了?
【发布时间】:2015-05-21 21:05:52
【问题描述】:

我有一个电子邮件列表,我想使用最长公共子字符串比较行之间的模式(相似性)来比较它们。

data 是一个包含 300.000 封电子邮件的数据框:

         V1
1   "01003@163.com"
2   "cloud@coldmail.com"
3   "den_smukk_kiilar@hotmail.com"
4   "Esteban.verduzco@gmail.com"
5   "freiheitmensch@gmail.com"
6   "mitsoanastos@yahoo.com"
7   "ahmedsir744@yahoo.com" 
8   ...

我正在使用此代码:

# Compare Strings Difference
compare_strings = function(j,i) {
  value = as.numeric(stringdist(data[j,],data[i,],method='lcs', nthread = 2))   
  pair <- rbind(data[j,], data[i,],value)
  return(pair)
}

i = 0 
kk = 1 

while(kk<nrow(data)) {

  i = i+1 # fix row
  j = c((kk+1):nrow(data)) # rows to be compared 

  # Apply Function "compare_strings" for row "i" with all the others rows  
  out <- as.matrix(t(apply(expand.grid(i,j),1, function(x,y)  compare_strings(x[1],x[2]))))

  kk = kk +1
}

效果很好!但是我有 300.000 封电子邮件,我正在尝试将购买过程并行化:

require(parallel)

 clus <- makeCluster(2)

 clusterEvalQ(clus,  compare.strings <- function(j,i) {
    library(stringdist)
    value = as.numeric(stringdist(data[j,],data[i,],method='lcs', nthread = 2))
    pair <- rbind(data[j,], data[i,],value)
    return(pair)
  })

  out = as.matriz(t(parRapply(clus, expand.grid(i,j),function(x,y) compare.strings(x[1],x[2]))))

但我明白了:

Error in checkForRemoteErrors(val) : 
  2 nodes produced errors; first error: object of type 'closure' is not subsettable

我做错了什么?有没有更好的方法来比较字符串的数量?

【问题讨论】:

    标签: r parallel-processing apply


    【解决方案1】:

    我们需要使用“clusterExport”导出数据。

    #Create cluster
    
    clus <- makeCluster(8)
    
    clusterExport(clus, list("data","stringdist") , envir=environment())
    
    clusterEvalQ(clus,
                 compare_strings <- function(j,i) {
                   #library(stringdist)
                   #print(i)
                   value = as.numeric(stringdist(data[j,],data[i,],method='lcs', nthread = 8))
                   pair <- rbind(data[j,],data[i,],value)
                   return(pair) 
                 })
    

    并定义i和j的范围,我们可以运行:

    #Apply the declared function  
    
    out = matrix(unlist(parRapply(clus,expand.grid(i,j), function(x,y) compare_strings(x[1],x[2]))),ncol=3, byrow = T)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2015-06-29
      • 2012-09-05
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多