猿问

如何将Vector拆分为列 - 使用PySpark

如何将Vector拆分为列 - 使用PySpark

上下文:我有DataFrame2列:单词和向量。其中“vector”的列类型是VectorUDT

一个例子:

word    |  vectorassert  | [435,323,324,212...]

我希望得到这个:

word   |  v1 | v2  | v3 | v4 | v5 | v6 ......assert | 435 | 5435| 698| 356|....

题:

如何使用PySpark为每个维度拆分包含多列向量的列?

提前致谢


潇湘沐
浏览 2193回答 2
2回答

鸿蒙传说

def splitVecotr(df, new_features=['f1','f2']):schema = df.schema cols = df.columnsfor col in new_features: # new_features should be the same length as vector column length     schema = schema.add(col,DoubleType(),True)return spark.createDataFrame(df.rdd.map(lambda row: [row[i] for i in cols]+row.features.tolist()), schema)该函数将特征向量列转换为单独的列
随时随地看视频慕课网APP

相关分类

Python
我要回答