PySpark ML Pipeline.load 结果抛出 java.lang.UnsupportedOperationException:空集合
Posted
技术标签:
【中文标题】PySpark ML Pipeline.load 结果抛出 java.lang.UnsupportedOperationException:空集合【英文标题】:PySpark ML Pipeline.load results throws java.lang.UnsupportedOperationException: empty collection 【发布时间】:2018-02-21 23:50:50 【问题描述】:我有一个安装了 PySpark 的管道,我将其保存到磁盘以供以后使用。
这是我的管道代码:
model = Pipeline(stages=[segment_indexer, model_name_indexer, make_name_indexer, engine_type_indexer, segment_encoder, model_name_incoder, make_name_incoder, engine_type_incoder, x_assembler, estimator]).fit(trainingData)
model.save('file:/opt/app/fitted-model' )
这会将模型保存到磁盘。
我正在尝试像这样加载模型
model2 = pyspark.ml.pipeline.PipelineModel.load("file:/tmp/mymodels/fitted-model")
抛出
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/spark/python/pyspark/ml/util.py", line 257, in load
return cls.read().load(path)
File "/usr/lib/spark/python/pyspark/ml/util.py", line 197, in load
java_obj = self._jread.load(path)
File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
File "/usr/lib/spark/python/pyspark/sql/utils.py", line 63, in deco
return f(*a, **kw)
File "/usr/lib/spark/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 o2760.load.
: java.lang.UnsupportedOperationException: empty collection
at org.apache.spark.rdd.RDD$$anonfun$first$1.apply(RDD.scala:1370)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
at org.apache.spark.rdd.RDD.first(RDD.scala:1367)
at org.apache.spark.ml.util.DefaultParamsReader$.loadMetadata(ReadWrite.scala:382)
at org.apache.spark.ml.Pipeline$SharedReadWrite$.load(Pipeline.scala:266)
at org.apache.spark.ml.PipelineModel$PipelineModelReader.load(Pipeline.scala:347)
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: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.lang.Thread.run(Thread.java:748)
我做错了什么?
`
【问题讨论】:
【参考方案1】:我必须执行pipeline.transform
才能让管道在保存之前对数据帧实际应用转换。
之后,重新加载模型会带回模型实例,然后我们可以再次在 dataframe 上应用transform
。
【讨论】:
为什么在保存之前必须这样做?当然,您可以只做pipeline.fit(train)
并保存该结果吗?然后在你读回来的时候执行transform()
?还是那样不行?以上是关于PySpark ML Pipeline.load 结果抛出 java.lang.UnsupportedOperationException:空集合的主要内容,如果未能解决你的问题,请参考以下文章
如何从 PySpark 中的 spark.ml 中提取模型超参数?