pyspark.ml.functions.array_to_vector

pyspark.ml.functions.array_to_vector(col)[source]

Converts a column of array of numeric type into a column of dense vectors in MLlib

New in version 3.1.0.

Parameters:
colpyspark.sql.Column or str

Input column

Returns:
pyspark.sql.Column

The converted column of MLlib dense vectors.

Examples

>>> from pyspark.ml.functions import array_to_vector
>>> df1 = spark.createDataFrame([([1.5, 2.5],),], schema='v1 array<double>')
>>> df1.select(array_to_vector('v1').alias('vec1')).collect()
[Row(vec1=DenseVector([1.5, 2.5]))]
>>> df2 = spark.createDataFrame([([1.5, 3.5],),], schema='v1 array<float>')
>>> df2.select(array_to_vector('v1').alias('vec1')).collect()
[Row(vec1=DenseVector([1.5, 3.5]))]
>>> df3 = spark.createDataFrame([([1, 3],),], schema='v1 array<int>')
>>> df3.select(array_to_vector('v1').alias('vec1')).collect()
[Row(vec1=DenseVector([1.0, 3.0]))]