UseMethod 中的 SparkR 错误("predict")

SparkR Error in UseMethod("predict")

遵循 ALS 示例 here

...但是 运行 在分布式模式下,例如

Sys.setenv("SPARKR_SUBMIT_ARGS"="--master yarn sparkr-shell")
spark <- sparkR.session(master = "yarn",
                    sparkConfig = list(
                      spark.driver.memory = "2g",
                      spark.driver.extraJavaOptions =
                        paste("-Dhive.metastore.uris=",
                              Sys.getenv("HIVE_METASTORE_URIS"),
                              " -Dspark.executor.instances=",
                              Sys.getenv("SPARK_EXECUTORS"),
                              " -Dspark.executor.cores=",
                              Sys.getenv("SPARK_CORES"),
                              sep = "")
                    ))


ratings <- list(list(0, 0, 4.0), list(0, 1, 2.0), list(1, 1, 3.0), list(1, 2, 4.0),list(2, 1, 1.0), list(2, 2, 5.0))
df <- createDataFrame(ratings, c("user", "item", "rating"))
model <- spark.als(df, "rating", "user", "item")
stats <- summary(model)
userFactors <- stats$userFactors
itemFactors <- stats$itemFactors
# make predictions
summary(model)
predicted <- predict(object=model, data=df)

我收到以下错误:

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "ALSModel"

source for 2.1.1这个方法好像是存在的,上面直接定义的summary()函数就可以了

我已经尝试使用 Spark、2.1.0、2.1.1 和 2.2.0-rc6,所有这些都给出了相同的结果。此外,这不仅限于 ALS 模型,对任何模型调用 predict() 都会产生相同的错误。

当我在本地模式下 运行 时,我也遇到同样的错误,例如

spark <- sparkR.session("local[*]")

以前有人遇到过这个问题吗?

虽然我没有完全重现你的错误(我得到了一个不同的错误),但问题很可能出在你的 predict 调用的第二个参数中,它应该是 newData,而不是data(参见 documentation)。

这里是从 RStudio 本地为 Spark 2.2.0 运行 改编的代码:

library(SparkR, lib.loc = "/home/ctsats/spark-2.2.0-bin-hadoop2.7/R/lib") # change the path accordingly here

sparkR.session(sparkHome = "/home/ctsats/spark-2.2.0-bin-hadoop2.7")      # and here

ratings <- list(list(0, 0, 4.0), list(0, 1, 2.0), list(1, 1, 3.0), list(1, 2, 4.0),list(2, 1, 1.0), list(2, 2, 5.0))
df <- createDataFrame(ratings, c("user", "item", "rating"))
model <- spark.als(df, "rating", "user", "item")
stats <- summary(model)
userFactors <- stats$userFactors
itemFactors <- stats$itemFactors
# make predictions
summary(model)
predicted <- predict(object=model, newData=df)  # newData here
showDF(predicted)
# +----+----+------+----------+
# |user|item|rating|prediction|
# +----+----+------+----------+
# | 1.0| 1.0|   3.0|  2.810426|
# | 2.0| 1.0|   1.0| 1.0784092|
# | 0.0| 1.0|   2.0|  1.997412|
# | 1.0| 2.0|   4.0| 3.9731808|
# | 2.0| 2.0|   5.0| 4.8602753|
# | 0.0| 0.0|   4.0| 3.8844662|
# +----+----+------+----------+

一个简单的 predict(model, df) 也可以。