I am trying to convert a pyspark dataframe column of DenseVector into array but I always got an error.
data = [(Vectors.dense([8.0, 1.0, 3.0, 2.0, 5.0]),),
(Vectors.dense([2.0, 0.0, 3.0, 4.0, 5.0]),),
(Vectors.dense([4.0, 0.0, 0.0, 6.0, 7.0]),)]
df = spark.createDataFrame(data,["features"])
I tried to define a UDF and use toArray()
to_array = udf(lambda x: x.toArray(), ArrayType(FloatType()))
df = df.withColumn('features', to_array('features'))
But then, I got the following error if I do df.collect()
org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 17.0 failed 4 times,
most recent failure: Lost task 1.3 in stage 17.0 (TID 100, 10.139.64.6, executor 0):
net.razorvine.pickle.PickleException: expected zero arguments for construction of ClassDict
(for numpy.core.multiarray._reconstruct)
Any idea on how I could achieve this ?