jiangxin369 commented on code in PR #158: URL: https://github.com/apache/flink-ml/pull/158#discussion_r981893063
########## flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VarianceThresholdSelectorTest.java: ########## @@ -0,0 +1,310 @@ +/* + * 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.feature; + +import org.apache.flink.api.common.functions.MapFunction; +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.feature.variancethresholdselector.VarianceThresholdSelector; +import org.apache.flink.ml.feature.variancethresholdselector.VarianceThresholdSelectorModel; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.SparseVector; +import org.apache.flink.ml.linalg.Vectors; +import org.apache.flink.ml.util.TestUtils; +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.Expressions; +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.test.util.AbstractTestBase; +import org.apache.flink.types.Row; + +import org.apache.commons.collections.IteratorUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** Tests {@link VarianceThresholdSelector} and {@link VarianceThresholdSelectorModel}. */ +public class VarianceThresholdSelectorTest extends AbstractTestBase { + + @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); + private StreamExecutionEnvironment env; + private StreamTableEnvironment tEnv; + private Table trainDataTable; + private Table predictDataTable; + + private static final double EPS = 1.0e-5; + private static final List<Row> TRAIN_DATA = + new ArrayList<>( + Arrays.asList( + Row.of(1, Vectors.dense(5.0, 7.0, 0.0, 7.0, 6.0, 0.0)), + Row.of(2, Vectors.dense(0.0, 9.0, 6.0, 0.0, 5.0, 9.0)), + Row.of(3, Vectors.dense(0.0, 9.0, 3.0, 0.0, 5.0, 5.0)), + Row.of(4, Vectors.dense(1.0, 9.0, 8.0, 5.0, 7.0, 4.0)), + Row.of(5, Vectors.dense(9.0, 8.0, 6.0, 5.0, 4.0, 4.0)), + Row.of(6, Vectors.dense(6.0, 9.0, 7.0, 0.0, 2.0, 0.0)))); + + private static final List<Row> PREDICT_DATA = + new ArrayList<>( + Arrays.asList( + Row.of(Vectors.dense(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)), + Row.of(Vectors.dense(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)))); + + private static final List<DenseVector> EXPECTED_TRAIN_DATA = + new ArrayList<>( + Arrays.asList( + Vectors.dense(5.0, 7.0, 0.0), + Vectors.dense(0.0, 0.0, 9.0), + Vectors.dense(0.0, 0.0, 5.0), + Vectors.dense(1.0, 5.0, 4.0), + Vectors.dense(9.0, 5.0, 4.0), + Vectors.dense(6.0, 0.0, 0.0))); + + private static final List<DenseVector> EXPECTED_PREDICT_DATA = + new ArrayList<>( + Arrays.asList(Vectors.dense(1.0, 4.0, 6.0), Vectors.dense(0.1, 0.4, 0.6))); + + @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); + + trainDataTable = tEnv.fromDataStream(env.fromCollection(TRAIN_DATA)).as("id", "features"); + predictDataTable = tEnv.fromDataStream(env.fromCollection(PREDICT_DATA)).as("features"); + } + + private static void verifyPredictionResult( + Table output, String outputCol, List<DenseVector> expected) throws Exception { + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) output).getTableEnvironment(); + DataStream<DenseVector> stream = + tEnv.toDataStream(output) + .map( + (MapFunction<Row, DenseVector>) + row -> (DenseVector) row.getField(outputCol)); + List<DenseVector> result = IteratorUtils.toList(stream.executeAndCollect()); + compareResultCollections(expected, result, TestUtils::compare); + } + + @Test + public void testParam() { + VarianceThresholdSelector varianceThresholdSelector = new VarianceThresholdSelector(); + assertEquals("features", varianceThresholdSelector.getFeaturesCol()); + assertEquals("output", varianceThresholdSelector.getOutputCol()); + assertEquals(0.0, varianceThresholdSelector.getVarianceThreshold(), EPS); + + varianceThresholdSelector + .setFeaturesCol("test_feature") + .setOutputCol("test_output") + .setVarianceThreshold(0.5); + assertEquals("test_feature", varianceThresholdSelector.getFeaturesCol()); + assertEquals(0.5, varianceThresholdSelector.getVarianceThreshold(), EPS); + assertEquals("test_output", varianceThresholdSelector.getOutputCol()); + } + + @Test + public void testOutputSchema() { + VarianceThresholdSelector varianceThresholdSelector = + new VarianceThresholdSelector() + .setFeaturesCol("feature") + .setOutputCol("output") + .setVarianceThreshold(0.5); + + VarianceThresholdSelectorModel model = + varianceThresholdSelector.fit(trainDataTable.as("input")); + Table output = model.transform(trainDataTable)[0]; + assertEquals( + Arrays.asList("id", "features", "output"), + output.getResolvedSchema().getColumnNames()); + } + + @Test + public void testFitAndPredict() throws Exception { + VarianceThresholdSelector varianceThresholdSelector = + new VarianceThresholdSelector() + .setVarianceThreshold(8.0) + .setFeaturesCol("features"); + VarianceThresholdSelectorModel model = varianceThresholdSelector.fit(trainDataTable); + Table trainTableOutput = model.transform(trainDataTable)[0]; + verifyPredictionResult( + trainTableOutput, varianceThresholdSelector.getOutputCol(), EXPECTED_TRAIN_DATA); + + Table predictTableOutput = model.transform(predictDataTable)[0]; Review Comment: This check indicates that the model can be applied not only to the training datasets which is the most common usage, but also to the new datasets. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
