Eoi*_*ane 11 python vector apache-spark pyspark
从Spark DataFrame开始,为进一步的分析处理创建矢量矩阵.
feature_matrix_vectors = feature_matrix1.map(lambda x: Vectors.dense(x)).cache()
feature_matrix_vectors.first()
Run Code Online (Sandbox Code Playgroud)
输出是一个向量数组.其中一些向量中包含空值
>>> DenseVector([1.0, 31.0, 5.0, 1935.0, 24.0])
...
>>> DenseVector([1.0, 1231.0, 15.0, 2008.0, null])
Run Code Online (Sandbox Code Playgroud)
从这里我想迭代矢量矩阵并创建一个LabeledPoint数组,如果向量包含null,则为0(零),否则为1.
def f(row):
if row.contain(None):
LabeledPoint(1.0,row)
else:
LabeledPoint(0.0,row)
Run Code Online (Sandbox Code Playgroud)
我试图使用迭代遍历向量矩阵
feature_matrix_labeledPoint = (f(row) for row in feature_matrix_vectors) # create a generator of row sums
next(feature_matrix_labeledPoint) # Run the iteration protocol
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
TypeError: 'PipelinedRDD' object is not iterable
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒
RDDs不是Python列表的替代品.您必须使用给定的可用操作或转换RDD.在这里你可以简单地使用map:
from pyspark.mllib.linalg import DenseVector
from pyspark.mllib.regression import LabeledPoint
feature_matrix_vectors = sc.parallelize([
DenseVector([1.0, 31.0, 5.0, 1935.0, 24.0]),
DenseVector([1.0, 1231.0, 15.0, 2008.0, None])
])
(feature_matrix_vectors
.map(lambda v: LabeledPoint(1.0 if None in v else 0.0, v))
.collect())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13523 次 |
| 最近记录: |