You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "zhipeng93 (via GitHub)" <gi...@apache.org> on 2023/03/31 08:58:28 UTC

[GitHub] [flink-ml] zhipeng93 commented on a diff in pull request #222: [FLINK-31029] Fix bug when using quantile in KbinsDiscretizer

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


##########
flink-ml-lib/src/main/java/org/apache/flink/ml/feature/kbinsdiscretizer/KBinsDiscretizer.java:
##########
@@ -220,22 +220,46 @@ private static double[][] findBinEdgesWithQuantileStrategy(
                 binEdges[columnId] =
                         new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY};
             } else {
-                double width = 1.0 * features.length / numBins;
-                double[] tempBinEdges = new double[numBins + 1];
+                double[] tempBinEdges;
+                if (features.length > numBins) {
+                    double width = 1.0 * features.length / numBins;
+                    tempBinEdges = new double[numBins + 1];
 
-                for (int binEdgeId = 0; binEdgeId < numBins; binEdgeId++) {
-                    tempBinEdges[binEdgeId] = features[(int) (binEdgeId * width)];
+                    for (int binEdgeId = 0; binEdgeId < numBins; binEdgeId++) {
+                        tempBinEdges[binEdgeId] = features[(int) (binEdgeId * width)];
+                    }
+                    tempBinEdges[numBins] = features[numData - 1];
+                } else {
+                    tempBinEdges = features;
                 }
-                tempBinEdges[numBins] = features[numData - 1];
 
-                // Removes bins that are empty, i.e., the left edge equals to the right edge.
-                Set<Double> edges = new HashSet<>(numBins);
+                // Bins with zero width should be converted to a non-empty bin.
+                Map<Double, Integer> edgesAndCnt = new HashMap<>(numBins);
                 for (double edge : tempBinEdges) {
+                    edgesAndCnt.put(edge, edgesAndCnt.getOrDefault(edge, 0) + 1);
+                }
+                List<Double> edges = new ArrayList<>();
+                for (Map.Entry<Double, Integer> edgeAndCnt : edgesAndCnt.entrySet()) {
+                    double edge = edgeAndCnt.getKey();
+                    int cnt = edgeAndCnt.getValue();
                     edges.add(edge);
+                    if (cnt > 1) {
+                        edges.add(edge);
+                    }
+                }
+                tempBinEdges = edges.stream().mapToDouble(Double::doubleValue).toArray();
+                Arrays.sort(tempBinEdges);
+                int i = 1;
+                for (; i < tempBinEdges.length - 1; i++) {
+                    if (tempBinEdges[i] == tempBinEdges[i - 1]) {
+                        tempBinEdges[i] = (tempBinEdges[i + 1] + tempBinEdges[i - 1]) / 2;
+                    }
+                }
+                if (tempBinEdges[i] == tempBinEdges[i - 1]) {
+                    binEdges[columnId] = Arrays.copyOfRange(tempBinEdges, 0, i);

Review Comment:
   Good catch. I have updated the logic and also updated the java doc.
   
   In the updated PR, [0, 1, 1] should be transformed into [0, 0.5, 1]



-- 
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-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org