srowen commented on a change in pull request #27841: [SPARK-31077][ML] Remove
ChiSqSelector dependency on mllib.ChiSqSelectorModel
URL: https://github.com/apache/spark/pull/27841#discussion_r389698830
##########
File path: mllib/src/main/scala/org/apache/spark/ml/stat/ChiSquareTest.scala
##########
@@ -75,4 +77,34 @@ object ChiSquareTest {
val statistics: Vector = Vectors.dense(testResults.map(_.statistic))
spark.createDataFrame(Seq(ChiSquareResult(pValues, degreesOfFreedom,
statistics)))
}
+
+ /**
+ * @param dataset DataFrame of categorical labels and categorical features.
+ * Real-valued features will be treated as categorical for
each distinct value.
+ * @param featuresCol Name of features column in dataset, of type `Vector`
(`VectorUDT`)
+ * @param labelCol Name of label column in dataset, of any numerical type
+ * @return Array containing the SelectionTestResult for every feature
against the label.
+ */
+ @Since("3.1.0")
+ def testChiSquare(dataset: Dataset[_], featuresCol: String, labelCol:
String):
+ Array[SelectionTestResult] = {
+
+ val spark = dataset.sparkSession
+
+ SchemaUtils.checkColumnType(dataset.schema, featuresCol, new VectorUDT)
+ SchemaUtils.checkNumericType(dataset.schema, labelCol)
+ val input: RDD[OldLabeledPoint] =
+ dataset.select(col(labelCol).cast(DoubleType), col(featuresCol)).rdd
+ .map {
+ case Row(label: Double, features: Vector) =>
+ OldLabeledPoint(label, OldVectors.fromML(features))
+ }
+ val chiTestResult = OldStatistics.chiSqTest(input)
+ var chiTestResultArray = new
Array[SelectionTestResult](chiTestResult.length)
Review comment:
Another nit but I think the remainder of the function could be:
```
chiTestResult.map(r => new ChiSqTestResult(r.pValue, r.degreesOfFreedom,
r.statistic)
```
----------------------------------------------------------------
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]