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

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

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


##########
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:
   Does this line means the last bin edge is dropped?  
   
   If so, when the `tempBinEdges ` are `[0, 1, 1]`, `binEdges ` will be `[0, 1]`. Then all values are in same bin in `KBinsDiscretizerModel#transform`.   
   
   It seems this case should be handle separately when there are only two distinct bin edges.



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