mrk-andreev commented on a change in pull request #8466:
URL: https://github.com/apache/ignite/pull/8466#discussion_r535916861
##########
File path:
modules/ml/src/main/java/org/apache/ignite/ml/preprocessing/encoding/EncoderTrainer.java
##########
@@ -116,6 +143,77 @@
}
}
+ /**
+ * Calculates encoding frequencies as frequency divided on amount of rows
in dataset.
+ *
+ * NOTE: The amount of rows is calculated as sum of absolute frequencies.
+ *
+ * @param dataset Dataset.
+ * @return Encoding frequency for each feature.
+ */
+ private TargetEncodingMeta[]
calculateTargetEncodingFrequencies(Dataset<EmptyContext, EncoderPartitionData>
dataset) {
+ TargetCounter[] targetCounters = dataset.compute(
+ EncoderPartitionData::targetCounters,
+ (a, b) -> {
+ if (a == null)
+ return b;
+
+ if (b == null)
+ return a;
+
+ assert a.length == b.length;
+
+ for (int i = 0; i < a.length; i++) {
+ if (handledIndices.contains(i)) {
+ int finalI = i;
+ b[i].setTargetSum(a[i].getTargetSum() +
b[i].getTargetSum());
+ b[i].setTargetCount(a[i].getTargetCount() +
b[i].getTargetCount());
+ a[i].getCategoryCounts()
+ .forEach((k, v) ->
b[finalI].getCategoryCounts().merge(k, v, Long::sum));
+ a[i].getCategoryTargetSum()
+ .forEach((k, v) ->
b[finalI].getCategoryTargetSum().merge(k, v, Double::sum));
+ }
+ }
+ return b;
+ }
+ );
+
+ TargetEncodingMeta[] targetEncodingMetas = new
TargetEncodingMeta[targetCounters.length];
+ for (int i = 0; i < targetCounters.length; i++) {
+ if (handledIndices.contains(i)) {
+ int finalI = i;
+
+ targetEncodingMetas[i] = new TargetEncodingMeta(
+ targetCounters[i].getTargetSum() /
targetCounters[i].getTargetCount(),
+
targetCounters[i].getCategoryTargetSum().entrySet().stream()
+ .collect(Collectors.toMap(
+ Map.Entry::getKey,
+ value -> {
+ double prior =
targetCounters[finalI].getTargetSum() /
Review comment:
`prior` evaluation extracted but lambda still exists.
##########
File path:
modules/ml/src/main/java/org/apache/ignite/ml/preprocessing/encoding/EncoderTrainer.java
##########
@@ -116,6 +143,77 @@
}
}
+ /**
+ * Calculates encoding frequencies as frequency divided on amount of rows
in dataset.
+ *
+ * NOTE: The amount of rows is calculated as sum of absolute frequencies.
+ *
+ * @param dataset Dataset.
+ * @return Encoding frequency for each feature.
+ */
+ private TargetEncodingMeta[]
calculateTargetEncodingFrequencies(Dataset<EmptyContext, EncoderPartitionData>
dataset) {
+ TargetCounter[] targetCounters = dataset.compute(
+ EncoderPartitionData::targetCounters,
+ (a, b) -> {
+ if (a == null)
+ return b;
+
+ if (b == null)
+ return a;
+
+ assert a.length == b.length;
+
+ for (int i = 0; i < a.length; i++) {
+ if (handledIndices.contains(i)) {
+ int finalI = i;
+ b[i].setTargetSum(a[i].getTargetSum() +
b[i].getTargetSum());
+ b[i].setTargetCount(a[i].getTargetCount() +
b[i].getTargetCount());
+ a[i].getCategoryCounts()
+ .forEach((k, v) ->
b[finalI].getCategoryCounts().merge(k, v, Long::sum));
+ a[i].getCategoryTargetSum()
+ .forEach((k, v) ->
b[finalI].getCategoryTargetSum().merge(k, v, Double::sum));
+ }
+ }
+ return b;
+ }
+ );
+
+ TargetEncodingMeta[] targetEncodingMetas = new
TargetEncodingMeta[targetCounters.length];
+ for (int i = 0; i < targetCounters.length; i++) {
+ if (handledIndices.contains(i)) {
+ int finalI = i;
+
+ targetEncodingMetas[i] = new TargetEncodingMeta(
+ targetCounters[i].getTargetSum() /
targetCounters[i].getTargetCount(),
+
targetCounters[i].getCategoryTargetSum().entrySet().stream()
+ .collect(Collectors.toMap(
+ Map.Entry::getKey,
+ value -> {
+ double prior =
targetCounters[finalI].getTargetSum() /
+ targetCounters[finalI].getTargetCount();
+ double targetSum =
targetCounters[finalI].getCategoryTargetSum()
+ .get(value.getKey());
+ long categorySize =
targetCounters[finalI].getCategoryCounts()
+ .get(value.getKey());
+
+ if (categorySize < minCategorySize) {
+ return prior;
+ } else {
+ double categoryMean = targetSum /
categorySize;
+
+ double smoove = 1 / (1 +
+ Math.exp(-(categorySize -
minSamplesLeaf) / smoothing));
+ return prior * (1 - smoove) + categoryMean
* smoove;
+ }
+ }
+ ))
+ );
+ }
+ }
+
Review comment:
Done
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]