forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-15945][MLLIB] Conversion between old/new vector columns in a D…
…ataFrame (Scala/Java) ## What changes were proposed in this pull request? This PR provides conversion utils between old/new vector columns in a DataFrame. So users can use it to migrate their datasets and pipelines manually. The methods are implemented under `MLUtils` and called `convertVectorColumnsToML` and `convertVectorColumnsFromML`. Both take a DataFrame and a list of vector columns to be converted. It is a no-op on vector columns that are already converted. A warning message is logged if actual conversion happens. This is the first sub-task under SPARK-15944 to make it easier to migrate existing pipelines to Spark 2.0. ## How was this patch tested? Unit tests in Scala and Java. cc: yanboliang Author: Xiangrui Meng <[email protected]> Closes apache#13662 from mengxr/SPARK-15945.
- Loading branch information
1 parent
42a28ca
commit 63e0aeb
Showing
3 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
mllib/src/test/java/org/apache/spark/mllib/util/JavaMLUtilsSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.mllib.util; | ||
|
||
import java.util.Collections; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import org.apache.spark.SharedSparkSession; | ||
import org.apache.spark.mllib.linalg.Vector; | ||
import org.apache.spark.mllib.linalg.Vectors; | ||
import org.apache.spark.mllib.regression.LabeledPoint; | ||
import org.apache.spark.sql.Dataset; | ||
import org.apache.spark.sql.Row; | ||
import org.apache.spark.sql.RowFactory; | ||
|
||
public class JavaMLUtilsSuite extends SharedSparkSession { | ||
|
||
@Test | ||
public void testConvertVectorColumnsToAndFromML() { | ||
Vector x = Vectors.dense(2.0); | ||
Dataset<Row> dataset = spark.createDataFrame( | ||
Collections.singletonList(new LabeledPoint(1.0, x)), LabeledPoint.class | ||
).select("label", "features"); | ||
Dataset<Row> newDataset1 = MLUtils.convertVectorColumnsToML(dataset); | ||
Row new1 = newDataset1.first(); | ||
Assert.assertEquals(RowFactory.create(1.0, x.asML()), new1); | ||
Row new2 = MLUtils.convertVectorColumnsToML(dataset, "features").first(); | ||
Assert.assertEquals(new1, new2); | ||
Row old1 = MLUtils.convertVectorColumnsFromML(newDataset1).first(); | ||
Assert.assertEquals(RowFactory.create(1.0, x), old1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters