无法在Spark 2.x中加载Logistic回归模型

时间:2018-10-03 11:03:30

标签: apache-spark pyspark

我正在尝试保存和加载Spark 2.x版本中可用的选项。我建立了LogisticRegression模型并成功保存了该模型。但是在加载模型时,面临以下问题

代码段:

from pyspark.ml.classification import LogisticRegressionModel
LogisticRegressionModel.load("lrmodel")

错误消息:

Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.hadoop.security.authentication.util.KerberosUtil (file:/Volumes/Data/Innominds/spark-2.2.0-bin-hadoop2.7/jars/hadoop-auth-2.7.3.jar) to method sun.security.krb5.Config.getInstance()
WARNING: Please consider reporting this to the maintainers of org.apache.hadoop.security.authentication.util.KerberosUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
18/10/03 16:26:16 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
18/10/03 16:26:20 WARN SparkContext: Using an existing SparkContext; some configuration may not take effect.
Traceback (most recent call last):
  File "/Volumes/Data/Innominds/spark-2.2.0-bin-hadoop2.7/python/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/Volumes/Data/Innominds/spark-2.2.0-bin-hadoop2.7/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py", line 319, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o25.load.
: java.lang.IllegalArgumentException: requirement failed: Error loading metadata: Expected class name org.apache.spark.ml.classification.LogisticRegressionModel but found class name org.apache.spark.ml.PipelineModel
    at scala.Predef$.require(Predef.scala:224)
    at org.apache.spark.ml.util.DefaultParamsReader$.parseMetadata(ReadWrite.scala:404)
    at org.apache.spark.ml.util.DefaultParamsReader$.loadMetadata(ReadWrite.scala:383)
    at org.apache.spark.ml.classification.LogisticRegressionModel$LogisticRegressionModelReader.load(LogisticRegression.scala:1197)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.base/java.lang.Thread.run(Thread.java:844)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Volumes/Data/Innominds/WorkSpace/SparkIncrementalLearning/src/PipilineBasedModelling.py", line 59, in <module>
    loadAndRetrainModel(spark)
  File "/Volumes/Data/Innominds/WorkSpace/SparkIncrementalLearning/src/PipilineBasedModelling.py", line 51, in loadAndRetrainModel
    LogisticRegressionModel.load("lrmodel")
  File "/Volumes/Data/Innominds/spark-2.2.0-bin-hadoop2.7/python/pyspark/ml/util.py", line 257, in load
    return cls.read().load(path)
  File "/Volumes/Data/Innominds/spark-2.2.0-bin-hadoop2.7/python/pyspark/ml/util.py", line 197, in load
    java_obj = self._jread.load(path)
  File "/Volumes/Data/Innominds/spark-2.2.0-bin-hadoop2.7/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
  File "/Volumes/Data/Innominds/spark-2.2.0-bin-hadoop2.7/python/pyspark/sql/utils.py", line 79, in deco
    raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
pyspark.sql.utils.IllegalArgumentException: 'requirement failed: Error loading metadata: Expected class name org.apache.spark.ml.classification.LogisticRegressionModel but found class name org.apache.spark.ml.PipelineModel'

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:2)

那是因为您的模型不是LogisticRegressionModel。如果您阅读stracktrace,则会看到以下特定行(强调我的意思):

  

pyspark.sql.utils.IllegalArgumentException:'要求失败:加载元数据时出错:预期的类名org.apache.spark.ml.classification.LogisticRegressionModel ,但找到了类名org.apache .spark.ml.PipelineModel'

因此,您应该使用PipelineModel

from pyspark.ml import PipelineModel

PipelineModel.load("lrmodel")