Re: [問題] spark 資料減去平均值

看板Python作者 (LawTea)時間5年前 (2018/10/01 20:26), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《comeonbaby (來巴寶貝)》之銘言: : 目前資料已分成一串tuple存在RDD裡面 (ID, Data) : 目前想做的是將Data值減去其ID的Data平均值 : 例如: (1,10) (1,20) (2,10) 跑完後會變成 : (1,-5) (1,5) (2,0) 這樣 : 因為剛接觸不太清楚該怎麼實作 沒用過Spark,但看起起來跟它無關 from collections import defaultdict x = [(1,10),(1,20),(2,10)] #your data avg = defaultdict(lambda:[0,0]) for ID,data in x: avg[ID][0] = (avg[ID][0]*avg[ID][1] + data)/(avg[ID][1]+1) avg[ID][1] += 1 y = list(map(lambda s:(s[0],s[1]-avg[s[0]][0]),x)) #result -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 58.114.212.150 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1538396778.A.EED.html
文章代碼(AID): #1RiX9gxj (Python)
文章代碼(AID): #1RiX9gxj (Python)