pyspark MLlib踩坑之model predict+rdd map zip,zip使用尤其注意啊啊啊!

简介:

一开始是因为没法直接在pyspark里使用map 来做model predict,但是scala是可以的!如下:

When we use Scala API a recommended way of getting predictions for RDD[LabeledPoint] using DecisionTreeModel is to simply map over RDD:

val labelAndPreds = testData.map { point => val prediction = model.predict(point.features) (point.label, prediction) }

Unfortunately similar approach in PySpark doesn't work so well:

labelsAndPredictions = testData.map( lambda lp: (lp.label, model.predict(lp.features)) labelsAndPredictions.first()

Exception: It appears that you are attempting to reference SparkContext from a broadcast variable, action, or transforamtion. SparkContext can only be used on the driver, not in code that it run on workers. For more information, see SPARK-5063.

Instead of that official documentation recommends something like this:

predictions = model.predict(testData.map(lambda x: x.features)) labelsAndPredictions = testData.map(lambda lp: lp.label).zip(predictions)

 

而这就是万恶的根源,因为zip在某些情况下并不能得到你想要的结果,就是说zip后的顺序是混乱的!!!我就在项目里遇到了!!!

 

This appears to imply that even the trivial a.map(f).zip(a) is not guaranteed to be equivalent to a.map(x => (f(x),x)). What is the situation when zip() results are reproducible?

见:https://stackoverflow.com/questions/29268210/mind-blown-rdd-zip-method

原因:

zip is generally speaking a tricky operation. It requires both RDDs not only to have the same number of partitions but also the same number of elements per partition.

Excluding some special cases this is guaranteed only if both RDDs have the same ancestor and there are not shuffles and operations potentially changing number of elements (filterflatMap) between the common ancestor and the current state. Typically it means only map (1-to-1) transformations.

见:https://stackoverflow.com/questions/32084368/can-only-zip-with-rdd-which-has-the-same-number-of-partitions-error

 

根源是因为我的ancestor rdd做了shuffle和filter的操作!最后在他们的子rdd上使用zip就会出错(数据乱序了)!!!真是太郁闷了,折腾一天这个问题,感谢上帝终于解决了!阿门!

 

最后我的解决方法是:

1、直接将rdd做union操作,rdd = rdd.union(sc.parallelize([])),然后map,zip就能输出正常结果了!

2、或者是直接将预测的rdd collect到driver机器,使用model predict,是比较丑陋的做法!


 









本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/7218268.html ,如需转载请自行联系原作者

相关文章
SpringMVC入门到实战------5、域对象共享数据 Request、Session、Application、Model、ModelAndView、Map、ModelMap的详细使用及代码实例
这篇文章详细解释了在IntelliJ IDEA中如何使用Mute Breakpoints功能来快速跳过程序中的后续断点,并展示了如何一键清空所有设置的断点。
SpringMVC入门到实战------5、域对象共享数据 Request、Session、Application、Model、ModelAndView、Map、ModelMap的详细使用及代码实例
域对象共享数据model、modelAndView、map、mapModel、request。从源码角度分析
这篇文章详细解释了在IntelliJ IDEA中如何使用Mute Breakpoints功能来快速跳过程序中的后续断点,并展示了如何一键清空所有设置的断点。
域对象共享数据model、modelAndView、map、mapModel、request。从源码角度分析
|
分布式计算 Serverless 数据处理
|
存储 数据处理 Python
python 之map、zip和filter迭代器示例详解
python 之map、zip和filter迭代器示例详解
158 0
【SpringMVC】SpringMVC方式,向作用域对象共享数据(ModelAndView、Model、map、ModelMap)
【SpringMVC】SpringMVC方式,向作用域对象共享数据(ModelAndView、Model、map、ModelMap)
133 1
|
JavaScript 前端开发 测试技术
[小笔记]TypeScript/JavaScript模拟Python中的zip(不使用map)
[小笔记]TypeScript/JavaScript模拟Python中的zip(不使用map)
122 0
|
分布式计算 Scala Spark
[Spark精进]必须掌握的4个RDD算子之map算子
[Spark精进]必须掌握的4个RDD算子之map算子
240 0
|
存储 前端开发 Java
SpringMVC里的Model、Map、ModelMap以及ModelAndView
SpringMVC里的Model、Map、ModelMap以及ModelAndView
472 0
|
Python
Python编程 字典创建map与Zip
Python编程 字典创建map与Zip
159 0
|
索引 Python
Python学习笔记(九) map、zip、filter、reduce
这篇文章主要介绍 Python 中几个常用的内置函数,用好这几个函数可以让自己的代码更加 Pythonic 哦
171 0