srowen commented on a change in pull request #28045: [SPARK-31283][ML] Simplify
ChiSq by adding a common method
URL: https://github.com/apache/spark/pull/28045#discussion_r399821160
##########
File path:
mllib/src/main/scala/org/apache/spark/mllib/stat/test/ChiSqTest.scala
##########
@@ -94,127 +94,73 @@ private[spark] object ChiSqTest extends Logging {
methodName: String = PEARSON.name): Array[ChiSqTestResult] = {
data.flatMap { case LabeledPoint(label, features) =>
require(features.size == numFeatures)
- features.iterator.map { case (col, value) =>
- (col, (value, label))
- }
+ features.iterator.map { case (col, value) => (col, (label, value)) }
}.aggregateByKey(new OpenHashMap[(Double, Double), Long])(
- seqOp = { case (count, t) =>
- count.changeValue(t, 1L, _ + 1L)
- count
+ seqOp = { case (counts, t) =>
+ counts.changeValue(t, 1L, _ + 1L)
+ counts
},
- combOp = { case (count1, count2) =>
- count2.iterator.foreach { case (t, c) =>
- count1.changeValue(t, c, _ + c)
- }
- count1
- }
- ).map { case (col, count) =>
- val label2Index =
count.iterator.map(_._1._2).toArray.distinct.sorted.zipWithIndex.toMap
- val numLabels = label2Index.size
- if (numLabels > maxCategories) {
- throw new SparkException(s"Chi-square test expect factors (categorical
values) but "
- + s"found more than $maxCategories distinct label values.")
- }
-
- val value2Index =
count.iterator.map(_._1._1).toArray.distinct.sorted.zipWithIndex.toMap
- val numValues = value2Index.size
- if (numValues > maxCategories) {
- throw new SparkException(s"Chi-square test expect factors (categorical
values) but "
- + s"found more than $maxCategories distinct values in column $col.")
+ combOp = { case (counts1, counts2) =>
+ counts2.foreach { case (t, c) => counts1.changeValue(t, c, _ + c) }
Review comment:
OK, so the iterator isn't helping here if just used with foreach. OK.
----------------------------------------------------------------
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]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]