yunfengzhou-hub commented on code in PR #146: URL: https://github.com/apache/flink-ml/pull/146#discussion_r956827092
########## flink-ml-lib/src/test/java/org/apache/flink/ml/feature/BinarizerTest.java: ########## @@ -0,0 +1,161 @@ +/* + * 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.restartstrategy.RestartStrategies; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.feature.binarizer.Binarizer; +import org.apache.flink.ml.linalg.Vector; +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.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.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +/** Tests {@link Binarizer}. */ +public class BinarizerTest extends AbstractTestBase { + + private StreamTableEnvironment tEnv; + private Table inputDataTable; + + private static final List<Row> INPUT_DATA = + Arrays.asList( + Row.of( + 1, + Vectors.dense(1, 2), + Vectors.sparse(17, new int[] {0, 3, 9}, new double[] {1.0, 2.0, 7.0})), + Row.of( + 2, + Vectors.dense(2, 1), + Vectors.sparse(17, new int[] {0, 2, 14}, new double[] {5.0, 4.0, 1.0})), + Row.of( + 3, + Vectors.dense(5, 18), + Vectors.sparse( + 17, new int[] {0, 11, 12}, new double[] {2.0, 4.0, 4.0}))); + + private static final Double[] EXPECTED_VALUE_OUTPUT = new Double[] {0.0, 1.0, 1.0}; + + private static final List<Vector> EXPECTED_DENSE_OUTPUT = + Arrays.asList( + Vectors.dense(0.0, 1.0), Vectors.dense(1.0, 0.0), Vectors.dense(1.0, 1.0)); + + private static final List<Vector> EXPECTED_SPARSE_OUTPUT = + Arrays.asList( + Vectors.sparse(17, new int[] {9}, new double[] {1.0}), + Vectors.sparse(17, new int[] {0, 2}, new double[] {1.0, 1.0}), + Vectors.sparse(17, new int[] {11, 12}, new double[] {1.0, 1.0})); + + @Before + public void before() { + Configuration config = new Configuration(); + config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true); + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(config); + + env.setParallelism(4); + env.enableCheckpointing(100); + env.setRestartStrategy(RestartStrategies.noRestart()); + + tEnv = StreamTableEnvironment.create(env); + DataStream<Row> dataStream = env.fromCollection(INPUT_DATA); + inputDataTable = tEnv.fromDataStream(dataStream).as("f0", "f1", "f2"); + } + + private void verifyOutputResult(Table output, String[] outputCols) throws Exception { + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) output).getTableEnvironment(); + DataStream<Row> stream = tEnv.toDataStream(output); + + List<Row> results = IteratorUtils.toList(stream.executeAndCollect()); + List<Double> doubleValues = new ArrayList<>(results.size()); + List<Vector> sparseVectorValues = new ArrayList<>(results.size()); + List<Vector> denseVectorValues = new ArrayList<>(results.size()); + for (Row row : results) { + if (row.getField(outputCols[0]) != null) { Review Comment: It seems that this `if` condition would never be true given our test data. Let's either remove this `if` condition or add `null` test data. ########## flink-ml-python/pyflink/ml/lib/feature/binarizer.py: ########## @@ -0,0 +1,68 @@ +################################################################################ +# 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. +################################################################################ +from typing import Tuple + +from pyflink.ml.core.param import ParamValidators, Param, FloatArrayParam +from pyflink.ml.core.wrapper import JavaWithParams +from pyflink.ml.lib.feature.common import JavaFeatureTransformer +from pyflink.ml.lib.param import HasInputCols, HasOutputCols + + +class _BinarizerParams( + JavaWithParams, + HasInputCols, + HasOutputCols +): + """ + Params for :class:`Binarizer`. + """ + + THRESHOLDS: Param[Tuple[float, ...]] = FloatArrayParam( + "thresholds", + "Binarization thresholds, when the input continuous feature value" + " is greater than the threshold, it will be set to 1.0, else 0.0.", Review Comment: Let's keep the description the same as that in Java. Besides, could you please check whether the other comments in the previous review about the Java implementation have corresponding components in Python? Let's also modify those components to keep them aligned with the implementation in Java. ########## flink-ml-python/pyflink/ml/lib/feature/tests/test_binarizer.py: ########## @@ -0,0 +1,81 @@ +################################################################################ +# 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. +################################################################################ +import os + +from pyflink.common import Types + +from pyflink.ml.core.linalg import Vectors, DenseVectorTypeInfo +from pyflink.ml.lib.feature.binarizer import Binarizer +from pyflink.ml.tests.test_utils import PyFlinkMLTestCase + + +class BinarizerTest(PyFlinkMLTestCase): + def setUp(self): + super(BinarizerTest, self).setUp() + self.input_data_table = self.t_env.from_data_stream( + self.env.from_collection([ + (0, + Vectors.dense(2.1, 3.1)), + (2, + Vectors.dense(3.3, 1.3)), + ], + type_info=Types.ROW_NAMED( + ['f0', 'f1'], + [Types.INT(), DenseVectorTypeInfo()]))) + + self.expected_output_data_1 = Vectors.dense(0.0, 1.0) + self.expected_output_data_2 = Vectors.dense(1.0, 0.0) + + def test_param(self): + binarizer = Binarizer() + + binarizer.set_input_cols('f0', 'f1') \ + .set_output_cols('of0', 'of1') \ + .set_thresholds(1.5, 2.5) + + self.assertEqual(('f0', 'f1'), binarizer.input_cols) + self.assertEqual(('of0', 'of1'), binarizer.output_cols) + self.assertEqual((1.5, 2.5), binarizer.get_thresholds()) + + def test_save_load_transform(self): + binarizer = Binarizer() \ + .set_input_cols('f0', 'f1') \ + .set_output_cols('of0', 'of1') \ + .set_thresholds(1.5, 2.5) + + path = os.path.join(self.temp_dir, 'test_save_load_transform_binarizer') + binarizer.save(path) + binarizer = Binarizer.load(self.t_env, path) + + output_table = binarizer.transform(self.input_data_table)[0] + actual_outputs = [(result[0], result[2], result[3]) for result in + self.t_env.to_data_stream(output_table).execute_and_collect()] + + self.assertEqual(2, len(actual_outputs)) + for actual_output in actual_outputs: + self.assertEqual(2, len(actual_output[2])) + if actual_output[0] == 0: + self.assertAlmostEqual(0.0, actual_output[1]) Review Comment: It would be better to explicitly set a tolerance threshold to the `assertAlmostEqual`. -- 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]
