pyspark.ml.connect.functions.vector_to_array#
- pyspark.ml.connect.functions.vector_to_array(col, dtype='float64')[source]#
Converts a column of MLlib sparse/dense vectors into a column of dense arrays.
New in version 3.0.0.
Changed in version 3.5.0: Supports Spark Connect.
- Parameters
- col
pyspark.sql.Column
or str Input column
- dtypestr, optional
The data type of the output array. Valid values: “float64” or “float32”.
- col
- Returns
pyspark.sql.Column
The converted column of dense arrays.
Examples
>>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.functions import vector_to_array >>> from pyspark.mllib.linalg import Vectors as OldVectors >>> df = spark.createDataFrame([ ... (Vectors.dense(1.0, 2.0, 3.0), OldVectors.dense(10.0, 20.0, 30.0)), ... (Vectors.sparse(3, [(0, 2.0), (2, 3.0)]), ... OldVectors.sparse(3, [(0, 20.0), (2, 30.0)]))], ... ["vec", "oldVec"]) >>> df1 = df.select(vector_to_array("vec").alias("vec"), ... vector_to_array("oldVec").alias("oldVec")) >>> df1.collect() [Row(vec=[1.0, 2.0, 3.0], oldVec=[10.0, 20.0, 30.0]), Row(vec=[2.0, 0.0, 3.0], oldVec=[20.0, 0.0, 30.0])] >>> df2 = df.select(vector_to_array("vec", "float32").alias("vec"), ... vector_to_array("oldVec", "float32").alias("oldVec")) >>> df2.collect() [Row(vec=[1.0, 2.0, 3.0], oldVec=[10.0, 20.0, 30.0]), Row(vec=[2.0, 0.0, 3.0], oldVec=[20.0, 0.0, 30.0])] >>> df1.schema.fields [StructField('vec', ArrayType(DoubleType(), False), False), StructField('oldVec', ArrayType(DoubleType(), False), False)] >>> df2.schema.fields [StructField('vec', ArrayType(FloatType(), False), False), StructField('oldVec', ArrayType(FloatType(), False), False)]