NoSuchMethodException:Pyspark 模型加载中的 org.apache.spark.ml.classification.GBTClassificationModel

问题描述

我已经在 pyspark 中训练了一个模型

##Model
gbt = GBTClassifier(maxIter=10)
gbtModel = gbt.fit(train)
predictions = gbtModel.transform(test)

这里我正在保存管道和模型

#Save pipeline
pipelineModel.write().overwrite().save("s3://data-production/pipelineModel_v1")

#Save Model
gbtModel.save("s3://data-production/first_Trade.model_v0")

现在在生产/未来数据集中,我正在加载管道和模型

pipelineModel = PipelineModel.load("s3://data-production/pipelineModel_v1")

new_test= pipelineModel.transform(new_df1)

model = GBTClassifier.load("s3://data-production/first_Trade.model_v0")

模型加载后出现此错误

Py4JJavaError: An error occurred while calling o4701.load.
: java.lang.NoSuchMethodException: org.apache.spark.ml.classification.GBTClassificationModel.<init>(java.lang.String)
    at java.lang.class.getConstructor0(Class.java:3082)
    at java.lang.class.getConstructor(Class.java:1825)
    at org.apache.spark.ml.util.DefaultParamsReader.load(ReadWrite.scala:496)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

(<class 'py4j.protocol.Py4JJavaError'>,Py4JJavaError('An error occurred while calling o4701.load.\n',JavaObject id=o4702),<traceback object at 0x7f247a3eb9c8>)

解决方法

保存的模型本质上是经过训练的 GBTClassifier 的序列化版本。要反序列化模型,您还需要生产代码中的原始类。将此行添加到导入语句集。

from pyspark.ml.classification import GBTClassifier,GBTClassificationModel