简体   繁体   English

Sparklyr:如何计算2个Spark表之间的相关系数?

[英]Sparklyr: how to calculate correlation coefficient between 2 Spark tables?

I have these 2 Spark tables: 我有这两个Spark表:

simx
x0: num 1.00 2.00 3.00 ...
x1: num 2.00 3.00 4.00 ...
...
x788: num 2.00 3.00 4.00 ...

and

simy
y0: num 1.00 2.00 3.00 ...

In both tables, each column has the same number of values. 在两个表中,每列具有相同数量的值。 Both table x and y are saved into handle simX_tbl and simY_tbl respectively. xy都分别保存在句柄simX_tblsimY_tbl The actual data size is quite big and may reach 40GB. 实际数据量非常大,可能达到40GB。

I want to calculate the correlation coefficient of each column in simx with simy (let's say like cor(x0, y0, 'pearson') ). 我想用simy计算simx中每列的相关系数(让我们说像cor(x0, y0, 'pearson') )。

I searched everywhere and I don't think there's any ready-to-use cor function, so I'm thinking about using the correlation formula itself (just like mentioned in here ). 我到处搜索,我认为没有任何现成的cor函数,所以我正在考虑使用相关公式本身(就像这里提到的那样 )。

Based on a good explanation in my previous question , I think using mutate_all or mutate_each is not very efficient and gives a C stack error for a bigger data size, so I consider to use invoke instead to call functions from Spark directly. 基于在上一个问题中的一个很好的解释,我认为使用mutate_allmutate_each不是非常有效,并且为更大的数据大小提供了C stack error ,因此我考虑使用invoke来直接从Spark调用函数。

So far I managed to get until here: 到目前为止,我设法到达这里:

exprs <- as.list(paste0("sum(", colnames(simX_tbl),")"))

corr_result <- simX_tbl%>%  
  spark_dataframe() %>% 
  invoke("selectExpr", exprs) %>% 
  invoke("toDF", as.list(colnames(simX_tbl))) %>% 
  sdf_register("corr_result")

to calculate the sum of each column in simx . 以计算sum中的每一列的simx But then, I realize that I also need to calculate the simy table and I don't know how to interact the two tables together (like, accessing simy while manipulating simx ). 但后来,我才知道我还需要计算simy表,我不知道如何将两个表互动起来(比如,访问simy在操纵simx )。

Is there any way to calculate the correlation in a better way? 有没有办法以更好的方式计算相关性? Or maybe just how to interact with other Spark table. 或者也许只是如何与其他Spark表进行交互。

My Spark version is 1.6.0 我的Spark版本是1.6.0

EDIT: I tried to use combine function from dplyr : 编辑:我试图使用dplyr combine功能:

xy_df <- simX_tbl %>% 
  as.data.frame %>%
  combine(as.data.frame(simY_tbl)) %>%
  # convert both table to dataframe, then combine. 
  # It will become list, so need to convert to dataframe again
  as.data.frame 

xydata <- copy_to(sc, xy_df, "xydata") #copy the dataframe into Spark table

But I'm not sure if this is a good solution because: 但我不确定这是否是一个很好的解决方案,因为:

  1. Need to load into dataframe inside of R, which I consider non-practical for big size data 需要加载到R内部的数据帧中,我认为这对于大尺寸数据是不实用的
  2. When trying to head the handle xydata , the column name becomes a concat of all values 当试图head手柄xydata ,列名称将成为所有值的CONCAT

     xydata %>% head Source: query [6 x 790] Database: spark connection master=yarn-client app=sparklyr local=FALSE 

    c_1_67027262134984_2_44919662134984_1_85728542134984_1_49317262134984_ c_1_67027262134984_2_44919662134984_1_85728542134984_1_49317262134984_
    1 1.670273 1 1.670273
    2 2.449197 2 2.449197
    3 1.857285 3 1.857285
    4 1.493173 4 1.493173
    5 1.576857 5 1.576857
    6 -5.672155 6 -5.672155

Personally I would solve it by going back to the input dataset . 我个人会通过返回输入数据集来解决它。 Just for the record the input data has been loaded using CSV reader: 只是为了记录输入数据已使用CSV阅读器加载:

df <- spark_read_csv(
  sc, path = path, name = "simData", delimiter = " ", 
  header = "false", infer_schema = "false"
) %>% rename(y = `_c0`, xs = `_c1`)

and looks more or less like this: 看起来或多或少像这样:

      y                                                   xs
  <chr>                                                <chr>
1 21.66     2.643227,1.2698358,2.6338573,1.8812188,3.8708665
2 35.15 3.422151,-0.59515584,2.4994135,-0.19701914,4.0771823
3 15.22  2.8302398,1.9080592,-0.68780196,3.1878228,4.6600842

Now instead of splitting data into mutlitple tables let's process both part together: 现在不是将数据拆分成mutlitple表,而是将两个部分组合在一起:

exprs <- lapply(
 0:(n - 1), 
 function(i) paste("CAST(xs[", i, "] AS double) AS x", i, sep=""))

df %>% 
  # Convert to native Spark
  spark_dataframe() %>%
  # Split and select xs, but retain y
  invoke("selectExpr", list("y", "split(xs, ',') AS  xs")) %>%
  invoke("selectExpr", c("CAST(y AS DOUBLE)", exprs)) %>%
  # Register table so we can access it from dplyr
  invoke("registerTempTable", "exploded_df")

and apply summarize_each : 并应用summarize_each

tbl(sc, "exploded_df") %>% summarize_each(funs(corr(., y)), starts_with("x"))
Source:   query [1 x 5]
Database: spark connection master=local[*] app=sparklyr local=TRUE

         x0         x1        x2         x3         x4
      <dbl>      <dbl>     <dbl>      <dbl>      <dbl>
1 0.8503358 -0.9972426 0.7242708 -0.9975092 -0.5571591

A quick sanity check (correlation between y and x0 , y and x4 ): 快速健全性检查( yx0yx4之间的相关性):

cor(c(21.66, 35.15, 15.22), c(2.643227, 3.422151, 2.8302398))
[1] 0.8503358
cor(c(21.66, 35.15, 15.22), c(3.8708665, 4.0771823, 4.6600842))
[1] -0.5571591

You can of course center the data first: 您当然可以首先集中数据:

exploded <- tbl(sc, "exploded_df")

avgs <- summarize_all(exploded, funs(mean)) %>% as.data.frame()
center_exprs <- as.list(paste(colnames(exploded ),"-", avgs))

transmute_(exploded, .dots = setNames(center_exprs, colnames(exploded))) %>% 
  summarize_each(funs(corr(., y)), starts_with("x"))

but it doesn't affect the result : 它不会影响结果

Source:   query [1 x 5]
Database: spark connection master=local[*] app=sparklyr local=TRUE

         x0         x1        x2         x3         x4
      <dbl>      <dbl>     <dbl>      <dbl>      <dbl>
1 0.8503358 -0.9972426 0.7242708 -0.9975092 -0.5571591

If both the transmute_ and summarize_each causes some issue we can push the centering and correlation directly into Spark: 如果transmute_summarize_each都会导致某些问题,我们可以将居中和关联直接推送到Spark:

#Centering
center_exprs <- as.list(paste(colnames(exploded ),"-", avgs))

exploded %>%  
  spark_dataframe() %>% 
  invoke("selectExpr", center_exprs) %>% 
  invoke("toDF", as.list(colnames(exploded))) %>%
  invoke("registerTempTable", "centered")

centered <- tbl(sc, "centered")

#Correlation
corr_exprs <- lapply(
  0:(n - 1), 
  function(i) paste("corr(y, x", i, ") AS x", i, sep=""))

centered %>% 
  spark_dataframe() %>% 
  invoke("selectExpr", corr_exprs) %>% 
  invoke("registerTempTable", "corrs")

 tbl(sc, "corrs")
Source:   query [1 x 5]
Database: spark connection master=local[*] app=sparklyr local=TRUE

         x0         x1        x2         x3         x4
      <dbl>      <dbl>     <dbl>      <dbl>      <dbl>
1 0.8503358 -0.9972426 0.7242708 -0.9975092 -0.5571591

Intermediate table is of course not necessary and this could be applied at the same time as we extract data from arrays. 中间表当然不是必需的,这可以在我们从数组中提取数据的同时应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM