lindong28 commented on code in PR #158: URL: https://github.com/apache/flink-ml/pull/158#discussion_r984668405
########## flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VarianceThresholdSelectorTest.java: ########## @@ -0,0 +1,294 @@ +/* + * 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_OUTPUT_TRAIN = Review Comment: Should we remove this? ########## flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VarianceThresholdSelectorTest.java: ########## @@ -0,0 +1,294 @@ +/* + * 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_OUTPUT_TRAIN = + 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_OUTPUT_PREDICT = Review Comment: nits: would it be simpler to use `EXPECTED_OUTPUT` or `EXPECTED_PREDICT_OUTPUT`? ########## flink-ml-lib/src/main/java/org/apache/flink/ml/feature/variancethresholdselector/VarianceThresholdSelector.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.variancethresholdselector; + +import org.apache.flink.api.common.functions.AggregateFunction; +import org.apache.flink.api.common.functions.MapFunction; +import org.apache.flink.api.java.tuple.Tuple3; +import org.apache.flink.ml.api.Estimator; +import org.apache.flink.ml.common.datastream.DataStreamUtils; +import org.apache.flink.ml.linalg.BLAS; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.Vector; +import org.apache.flink.ml.param.Param; +import org.apache.flink.ml.util.ParamUtils; +import org.apache.flink.ml.util.ReadWriteUtils; +import org.apache.flink.streaming.api.datastream.DataStream; +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.flink.util.Preconditions; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.IntStream; + +/** + * An Estimator which implements the VarianceThresholdSelector algorithm. The algorithm removes all + * low-variance features. Features with a variance not greater than the threshold will be removed. + * The default is to keep all features with non-zero variance, i.e. remove the features that have + * the same value in all samples. + */ +public class VarianceThresholdSelector + implements Estimator<VarianceThresholdSelector, VarianceThresholdSelectorModel>, + VarianceThresholdSelectorParams<VarianceThresholdSelector> { + + private final Map<Param<?>, Object> paramMap = new HashMap<>(); + + public VarianceThresholdSelector() { + ParamUtils.initializeMapWithDefaultValues(paramMap, this); + } + + @Override + public VarianceThresholdSelectorModel fit(Table... inputs) { + Preconditions.checkArgument(inputs.length == 1); + final String featuresCol = getFeaturesCol(); + final double varianceThreshold = getVarianceThreshold(); Review Comment: Should we remove this line? -- 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]
