Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/18513#discussion_r127642922
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/feature/FeatureHasher.scala ---
    @@ -0,0 +1,185 @@
    +/*
    + * 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 org.apache.spark.annotation.Since
    +import org.apache.spark.ml.Transformer
    +import org.apache.spark.ml.attribute.AttributeGroup
    +import org.apache.spark.ml.linalg.Vectors
    +import org.apache.spark.ml.param.{IntParam, ParamMap, ParamValidators}
    +import org.apache.spark.ml.param.shared.{HasInputCols, HasOutputCol}
    +import org.apache.spark.ml.util.{DefaultParamsReadable, 
DefaultParamsWritable, Identifiable, SchemaUtils}
    +import org.apache.spark.mllib.feature.{HashingTF => OldHashingTF}
    +import org.apache.spark.sql.{DataFrame, Dataset, Row}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.types._
    +import org.apache.spark.util.Utils
    +import org.apache.spark.util.collection.OpenHashMap
    +
    +/**
    + * Feature hashing projects a set of categorical or numerical features 
into a feature vector of
    + * specified dimension (typically substantially smaller than that of the 
original feature
    + * space). This is done using the hashing trick 
(https://en.wikipedia.org/wiki/Feature_hashing)
    + * to map features to indices in the feature vector.
    + *
    + * The [[FeatureHasher]] transformer operates on multiple columns. Each 
column may be numeric
    + * (representing a real feature) or string (representing a categorical 
feature). Boolean columns
    + * are also supported, and treated as categorical features. For numeric 
features, the hash value of
    + * the column name is used to map the feature value to its index in the 
feature vector.
    + * For categorical features, the hash value of the string 
"column_name=value" is used to map to the
    + * vector index, with an indicator value of `1.0`. Thus, categorical 
features are "one-hot" encoded
    + * (similarly to using [[OneHotEncoder]] with `dropLast=false`).
    + *
    + * Null (missing) values are ignored (implicitly zero in the resulting 
feature vector).
    + *
    + * Since a simple modulo is used to transform the hash function to a 
vector index,
    + * it is advisable to use a power of two as the numFeatures parameter;
    + * otherwise the features will not be mapped evenly to the vector indices.
    + *
    + * {{{
    + *   val df = Seq(
    + *    (2.0, true, "1", "foo"),
    + *    (3.0, false, "2", "bar")
    + *   ).toDF("real", "bool", "stringNum", "string")
    + *
    + *   val hasher = new FeatureHasher()
    + *    .setInputCols("real", "bool", "stringNum", "num")
    + *    .setOutputCol("features")
    + *
    + *   hasher.transform(df).show()
    + *
    + *   +----+-----+---------+------+--------------------+
    + *   |real| bool|stringNum|string|            features|
    + *   +----+-----+---------+------+--------------------+
    + *   | 2.0| true|        1|   foo|(262144,[51871,63...|
    + *   | 3.0|false|        2|   bar|(262144,[6031,806...|
    + *   +----+-----+---------+------+--------------------+
    + * }}}
    + */
    +@Since("2.3.0")
    +class FeatureHasher(@Since("2.3.0") override val uid: String) extends 
Transformer
    +  with HasInputCols with HasOutputCol with DefaultParamsWritable {
    +
    +  @Since("2.3.0")
    +  def this() = this(Identifiable.randomUID("featureHasher"))
    +
    +  /**
    +   * Number of features. Should be greater than 0.
    +   * (default = 2^18^)
    +   * @group param
    +   */
    +  @Since("2.3.0")
    +  val numFeatures = new IntParam(this, "numFeatures", "number of features 
(> 0)",
    +    ParamValidators.gt(0))
    +
    +  setDefault(numFeatures -> (1 << 18))
    +
    +  /** @group getParam */
    +  @Since("2.3.0")
    +  def getNumFeatures: Int = $(numFeatures)
    +
    +  /** @group setParam */
    +  @Since("2.3.0")
    +  def setNumFeatures(value: Int): this.type = set(numFeatures, value)
    +
    +  /** @group setParam */
    +  @Since("2.3.0")
    +  def setInputCols(values: String*): this.type = 
setInputCols(values.toArray)
    +
    +  /** @group setParam */
    +  @Since("2.3.0")
    +  def setInputCols(value: Array[String]): this.type = set(inputCols, value)
    +
    +  /** @group setParam */
    +  @Since("2.3.0")
    +  def setOutputCol(value: String): this.type = set(outputCol, value)
    +
    +  override def transform(dataset: Dataset[_]): DataFrame = {
    +    val hashFunc: Any => Int = OldHashingTF.murmur3Hash
    +    val n = $(numFeatures)
    +
    +    val outputSchema = transformSchema(dataset.schema)
    +    val realFields = outputSchema.fields.filter { f =>
    +      f.dataType.isInstanceOf[NumericType]
    +    }.map(_.name).toSet
    +
    +    def getDouble(x: Any): Double = {
    --- End diff --
    
    why?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to