aboutsummaryrefslogtreecommitdiff
path: root/mllib/src/test/java/org/apache
diff options
context:
space:
mode:
authorXiangrui Meng <meng@databricks.com>2016-06-14 18:57:45 -0700
committerYanbo Liang <ybliang8@gmail.com>2016-06-14 18:57:45 -0700
commit63e0aebe22ba41c636ecaddd8647721d7690a1ec (patch)
tree666ea76b8d347441d7a0f5116db56304f81ef16a /mllib/src/test/java/org/apache
parent42a28caf1001244d617b9256de196129348f2fef (diff)
downloadspark-63e0aebe22ba41c636ecaddd8647721d7690a1ec.tar.gz
spark-63e0aebe22ba41c636ecaddd8647721d7690a1ec.tar.bz2
spark-63e0aebe22ba41c636ecaddd8647721d7690a1ec.zip
[SPARK-15945][MLLIB] Conversion between old/new vector columns in a DataFrame (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 <meng@databricks.com> Closes #13662 from mengxr/SPARK-15945.
Diffstat (limited to 'mllib/src/test/java/org/apache')
-rw-r--r--mllib/src/test/java/org/apache/spark/mllib/util/JavaMLUtilsSuite.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/mllib/src/test/java/org/apache/spark/mllib/util/JavaMLUtilsSuite.java b/mllib/src/test/java/org/apache/spark/mllib/util/JavaMLUtilsSuite.java
new file mode 100644
index 0000000000..2fa0bd2546
--- /dev/null
+++ b/mllib/src/test/java/org/apache/spark/mllib/util/JavaMLUtilsSuite.java
@@ -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);
+ }
+}