-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-30154][ML] PySpark UDF to convert MLlib vectors to dense arrays #26910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
794a10b
afc71af
e2bb6c0
5aacfbc
66e3f5e
a41d01a
22865e0
05a525d
d257dce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.ml | ||
|
|
||
| import org.apache.spark.annotation.Since | ||
| import org.apache.spark.ml.linalg.Vector | ||
| import org.apache.spark.mllib.linalg.{Vector => OldVector} | ||
| import org.apache.spark.sql.Column | ||
| import org.apache.spark.sql.functions.udf | ||
|
|
||
| // scalastyle:off | ||
| @Since("3.0.0") | ||
| object functions { | ||
| // scalastyle:on | ||
|
|
||
| private val vectorToArrayUdf = udf { vec: Any => | ||
| vec match { | ||
| case v: Vector => v.toArray | ||
| case v: OldVector => v.toArray | ||
| case _ => throw new IllegalArgumentException( | ||
| "function vector_to_array requires a non-null input argument and input type must be " + | ||
| "`org.apache.spark.ml.linalg.Vector` or `org.apache.spark.mllib.linalg.Vector`.") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention input type (or null) in the error message.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean including
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add a test for the error message. |
||
| } | ||
| }.asNonNullable() | ||
|
|
||
| /** | ||
| * Converts a column of MLlib sparse/dense vectors into a column of dense arrays. | ||
| * | ||
| * @since 3.0.0 | ||
| */ | ||
| def vector_to_array(v: Column): Column = vectorToArrayUdf(v) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.ml | ||
|
|
||
| import org.apache.spark.ml.functions.vector_to_array | ||
| import org.apache.spark.ml.linalg.Vectors | ||
| import org.apache.spark.ml.util.MLTest | ||
| import org.apache.spark.mllib.linalg.{Vectors => OldVectors} | ||
|
|
||
| class FunctionsSuite extends MLTest { | ||
|
|
||
| import testImplicits._ | ||
|
|
||
| test("test vector_to_array") { | ||
| val df = Seq( | ||
| (Vectors.dense(1.0, 2.0, 3.0), OldVectors.dense(10.0, 20.0, 30.0)), | ||
| (Vectors.sparse(3, Seq((0, 2.0), (2, 3.0))), OldVectors.sparse(3, Seq((0, 20.0), (2, 30.0)))) | ||
| ).toDF("vec", "oldVec") | ||
|
|
||
| val result = df.select(vector_to_array('vec), vector_to_array('oldVec)) | ||
| .as[(Seq[Double], Seq[Double])] | ||
| .collect().toSeq | ||
|
|
||
| val expected = Seq( | ||
| (Seq(1.0, 2.0, 3.0), Seq(10.0, 20.0, 30.0)), | ||
| (Seq(2.0, 0.0, 3.0), Seq(20.0, 0.0, 30.0)) | ||
| ) | ||
| assert(result === expected) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # | ||
|
WeichenXu123 marked this conversation as resolved.
|
||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| from pyspark import since, SparkContext | ||
| from pyspark.sql.column import Column, _to_java_column | ||
|
|
||
|
|
||
| @since(3.0) | ||
| def vector_to_array(col): | ||
| """ | ||
| Converts a column of MLlib sparse/dense vectors into a column of dense arrays. | ||
|
|
||
| >>> 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"]) | ||
| >>> df.select(vector_to_array("vec").alias("vec"), | ||
| ... vector_to_array("oldVec").alias("oldVec")).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])] | ||
| """ | ||
| sc = SparkContext._active_spark_context | ||
| return Column( | ||
| sc._jvm.org.apache.spark.ml.functions.vector_to_array(_to_java_column(col))) | ||
|
WeichenXu123 marked this conversation as resolved.
WeichenXu123 marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.