zhipeng93 commented on code in PR #140:
URL: https://github.com/apache/flink-ml/pull/140#discussion_r945496201


##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/InteractionTest.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.interaction.Interaction;
+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.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+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.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/** Tests {@link Interaction}. */
+public class InteractionTest 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.dense(3, 4),
+                            Vectors.sparse(17, new int[] {0, 3, 9}, new 
double[] {1.0, 2.0, 7.0})),
+                    Row.of(
+                            2,
+                            Vectors.dense(2, 8),
+                            Vectors.dense(3, 4, 5),
+                            Vectors.sparse(17, new int[] {0, 2, 14}, new 
double[] {5.0, 4.0, 1.0})),
+                    Row.of(3, null, null, null));
+
+    private static final double[] EXPECTED_OUTPUT_DENSE_VEC_ARRAY_1 =

Review Comment:
   nit: Could we simplify the test code here with the following code and also 
the `verifyOutputResult` method?
   ```
   private static final List<DenseVector> EXPECTED_DENSE_OUTPUT = ...
   private static final List<DenseVector> EXPECTED_SPARSE_OUTPUT = ...
   ```



##########
flink-ml-lib/src/main/java/org/apache/flink/ml/feature/interaction/Interaction.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.interaction;
+
+import org.apache.flink.api.common.functions.MapFunction;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.ml.api.Transformer;
+import org.apache.flink.ml.common.datastream.TableUtils;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.linalg.typeinfo.VectorTypeInfo;
+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 org.apache.commons.lang3.ArrayUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A Transformer that takes vector or numerical columns, and generates a 
single vector column that
+ * contains the product of all combinations of one value from each input 
column.
+ *
+ * <p>For example, when the input feature values are Double(2) and Vector(3, 
4), the output would be
+ * Vector(6, 8). When the input feature values are Vector(1, 2) and Vector(3, 
4), the output would
+ * be Vector(3, 4, 6, 8). If you change the position of these two input 
Vectors, the output would be
+ * Vector(3, 6, 4, 8).
+ */
+public class Interaction implements Transformer<Interaction>, 
InteractionParams<Interaction> {
+    private final Map<Param<?>, Object> paramMap = new HashMap<>();
+
+    public Interaction() {
+        ParamUtils.initializeMapWithDefaultValues(paramMap, this);
+    }
+
+    @Override
+    public Table[] transform(Table... inputs) {
+        Preconditions.checkArgument(inputs.length == 1);
+        StreamTableEnvironment tEnv =
+                (StreamTableEnvironment) ((TableImpl) 
inputs[0]).getTableEnvironment();
+        RowTypeInfo inputTypeInfo = 
TableUtils.getRowTypeInfo(inputs[0].getResolvedSchema());
+        RowTypeInfo outputTypeInfo =
+                new RowTypeInfo(
+                        ArrayUtils.addAll(inputTypeInfo.getFieldTypes(), 
VectorTypeInfo.INSTANCE),
+                        ArrayUtils.addAll(inputTypeInfo.getFieldNames(), 
getOutputCol()));
+        DataStream<Row> output =
+                tEnv.toDataStream(inputs[0])
+                        .map(new InteractionFunction(getInputCols()), 
outputTypeInfo);
+        Table outputTable = tEnv.fromDataStream(output);
+        return new Table[] {outputTable};
+    }
+
+    private static class InteractionFunction implements MapFunction<Row, Row> {
+        private final String[] inputCols;
+        private final int[] featureSize;
+        private final int[][] featureIndices;
+        private final double[][] featureValues;
+
+        public InteractionFunction(String[] inputCols) {
+            this.inputCols = inputCols;
+            this.featureSize = new int[inputCols.length];
+            this.featureIndices = new int[inputCols.length][];
+            this.featureValues = new double[inputCols.length][];
+        }
+
+        @Override
+        public Row map(Row value) {
+            int nnz = 1;
+            boolean hasSparse = false;
+            for (int i = 0; i < inputCols.length; ++i) {
+                Object obj = value.getField(inputCols[i]);
+                if (obj == null) {
+                    return Row.join(value, Row.of((Object) null));
+                }
+                if (obj instanceof DenseVector) {
+                    featureSize[i] = ((Vector) obj).size();
+                    if (featureIndices[i] == null || featureIndices[i].length 
!= featureSize[i]) {
+                        featureIndices[i] = new int[featureSize[i]];
+                        for (int j = 0; j < featureSize[i]; ++j) {
+                            featureIndices[i][j] = j;
+                        }
+                    }
+                    featureValues[i] = ((DenseVector) obj).values;
+                    nnz *= featureSize[i];
+                } else if (obj instanceof SparseVector) {
+                    featureSize[i] = ((Vector) obj).size();
+                    featureIndices[i] = ((SparseVector) obj).indices;
+                    featureValues[i] = ((SparseVector) obj).values;
+                    nnz *= ((SparseVector) obj).values.length;
+                    hasSparse = true;
+                } else {
+                    featureSize[i] = 1;
+                    if (featureIndices[i] == null) {

Review Comment:
   nit: Is this line necessary here?



-- 
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

Reply via email to