我希望在数据框tfIdfFr
中插入名为"ref"
的列,该列的类型为pyspark.ml.linalg.SparseVector
。
当我尝试这个
ref = tfidfTest.select("features").collect()[0].features # the reference
tfIdfFr.withColumn("ref", ref).select("ref", "features").show()
我收到此错误
AssertionError: col should be Column
当我尝试这个:
from pyspark.sql.functions import lit
tfIdfFr.withColumn("ref", lit(ref)).select("ref", "features").show()
我收到该错误
AttributeError: 'SparseVector' object has no attribute '_get_object_id'
您是否知道在Dataframe列中插入常量SparseVector的解决方案?*
最佳答案
在这种情况下,我将跳过收集:
ref = tfidfTest.select(col("features").alias("ref")).limit(1)
tfIdfFr.crossJoin(ref)
通常,您可以使用
udf
:from pyspark.ml.linalg import DenseVector, SparseVector, Vector, Vectors, \
VectorUDT
from pyspark.sql.functions import udf
def vector_lit(v):
assert isinstance(v, Vector)
return udf(lambda: v, VectorUDT())()
用法:
spark.range(1).select(
vector_lit(Vectors.sparse(5, [1, 3], [-1, 1])
).alias("ref")).show()
+--------------------+
| ref|
+--------------------+
|(5,[1,3],[-1.0,1.0])|
+--------------------+
spark.range(1).select(vector_lit(Vectors.dense([1, 2, 3])).alias("ref")).show()
+-------------+
| ref|
+-------------+
|[1.0,2.0,3.0]|
+-------------+
也可以使用中间表示:
import json
from pyspark.sql.functions import from_json, lit
from pyspark.sql.types import StructType, StructField
def as_column(v):
assert isinstance(v, Vector)
if isinstance(v, DenseVector):
j = lit(json.dumps({"v": {
"type": 1,
"values": v.values.tolist()
}}))
else:
j = lit(json.dumps({"v": {
"type": 0,
"size": v.size,
"indices": v.indices.tolist(),
"values": v.values.tolist()
}}))
return from_json(j, StructType([StructField("v", VectorUDT())]))["v"]
用法:
spark.range(1).select(
as_column(Vectors.sparse(5, [1, 3], [-1, 1])
).alias("ref")).show()
+--------------------+
| ref|
+--------------------+
|(5,[1,3],[-1.0,1.0])|
+--------------------+
spark.range(1).select(as_column(Vectors.dense([1, 2, 3])).alias("ref")).show()
+-------------+
| ref|
+-------------+
|[1.0,2.0,3.0]|
+-------------+