weibozhao commented on a change in pull request #24:
URL: https://github.com/apache/flink-ml/pull/24#discussion_r765525887



##########
File path: 
flink-ml-lib/src/main/java/org/apache/flink/ml/classification/knn/KnnModelData.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.classification.knn;
+
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.core.fs.FSDataInputStream;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+import org.apache.flink.ml.linalg.DenseMatrix;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.typeinfo.DenseMatrixSerializer;
+import org.apache.flink.ml.linalg.typeinfo.DenseVectorSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.types.Row;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+
+/** Knn model data, which stores the data used to calculate the distances 
between nodes. */
+public class KnnModelData {
+    private final List<Tuple3<DenseMatrix, DenseVector, int[]>> dictData;
+    private final Comparator<? super Tuple2<Double, Integer>> comparator;
+
+    /**
+     * Constructor.
+     *
+     * @param list Row list.
+     */
+    public KnnModelData(List<Row> list) {
+        this.dictData = new ArrayList<>(list.size());
+        for (Row row : list) {
+            this.dictData.add(
+                    Tuple3.of(
+                            (DenseMatrix) row.getField(0),
+                            (DenseVector) row.getField(1),
+                            (int[]) row.getField(2)));
+        }
+        comparator = Comparator.comparingDouble(o -> -o.f0);
+    }
+
+    /**
+     * Gets comparator.
+     *
+     * @return Comparator.
+     */
+    public Comparator<? super Tuple2<Double, Integer>> getQueueComparator() {
+        return comparator;
+    }
+
+    /**
+     * Gets dictionary data size.
+     *
+     * @return Dictionary data size.
+     */
+    public Integer getLength() {
+        return dictData.size();
+    }
+
+    public List<Tuple3<DenseMatrix, DenseVector, int[]>> getDictData() {
+        return dictData;
+    }
+
+    /** Encoder for the Knn model data. */
+    public static class ModelDataEncoder implements Encoder<Row> {
+        @Override
+        public void encode(Row modelData, OutputStream outputStream) throws 
IOException {
+            DataOutputView dataOutputView = new 
DataOutputViewStreamWrapper(outputStream);
+
+            DenseMatrixSerializer matrixSerializer = new 
DenseMatrixSerializer();
+            matrixSerializer.serialize((DenseMatrix) modelData.getField(0), 
dataOutputView);
+
+            DenseVectorSerializer vectorSerializer = new 
DenseVectorSerializer();
+            vectorSerializer.serialize((DenseVector) modelData.getField(1), 
dataOutputView);
+
+            int[] label = (int[]) 
Objects.requireNonNull(modelData.getField(2));
+            for (Integer integer : label) {
+                dataOutputView.writeInt(integer);
+            }
+        }
+    }
+
+    /** Decoder for the Knn model data. */
+    public static class ModelDataStreamFormat extends SimpleStreamFormat<Row> {
+
+        @Override
+        public Reader<Row> createReader(Configuration config, 
FSDataInputStream stream) {
+            return new Reader<Row>() {
+
+                @Override
+                public Row read() throws IOException {
+                    try {
+                        DataInputView source = new 
DataInputViewStreamWrapper(stream);
+                        DenseMatrix matrix = new 
DenseMatrixSerializer().deserialize(source);
+                        DenseVector vector = new 
DenseVectorSerializer().deserialize(source);
+                        int[] label = new int[vector.size()];
+                        for (int i = 0; i < label.length; ++i) {
+                            label[i] = source.readInt();
+                        }
+                        return Row.of(matrix, vector, label);
+                    } catch (EOFException e) {
+                        return null;
+                    }
+                }
+
+                @Override
+                public void close() throws IOException {
+                    stream.close();
+                }
+            };
+        }
+
+        @Override
+        public TypeInformation<Row> getProducedType() {

Review comment:
       
![image](https://user-images.githubusercontent.com/40415963/145357394-d0ca57b2-8b3c-4146-b69d-3e622f222d98.png)
   
   KMeans and LR also override this function, but the return value are 
different.




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