srowen commented on a change in pull request #25442: [SPARK-28722][ML] Change
sequential label sorting in StringIndexer fit to parallel
URL: https://github.com/apache/spark/pull/25442#discussion_r314088956
##########
File path: mllib/src/main/scala/org/apache/spark/ml/feature/StringIndexer.scala
##########
@@ -190,59 +189,69 @@ class StringIndexer @Since("1.4.0") (
new Column(If(col.isNaN.expr, Literal(null),
col.expr)).cast(StringType)
}
}
+ }
+ private def countByValue(
+ dataset: Dataset[_],
+ inputCols: Array[String]): Array[OpenHashMap[String, Long]] = {
+
+ val aggregator = new StringIndexerAggregator(inputCols.length)
+ implicit val encoder = Encoders.kryo[Array[OpenHashMap[String, Long]]]
+
+ val selectedCols = getSelectedCols(dataset, inputCols)
dataset.select(selectedCols: _*)
.toDF
.groupBy().agg(aggregator.toColumn)
.as[Array[OpenHashMap[String, Long]]]
.collect()(0)
}
- @Since("2.0.0")
- override def fit(dataset: Dataset[_]): StringIndexerModel = {
- transformSchema(dataset.schema, logging = true)
+ private def sortByFreq(dataset: Dataset[_], ascending: Boolean):
Array[Array[String]] = {
+ val (inputCols, _) = getInOutCols()
+ val sortFunc = StringIndexer.getSortFunc(ascending = ascending)
+ val orgStrings = countByValue(dataset, inputCols).toSeq
+ ThreadUtils.parmap(orgStrings, "sortingStringLabels", 8) { counts =>
+ counts.toSeq.sortWith(sortFunc).map(_._1).toArray
+ }.toArray
+ }
+
+ private def sortByAlphabet(dataset: Dataset[_], ascending: Boolean):
Array[Array[String]] = {
val (inputCols, _) = getInOutCols()
// If input dataset is not originally cached, we need to unpersist it
// once we persist it later.
val needUnpersist = dataset.storageLevel == StorageLevel.NONE
+ dataset.persist()
+ val selectedCols = getSelectedCols(dataset, inputCols).map(collect_set(_))
+ val allLabels = dataset.select(selectedCols: _*)
+ .collect().toSeq.flatMap(_.toSeq).asInstanceOf[Seq[Seq[String]]]
+ val labels = ThreadUtils.parmap(allLabels, "sortingStringLabels", 8) {
labels =>
+ val sorted = labels.filter(_ != null).sorted
+ if (ascending) {
Review comment:
Nit: unindent
----------------------------------------------------------------
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]