问题描述
如何使用 Apache Spark(特别是 sparklyr)逐行求解根?
使用 iris
数据集,假设方程为 0 = Petal_Length + Petal_Width + x
iris_tbl <- sparklyr::copy_to(sc,iris,"iris_spark")
所需的输出:
iris_tbl_withRoot
# Source: spark<iris_spark_zzz> [?? x 5]
Sepal_Length Sepal_Width Petal_Length Petal_Width Species x
<dbl> <dbl> <dbl> <dbl> <chr> <dbl>
1 5.1 3.5 1.4 0.2 setosa -1.6
2 4.9 3 1.4 0.2 setosa -1.6
3 4.7 3.2 1.3 0.2 setosa -1.5
4 4.6 3.1 1.5 0.2 setosa -1.7
现在,您可能想简单地求解 x,
iris_tbl %>% dplyr::mutate(x = 0 - Petal_Length - Petal_Width)
但是使用Ryacas,我无法解决x:
solve(g,"x")
Error in yac_core(x) :
Yacas returned this error: CommandLine(1) : Max evaluation stack depth reached.
Please use MaxEvalDepth to increase the stack size as needed.
因此,我正在寻找一种在 Spark 中逐行查找根的方法。
尝试使用 nleqslv::nleqslv()
失败
虽然我可以让 nleqslv::nleqslv()
在数据帧上工作,但我无法在 Spark 数据帧上弄清楚:
iris_tbl %>%
dplyr::mutate(formula = paste('f <- function(x) {return(',Petal_Width,' + ',Petal_Length,' + x)}',sep = '')) %>%
dplyr::mutate(root = nleqslv::nleqslv(1,eval(parse(text = formula)))$x)
Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'
iris_tbl %>%
dplyr::mutate(formula = paste('f <- function(x) {return(',sep = '')) %>%
sparklyr::spark_apply(function(df) apply(df,MARGIN = 1,FUN = function(x) {nleqslv::nleqslv(1,eval(parse(text = df[["formula"]])))$x}))
# Source: spark<?> [?? x 1]
result
<dbl>
1 -6.9
2 -6.9
3 -6.9
4 -6.9
5 -6.9
6 -6.9
7 -6.9
8 -6.9
9 -6.9
10 -6.9
# … with more rows
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)