yunfengzhou-hub commented on code in PR #134: URL: https://github.com/apache/flink-ml/pull/134#discussion_r927221683
########## flink-ml-lib/src/main/java/org/apache/flink/ml/feature/vectorindexer/VectorIndexerParams.java: ########## @@ -0,0 +1,45 @@ +/* + * 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.vectorindexer; + +import org.apache.flink.ml.param.IntParam; +import org.apache.flink.ml.param.ParamValidators; + +/** + * Params of {@link VectorIndexer}. + * + * @param <T> The class type of this instance. + */ +public interface VectorIndexerParams<T> extends VectorIndexerModelParams<T> { + IntParam MAX_CATEGORIES = Review Comment: nit: current implementations tend to use `Param<Integer>` instead of `IntParam` here. I'm also OK with this one. ########## flink-ml-core/src/main/java/org/apache/flink/ml/linalg/SparseVector.java: ########## @@ -56,6 +56,16 @@ public double get(int i) { return 0.; } + @Override + public void set(int i, double value) { + int pos = Arrays.binarySearch(indices, i); + if (pos < 0) { + throw new IllegalStateException( + "Cannot set value for a SparseVector that does not contain value at that dimension."); Review Comment: This behavior seems different from that in Alink. I think that so long as `0 <= i < n`, the `set()` method should succeed. ########## flink-ml-lib/src/main/java/org/apache/flink/ml/feature/vectorindexer/VectorIndexer.java: ########## @@ -0,0 +1,255 @@ +/* + * 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.vectorindexer; + +import org.apache.flink.api.common.functions.MapFunction; +import org.apache.flink.api.common.functions.ReduceFunction; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.TypeHint; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.iteration.operator.OperatorStateUtils; +import org.apache.flink.ml.api.Estimator; +import org.apache.flink.ml.common.datastream.DataStreamUtils; +import org.apache.flink.ml.common.param.HasHandleInvalid; +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.runtime.state.StateInitializationContext; +import org.apache.flink.runtime.state.StateSnapshotContext; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.operators.AbstractStreamOperator; +import org.apache.flink.streaming.api.operators.BoundedOneInput; +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +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.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +/** + * An Estimator which implements the vector indexing algorithm. + * + * <p>A vector indexer maps each column of the input vector into a continuous/categorical feature. + * Whether one feature is transformed into a continuous or categorical feature depends on the number + * of distinct values in this column. If the number of distinct values in one column is greater than + * a specified parameter (i.e., maxCategories), the corresponding output column is unchanged. + * Otherwise, it is transformed into a categorical value. For categorical outputs, the indices are + * in [0, numDistinctValuesInThisColumn]. + * + * <p>The output model is organized in ascending order except that 0.0 is always mapped to 0 (for + * sparsity). We list two examples here: + * + * <ul> + * <li>If one column contains {-1.0, 1.0}, then -1.0 should be encoded as 0 and 1.0 will be + * encoded as 1. + * <li>If one column contains {-1.0, 0.0, 1.0}, then -1.0 should be encoded as 1, 0.0 should be + * encoded as 0 and 1.0 should be encoded as 2. + * </ul> + * + * <p>The `keep` option of {@link HasHandleInvalid} means that we put the invalid entries in a + * special bucket, whose index is the number of distinct values in this column. + */ +public class VectorIndexer + implements Estimator<VectorIndexer, VectorIndexerModel>, + VectorIndexerParams<VectorIndexer> { + private final Map<Param<?>, Object> paramMap = new HashMap<>(); + + public VectorIndexer() { + ParamUtils.initializeMapWithDefaultValues(paramMap, this); + } + + @Override + public VectorIndexerModel fit(Table... inputs) { + Preconditions.checkArgument(inputs.length == 1); + int maxCategories = getMaxCategories(); + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) inputs[0]).getTableEnvironment(); + + DataStream<HashSet<Double>[]> localDistinctDoubles = + tEnv.toDataStream(inputs[0]) + .transform( + "computeDistinctDoublesOperator", + TypeInformation.of(new TypeHint<HashSet<Double>[]>() {}), + new ComputeDistinctDoublesOperator(getInputCol(), maxCategories)); + + DataStream<HashSet<Double>[]> distinctDoubles = + DataStreamUtils.reduce( + localDistinctDoubles, + (ReduceFunction<HashSet<Double>[]>) + (value1, value2) -> { + for (int i = 0; i < value1.length; i++) { + if (value1[i] == null || value2[i] == null) { + value1[i] = null; + } else { + value1[i].addAll(value2[i]); Review Comment: nit: we can add a size check here so that all sets received by `ModelGenerator` would be valid categorical columns. -- 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