weibozhao commented on a change in pull request #24: URL: https://github.com/apache/flink-ml/pull/24#discussion_r766419360
########## File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/knn/KnnTest.java ########## @@ -0,0 +1,287 @@ +/* + * 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.flink.ml.classification.knn; + +import org.apache.flink.api.common.functions.MapFunction; +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.Vectors; +import org.apache.flink.ml.util.ReadWriteUtils; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.Schema; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.table.api.internal.TableImpl; +import org.apache.flink.types.Row; + +import org.apache.commons.collections.IteratorUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** Tests Knn and KnnModel. */ +public class KnnTest { + private StreamExecutionEnvironment env; + private StreamTableEnvironment tEnv; + private Table trainData; + private static final List<Row> trainArray = + new ArrayList<>( + Arrays.asList( + Row.of(1.0, Vectors.dense(2.0, 3.0)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(2.0, Vectors.dense(200.1, 300.1)), + Row.of(2.0, Vectors.dense(200.2, 300.2)), + Row.of(2.0, Vectors.dense(200.3, 300.3)), + Row.of(2.0, Vectors.dense(200.4, 300.4)), + Row.of(2.0, Vectors.dense(200.4, 300.4)), + Row.of(2.0, Vectors.dense(200.6, 300.6)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.3, 3.2)), + Row.of(1.0, Vectors.dense(2.3, 3.2)), + Row.of(3.0, Vectors.dense(2.8, 3.2)), + Row.of(4.0, Vectors.dense(300., 3.2)), + Row.of(1.0, Vectors.dense(2.2, 3.2)), + Row.of(5.0, Vectors.dense(2.4, 3.2)), + Row.of(5.0, Vectors.dense(2.5, 3.2)), + Row.of(5.0, Vectors.dense(2.5, 3.2)), + Row.of(1.0, Vectors.dense(2.1, 3.1)))); + + private static final List<Row> testArray = + new ArrayList<>( + Arrays.asList( + Row.of(5.0, Vectors.dense(4.0, 4.1)), + Row.of(2.0, Vectors.dense(300, 42)))); + private Table testData; + + @Before + public void before() { + Configuration config = new Configuration(); + config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true); + env = StreamExecutionEnvironment.getExecutionEnvironment(config); + env.setParallelism(4); + env.enableCheckpointing(100); + env.setRestartStrategy(RestartStrategies.noRestart()); + tEnv = StreamTableEnvironment.create(env); + + Schema schema = + Schema.newBuilder() + .column("f0", DataTypes.DOUBLE()) + .column("f1", DataTypes.of(DenseVector.class)) + .build(); + + DataStream<Row> dataStream = env.fromCollection(trainArray); + trainData = tEnv.fromDataStream(dataStream, schema).as("label", "features"); + + DataStream<Row> predDataStream = env.fromCollection(testArray); + testData = tEnv.fromDataStream(predDataStream, schema).as("label", "features"); + } + + // Executes the graph and returns a list which has true label and predict label. + private static List<Tuple2<Double, Double>> executeAndCollect(Table output) throws Exception { + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) output).getTableEnvironment(); + + DataStream<Tuple2<Double, Double>> stream = + tEnv.toDataStream(output) + .map( + new MapFunction<Row, Tuple2<Double, Double>>() { + @Override + public Tuple2<Double, Double> map(Row row) { + return Tuple2.of( + (Double) row.getField("label"), + (Double) row.getField("prediction")); + } + }); + return IteratorUtils.toList(stream.executeAndCollect()); + } + + private static void verifyClusteringResult(List<Tuple2<Double, Double>> result) { + for (Tuple2<Double, Double> t2 : result) { + Assert.assertEquals(t2.f0, t2.f1); + } + } + + /** Tests Param. */ + @Test + public void testParam() { + Knn knn = new Knn(); + assertEquals("features", knn.getFeaturesCol()); + assertEquals("label", knn.getLabelCol()); + assertEquals(10L, knn.getK().longValue()); + assertEquals("prediction", knn.getPredictionCol()); + + knn.setLabelCol("test_label") + .setFeaturesCol("test_features") + .setK(4) + .setPredictionCol("test_prediction"); + + assertEquals("test_features", knn.getFeaturesCol()); + assertEquals("test_label", knn.getLabelCol()); + assertEquals(4L, knn.getK().longValue()); + assertEquals("test_prediction", knn.getPredictionCol()); + } + + @Test + public void testFeaturePredictionParam() throws Exception { + Knn knn = + new Knn() + .setLabelCol("label") + .setFeaturesCol("features") + .setK(4) + .setPredictionCol("prediction"); + KnnModel model = knn.fit(trainData); + Table output = model.transform(testData)[0]; + + assertEquals( + Arrays.asList("label", "features", "prediction"), + output.getResolvedSchema().getColumnNames()); + + List<Tuple2<Double, Double>> result = executeAndCollect(output); + verifyClusteringResult(result); + } + + @Test + public void testFewerDistinctPointsThanCluster() throws Exception { + Knn knn = + new Knn() + .setLabelCol("label") + .setFeaturesCol("features") + .setK(4) + .setPredictionCol("prediction"); + KnnModel model = knn.fit(testData); + Table output = model.transform(testData)[0]; + + assertEquals( + Arrays.asList("label", "features", "prediction"), + output.getResolvedSchema().getColumnNames()); Review comment: OK ########## File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/knn/KnnTest.java ########## @@ -0,0 +1,287 @@ +/* + * 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.flink.ml.classification.knn; + +import org.apache.flink.api.common.functions.MapFunction; +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.Vectors; +import org.apache.flink.ml.util.ReadWriteUtils; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.Schema; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.table.api.internal.TableImpl; +import org.apache.flink.types.Row; + +import org.apache.commons.collections.IteratorUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** Tests Knn and KnnModel. */ +public class KnnTest { + private StreamExecutionEnvironment env; + private StreamTableEnvironment tEnv; + private Table trainData; + private static final List<Row> trainArray = + new ArrayList<>( + Arrays.asList( + Row.of(1.0, Vectors.dense(2.0, 3.0)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(2.0, Vectors.dense(200.1, 300.1)), + Row.of(2.0, Vectors.dense(200.2, 300.2)), + Row.of(2.0, Vectors.dense(200.3, 300.3)), + Row.of(2.0, Vectors.dense(200.4, 300.4)), + Row.of(2.0, Vectors.dense(200.4, 300.4)), + Row.of(2.0, Vectors.dense(200.6, 300.6)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.1, 3.1)), + Row.of(1.0, Vectors.dense(2.3, 3.2)), + Row.of(1.0, Vectors.dense(2.3, 3.2)), + Row.of(3.0, Vectors.dense(2.8, 3.2)), + Row.of(4.0, Vectors.dense(300., 3.2)), + Row.of(1.0, Vectors.dense(2.2, 3.2)), + Row.of(5.0, Vectors.dense(2.4, 3.2)), + Row.of(5.0, Vectors.dense(2.5, 3.2)), + Row.of(5.0, Vectors.dense(2.5, 3.2)), + Row.of(1.0, Vectors.dense(2.1, 3.1)))); + + private static final List<Row> testArray = + new ArrayList<>( + Arrays.asList( + Row.of(5.0, Vectors.dense(4.0, 4.1)), + Row.of(2.0, Vectors.dense(300, 42)))); + private Table testData; + + @Before + public void before() { + Configuration config = new Configuration(); + config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true); + env = StreamExecutionEnvironment.getExecutionEnvironment(config); + env.setParallelism(4); + env.enableCheckpointing(100); + env.setRestartStrategy(RestartStrategies.noRestart()); + tEnv = StreamTableEnvironment.create(env); + + Schema schema = + Schema.newBuilder() + .column("f0", DataTypes.DOUBLE()) + .column("f1", DataTypes.of(DenseVector.class)) + .build(); + + DataStream<Row> dataStream = env.fromCollection(trainArray); + trainData = tEnv.fromDataStream(dataStream, schema).as("label", "features"); + + DataStream<Row> predDataStream = env.fromCollection(testArray); + testData = tEnv.fromDataStream(predDataStream, schema).as("label", "features"); + } + + // Executes the graph and returns a list which has true label and predict label. + private static List<Tuple2<Double, Double>> executeAndCollect(Table output) throws Exception { + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) output).getTableEnvironment(); + + DataStream<Tuple2<Double, Double>> stream = + tEnv.toDataStream(output) + .map( + new MapFunction<Row, Tuple2<Double, Double>>() { + @Override + public Tuple2<Double, Double> map(Row row) { + return Tuple2.of( + (Double) row.getField("label"), + (Double) row.getField("prediction")); + } + }); + return IteratorUtils.toList(stream.executeAndCollect()); + } + + private static void verifyClusteringResult(List<Tuple2<Double, Double>> result) { + for (Tuple2<Double, Double> t2 : result) { + Assert.assertEquals(t2.f0, t2.f1); + } + } + + /** Tests Param. */ + @Test + public void testParam() { + Knn knn = new Knn(); + assertEquals("features", knn.getFeaturesCol()); + assertEquals("label", knn.getLabelCol()); + assertEquals(10L, knn.getK().longValue()); + assertEquals("prediction", knn.getPredictionCol()); + + knn.setLabelCol("test_label") + .setFeaturesCol("test_features") + .setK(4) + .setPredictionCol("test_prediction"); + + assertEquals("test_features", knn.getFeaturesCol()); + assertEquals("test_label", knn.getLabelCol()); + assertEquals(4L, knn.getK().longValue()); + assertEquals("test_prediction", knn.getPredictionCol()); + } + + @Test + public void testFeaturePredictionParam() throws Exception { + Knn knn = + new Knn() + .setLabelCol("label") + .setFeaturesCol("features") + .setK(4) + .setPredictionCol("prediction"); + KnnModel model = knn.fit(trainData); + Table output = model.transform(testData)[0]; + + assertEquals( + Arrays.asList("label", "features", "prediction"), + output.getResolvedSchema().getColumnNames()); + + List<Tuple2<Double, Double>> result = executeAndCollect(output); + verifyClusteringResult(result); + } + + @Test + public void testFewerDistinctPointsThanCluster() throws Exception { + Knn knn = + new Knn() + .setLabelCol("label") + .setFeaturesCol("features") + .setK(4) + .setPredictionCol("prediction"); + KnnModel model = knn.fit(testData); + Table output = model.transform(testData)[0]; + + assertEquals( + Arrays.asList("label", "features", "prediction"), + output.getResolvedSchema().getColumnNames()); + executeAndCollect(output); + } + + @Test + public void testFitAndPredict() throws Exception { + Knn knn = + new Knn() + .setLabelCol("label") + .setFeaturesCol("features") + .setK(4) + .setPredictionCol("prediction"); + KnnModel knnModel = knn.fit(trainData); + Table output = knnModel.transform(testData)[0]; + List<Tuple2<Double, Double>> result = executeAndCollect(output); + verifyClusteringResult(result); + } + + @Test + public void testSaveLoadAndPredict() throws Exception { + String path = Files.createTempDirectory("").toString(); + Knn knn = + new Knn() + .setLabelCol("label") + .setFeaturesCol("features") + .setK(4) + .setPredictionCol("prediction"); + knn.save(path); + + Knn loadKnn = Knn.load(env, path); Review comment: OK -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org