pyspark MLlib踩坑之model predict+rdd map zip,zip使用尤其注意啊啊啊!
Updated:use model broadcast, mappartition+flatmap,see:
from pyspark import SparkContext
import numpy as np
from sklearn import ensemble def batch(xs):
yield list(xs) N = 1000
train_x = np.random.randn(N, 10)
train_y = np.random.binomial(1, 0.5, N) model = ensemble.RandomForestClassifier(10).fit(train_x, train_y) test_x = np.random.randn(N * 100, 10) sc = SparkContext() n_partitions = 10
rdd = sc.parallelize(test_x, n_partitions).zipWithIndex() b_model = sc.broadcast(model) result = rdd.mapPartitions(batch) \
.map(lambda xs: ([x[0] for x in xs], [x[1] for x in xs])) \
.flatMap(lambda x: zip(x[1], b_model.value.predict(x[0]))) print(result.take(100))
see: https://gist.github.com/lucidfrontier45/591be3eb78557d1844ca
----------------------
一开始是因为没法直接在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 (filter, flatMap) 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,是比较丑陋的做法!
pyspark MLlib踩坑之model predict+rdd map zip,zip使用尤其注意啊啊啊!的更多相关文章
- Spark踩坑记——从RDD看集群调度
[TOC] 前言 在Spark的使用中,性能的调优配置过程中,查阅了很多资料,之前自己总结过两篇小博文Spark踩坑记--初试和Spark踩坑记--数据库(Hbase+Mysql),第一篇概况的归纳了 ...
- Django model重写save方法及update踩坑记录
一个非常实用的小方法 试想一下,Django中如果我们想对保存进数据库的数据做校验,有哪些实现的方法? 我们可以在view中去处理,每当view接收请求,就对提交的数据做校验,校验不通过直接返回错误, ...
- pyspark.mllib.feature module
Feature Extraction Feature Extraction converts vague features in the raw data into concrete numbers ...
- tensorflow踩坑合集2. TF Serving & gRPC 踩坑
这一章我们借着之前的NER的模型聊聊tensorflow serving,以及gRPC调用要注意的点.以下代码为了方便理解做了简化,完整代码详见Github-ChineseNER ,里面提供了训练好的 ...
- Spark踩坑记——Spark Streaming+Kafka
[TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...
- Spark踩坑记——数据库(Hbase+Mysql)
[TOC] 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时,我们往往需要操作数据库,去统计或者改变一些值.最近一个实时消费者处理任务,在使用spark streami ...
- Spark踩坑记——共享变量
[TOC] 前言 Spark踩坑记--初试 Spark踩坑记--数据库(Hbase+Mysql) Spark踩坑记--Spark Streaming+kafka应用及调优 在前面总结的几篇spark踩 ...
- [转]Spark 踩坑记:数据库(Hbase+Mysql)
https://cloud.tencent.com/developer/article/1004820 Spark 踩坑记:数据库(Hbase+Mysql) 前言 在使用Spark Streaming ...
- ABP框架入门踩坑-配置数据库表前缀
配置数据库表前缀 ABP踩坑记录-目录 本篇其实和ABP关系并不大,主要是EF Core的一些应用-.-. 起因 支持数据库表前缀应该是很多应用中比较常见的功能,而在ABP中并没直接提供这一功能,所以 ...
随机推荐
- BZOJ 3992 DP+NTT+快速幂
思路: 普通的DP很好想吧 f[i][j]+=f[i-1][j*s[k]] 前i个数 mod m=j 的个数 m是质数 模数是质数 这就很有趣了 那么我们就求出来原根 所有的数都取指数 搞出 ...
- BZOJ 2659 数学
思路: 一开始以为是真·欧几里德 a,b来回消 (其实用不了那么麻烦) 我们发现 这是一个矩形 求一下整点数 完了.. 要特判 p=q的情况 //By SiriusRen #include <c ...
- java使用FileUtils文件操作神器
前言: 在工作当中我们往往遇到很多文件的操作,我们也习惯写一些自己定义的工具类来简化文件操作,其实apache的commons的FileUtils类就是这样一个工具类,使用它能大大的简化我们对文件的操 ...
- Android拼图-变形金刚
开篇 学了几个月的Android开发,动手做了一个简单的拼图小游戏,没有使用游戏框架,名字也纯属娱乐,比较粗糙请大家一笑别骂. 游戏界面是一张图片切割的6*6的小图片,并将其中一块拿走,玩家通过不同的 ...
- jquery 获取及设置input各种类型的值
获取选中的值 获取一组radio被选中项的值 var item = $(“input[@name=items]:checked”).val(); 获取select被选中项的文本 var item = ...
- ADODB.RecordSet常用方法查询
rs = Server.CreateObject("ADODB.RecordSet") rs.Open(sqlStr,conn,1,A) 注:A=1表示读取数据:A=3表示新增.修 ...
- fabric.js 学习
官网地址:http://fabricjs.com/ git https://github.com/kangax/fabric.js/ <!DOCTYPE html> < ...
- switch穿透中语句的执行顺序
Day04_SHJavaTraing_4-6-2017 计算下列switch语句的运行结果 1. int x = 2,y=3; switch(x){ default: y++; case 3: y++ ...
- Socket 数据包顺序的问题
今天遇到一个问题,到现在还未查明原因,记录一下,留后续跟踪. 基于Netty的Socket通讯问题,Server在向Client发送数据时,假设数据原顺序为1,2,3,4... 但到了客户端顺序可能 ...
- python二级登陆菜单
""" 1.三级菜单 注册 登陆 注销 2.进入每一个一级菜单,都会有下一级的菜单"""user_item = dict()try: whi ...