zhengruifeng commented on a change in pull request #27322: 
[SPARK-26111][ML][WIP] Support F-value between label/feature for continuous 
distribution feature selection
URL: https://github.com/apache/spark/pull/27322#discussion_r373762537
 
 

 ##########
 File path: 
mllib/src/main/scala/org/apache/spark/ml/feature/FRegressionSelector.scala
 ##########
 @@ -0,0 +1,357 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.ml.feature
+
+import scala.collection.mutable.ArrayBuilder
+
+import org.apache.hadoop.fs.Path
+
+import org.apache.spark.annotation.Since
+import org.apache.spark.ml._
+import org.apache.spark.ml.attribute.{AttributeGroup, _}
+import org.apache.spark.ml.linalg._
+import org.apache.spark.ml.param._
+import org.apache.spark.ml.param.shared._
+import org.apache.spark.ml.stat.FRegressionTest
+import org.apache.spark.ml.util._
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql._
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types.{DoubleType, StructField, StructType}
+
+
+/**
+ * Params for [[FRegressionSelector]] and [[FRegressionSelectorModel]].
+ * TODO: put all these params in shared.scala
+ * TODO: Not include fdr and fwe for now. Need to check if these two are 
applicable!!!
+ */
+private[feature] trait FRegressionSelectorParams extends Params
+  with HasFeaturesCol with HasOutputCol with HasLabelCol {
+
+  /**
+   * Number of features that selector will select, ordered by ascending 
p-value. If the
+   * number of features is less than numTopFeatures, then this will select all 
features.
+   * Only applicable when selectorType = "numTopFeatures".
+   * The default value of numTopFeatures is 50.
+   *
+   * @group param
+   */
+  @Since("3.1.0")
+  final val numTopFeatures = new IntParam(this, "numTopFeatures",
+    "Number of features that selector will select, ordered by ascending 
p-value. If the" +
+      " number of features is < numTopFeatures, then this will select all 
features.",
+    ParamValidators.gtEq(1))
+  setDefault(numTopFeatures -> 50)
+
+  /** @group getParam */
+  @Since("3.1.0")
+  def getNumTopFeatures: Int = $(numTopFeatures)
+
+  /**
+   * Percentile of features that selector will select, ordered by statistics 
value descending.
+   * Only applicable when selectorType = "percentile".
+   * Default value is 0.1.
+   * @group param
+   */
+  @Since("3.1.0")
+  final val percentile = new DoubleParam(this, "percentile",
+    "Percentile of features that selector will select, ordered by ascending 
p-value.",
+    ParamValidators.inRange(0, 1))
+  setDefault(percentile -> 0.1)
+
+  /** @group getParam */
+  @Since("3.1.0")
+  def getPercentile: Double = $(percentile)
+
+  /**
+   * The highest p-value for features to be kept.
+   * Only applicable when selectorType = "fpr".
+   * Default value is 0.05.
+   * @group param
+   */
+  @Since("3.1.0")
+  final val fpr = new DoubleParam(this, "fpr", "The highest p-value for 
features to be kept.",
+    ParamValidators.inRange(0, 1))
+  setDefault(fpr -> 0.05)
+
+  /** @group getParam */
+  @Since("3.1.0")
+  def getFpr: Double = $(fpr)
+
+  /**
+   * The selector type of the FRegressionSelector.
+   * Supported options: "numTopFeatures" (default), "percentile", "fpr".
+   * @group param
+   */
+  @Since("3.1.0")
+  final val selectorType = new Param[String](this, "selectorType",
+    "The selector type of the FRegressionSelector. " +
+      "Supported options: numTopFeatures, percentile, fpr")
+
+  /** @group getParam */
+  @Since("3.1.0")
+  def getSelectorType: String = $(selectorType)
+}
+
+/**
+ * Regression F-value Selector
+ * This feature selector is for regressions where features are continuous and 
labels are continuous.
+ * ANOVA F-value Classification Selector is for when features are continuous 
and labels are
+ * categorical.
+ * Currently, Chi-Squared is for categorical features and categorical labels
+ * The selector supports different selection methods: `numTopFeatures`, 
`percentile`, `fpr`
+ *  - `numTopFeatures` chooses a fixed number of top features according to a 
fRegression test.
+ *  - `percentile` is similar but chooses a fraction of all features instead 
of a fixed number.
+ *  - `fpr` chooses all features whose p-value are below a threshold, thus 
controlling the false
+ *    positive rate of selection.
+ *
+ * By default, the selection method is `numTopFeatures`, with the default 
number of top features
+ * set to 50.
+ */
+@Since("3.1.0")
+final class FRegressionSelector @Since("3.1.0") (@Since("3.1.0") override val 
uid: String)
+  extends Estimator[FRegressionSelectorModel] with FRegressionSelectorParams
+  with DefaultParamsWritable {
+
+  @Since("3.1.0")
+  def this() = this(Identifiable.randomUID("FRegressionSelector"))
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setNumTopFeatures(value: Int): this.type = set(numTopFeatures, value)
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setPercentile(value: Double): this.type = set(percentile, value)
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setFpr(value: Double): this.type = set(fpr, value)
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setSelectorType(value: String): this.type = set(selectorType, value)
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setFeaturesCol(value: String): this.type = set(featuresCol, value)
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setOutputCol(value: String): this.type = set(outputCol, value)
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setLabelCol(value: String): this.type = set(labelCol, value)
+
+  @Since("3.1.0")
+  override def fit(dataset: Dataset[_]): FRegressionSelectorModel = {
+    transformSchema(dataset.schema, logging = true)
+    val input: RDD[LabeledPoint] =
+      dataset.select(col($(labelCol)).cast(DoubleType), 
col($(featuresCol))).rdd.map {
+        case Row(label: Double, features: Vector) =>
+          LabeledPoint(label, features)
+      }
+    val FTestResult = FRegressionTest.test_regression(dataset, getFeaturesCol, 
getLabelCol)
+      .zipWithIndex
+    val features = $(selectorType) match {
+      case "numTopFeatures" =>
+        FTestResult
+          .sortBy { case (res, _) => res.pValue }
+          .take(getNumTopFeatures)
+      case "percentile" =>
+        FTestResult
+          .sortBy { case (res, _) => res.pValue }
+          .take((FTestResult.length * getPercentile).toInt)
+      case "fpr" =>
+        FTestResult
+          .filter { case (res, _) => res.pValue < getFpr }
+      case errorType =>
+        throw new IllegalStateException(s"Unknown FRegressionSelector Type: 
$errorType")
+    }
+    val indices = features.map { case (_, index) => index }
 
 Review comment:
   it seems that this `indices` need to be sorted?

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

Reply via email to