gpt4 book ai didi

apache-spark - PySpark:标记点 RDD 的许多功能

转载 作者:行者123 更新时间:2023-12-04 23:18:59 26 4
gpt4 key购买 nike

Spark 新手,我读过的所有示例都处理小数据集,例如:

RDD = sc.parallelize([
LabeledPoint(1, [1.0, 2.0, 3.0]),
LabeledPoint(2, [3.0, 4.0, 5.0]),

但是,我有一个包含 50 多个功能的大型数据集。

一行示例
u'2596,51,3,258,0,510,221,232,148,6279,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,5'

我想在 PySpark 中快速创建一个 Labeledpoint RDD。我尝试将最后一个位置索引为 Labeledpoint RDD 中的第一个数据点,然后将前 n-1 个位置索引为密集向量。但是我收到以下错误。任何指导表示赞赏!注意:如果在创建标记点时将 [] 更改为 (),则会收到错误“语法无效”。
    df = myDataRDD.map(lambda line: line.split(','))
data = [
LabeledPoint(df[54], df[0:53])
]
TypeError: 'PipelinedRDD' object does not support indexing
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-67-fa1b56e8441e> in <module>()
2 df = myDataRDD.map(lambda line: line.split(','))
3 data = [
----> 4 LabeledPoint(df[54], df[0:53])
5 ]

TypeError: 'PipelinedRDD' object does not support indexing

最佳答案

由于您得到的错误状态,您无法通过索引访问 RDD。
您需要第二个 map将您的序列转换为 LabeledPoint 的语句秒

rows = [u'2596,51,3,258,0,510,221,232,148,6279,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,5', u'2596,51,3,258,0,510,221,232,148,6279,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,5']

rows_rdd = sc.parallelize(rows) # create RDD with given rows

labeled_points_rdd = rows_rdd\
.map(lambda row: row.split(','))\ # split rows into sequences
.map(lambda seq: LabeledPoint(seq[-1],seq[:-2])) # create Labeled Points from these sequences with last Item as label

print labeled_points_rdd.take(2)
# prints [LabeledPoint(5.0, [2596.0,51.0,3.0,258.0,0.0,510.0,221.0,...]),
# LabeledPoint(5.0,[2596.0,51.0,3.0,258.0,0.0,510.0,221.0,...])

请注意,python 中的负索引允许您向后访问序列。

.take(n)然后你得到第一个 n来自你的 RDD 的元素。

希望这可以帮助。

关于apache-spark - PySpark:标记点 RDD 的许多功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com