You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2020/03/20 01:05:39 UTC

[GitHub] [spark] huaxingao commented on a change in pull request #27954: [SPARK-31185][ML] Implement VarianceThresholdSelector

huaxingao commented on a change in pull request #27954: [SPARK-31185][ML] Implement VarianceThresholdSelector
URL: https://github.com/apache/spark/pull/27954#discussion_r395398439
 
 

 ##########
 File path: mllib/src/main/scala/org/apache/spark/ml/feature/VarianceThresholdSelector.scala
 ##########
 @@ -0,0 +1,278 @@
+/*
+ * 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.{Attribute, AttributeGroup, NominalAttribute}
+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.Summarizer
+import org.apache.spark.ml.util._
+import org.apache.spark.sql._
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types.{StructField, StructType}
+
+
+/**
+ * Params for [[VarianceThresholdSelector]] and [[VarianceThresholdSelectorModel]].
+ */
+private[feature] trait VarianceThresholdSelectorParams extends Params
+  with HasFeaturesCol with HasOutputCol {
+
+  /**
+   * Param for variance threshold. Features with a variance lower than this threshold will
+   * be removed.
+   *
+   * @group param
+   */
+  @Since("3.1.0")
+  final val varianceThreshold = new DoubleParam(this, "varianceThreshold",
+    "Param for variance threshold. Features with a variance lower than this threshold" +
+      " will be removed.", ParamValidators.gt(0))
+
+  /** @group getParam */
+  @Since("3.1.0")
+  def getVarianceThreshold: Double = $(varianceThreshold)
+}
+
+/**
+ * Feature selector that removes all low-variance features. Features with a
+ * variance lower than the threshold will be removed. The default is to keep
+ * all features with non-zero variance, i.e. remove the features that have the
+ * same value in all samples.
+ */
+@Since("3.1.0")
+final class VarianceThresholdSelector @Since("3.1.0")(@Since("3.1.0") override val uid: String)
+  extends Estimator[VarianceThresholdSelectorModel] with VarianceThresholdSelectorParams
+with DefaultParamsWritable {
+
+  @Since("3.1.0")
+  def this() = this(Identifiable.randomUID("VarianceThresholdSelector"))
+
+  /** @group setParam */
+  @Since("3.1.0")
+  def setVarianceThreshold(value: Double): this.type = set(varianceThreshold, 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)
+
+  @Since("3.1.0")
+  override def fit(dataset: Dataset[_]): VarianceThresholdSelectorModel = {
+    transformSchema(dataset.schema, logging = true)
+
+    val Row(maxs: Vector, mins: Vector, variances: Vector) = dataset
+      .select(Summarizer.metrics("max", "min", "variance").summary(col($(featuresCol)))
+        .as("summary"))
+      .select("summary.max", "summary.min", "summary.variance")
+      .first()
+
+    val result = variances.toArray.zip(maxs.toArray).zip(mins.toArray).zipWithIndex
+    // if varianceThreshold not set, remove the features that have the same value in all samples.
+    val features = if (!isSet(varianceThreshold)) {
 
 Review comment:
   I thought we keep the features >= threshold, so I can't default it to 0 (variance 0 will be kept too). 
   Maybe I should change the definition to "keep the features > threshold"?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org