Packages

class FeatureHasher extends Transformer with HasInputCols with HasOutputCol with HasNumFeatures with DefaultParamsWritable

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 contain either numeric or categorical features. Behavior and handling of column data types is as follows: -Numeric columns: For numeric features, the hash value of the column name is used to map the feature value to its index in the feature vector. By default, numeric features are not treated as categorical (even when they are integers). To treat them as categorical, specify the relevant columns in categoricalCols. -String columns: 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). -Boolean columns: Boolean values are treated in the same way as string columns. That is, boolean features are represented as "column_name=true" or "column_name=false", with an indicator value of 1.0.

Null (missing) values are ignored (implicitly zero in the resulting feature vector).

The hash function used here is also the MurmurHash 3 used in HashingTF. Since a simple modulo on the hashed value is used to determine the 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", "string")
 .setOutputCol("features")

hasher.transform(df).show(false)

+----+-----+---------+------+------------------------------------------------------+
|real|bool |stringNum|string|features                                              |
+----+-----+---------+------+------------------------------------------------------+
|2.0 |true |1        |foo   |(262144,[51871,63643,174475,253195],[1.0,1.0,2.0,1.0])|
|3.0 |false|2        |bar   |(262144,[6031,80619,140467,174475],[1.0,1.0,1.0,3.0]) |
+----+-----+---------+------+------------------------------------------------------+
Annotations
@Since( "2.3.0" )
Source
FeatureHasher.scala
Linear Supertypes
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. FeatureHasher
  2. DefaultParamsWritable
  3. MLWritable
  4. HasNumFeatures
  5. HasOutputCol
  6. HasInputCols
  7. Transformer
  8. PipelineStage
  9. Logging
  10. Params
  11. Serializable
  12. Serializable
  13. Identifiable
  14. AnyRef
  15. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Parameters

A list of (hyper-)parameter keys this algorithm can take. Users can set and get the parameter values through setters and getters, respectively.

  1. val categoricalCols: StringArrayParam

    Numeric columns to treat as categorical features.

    Numeric columns to treat as categorical features. By default only string and boolean columns are treated as categorical, so this param can be used to explicitly specify the numerical columns to treat as categorical. Note, the relevant columns should also be set in inputCols, categorical columns not set in inputCols will be listed in a warning.

    Annotations
    @Since( "2.3.0" )
  2. final val inputCols: StringArrayParam

    Param for input column names.

    Param for input column names.

    Definition Classes
    HasInputCols
  3. final val numFeatures: IntParam

    Param for Number of features.

    Param for Number of features. Should be greater than 0.

    Definition Classes
    HasNumFeatures
  4. final val outputCol: Param[String]

    Param for output column name.

    Param for output column name.

    Definition Classes
    HasOutputCol

Members

  1. final def clear(param: Param[_]): FeatureHasher.this.type

    Clears the user-supplied value for the input param.

    Clears the user-supplied value for the input param.

    Definition Classes
    Params
  2. def copy(extra: ParamMap): FeatureHasher

    Creates a copy of this instance with the same UID and some extra params.

    Creates a copy of this instance with the same UID and some extra params. Subclasses should implement this method and set the return type properly. See defaultCopy().

    Definition Classes
    FeatureHasherTransformerPipelineStageParams
    Annotations
    @Since( "2.3.0" )
  3. def explainParam(param: Param[_]): String

    Explains a param.

    Explains a param.

    param

    input param, must belong to this instance.

    returns

    a string that contains the input param name, doc, and optionally its default value and the user-supplied value

    Definition Classes
    Params
  4. def explainParams(): String

    Explains all params of this instance.

    Explains all params of this instance. See explainParam().

    Definition Classes
    Params
  5. final def extractParamMap(): ParamMap

    extractParamMap with no extra values.

    extractParamMap with no extra values.

    Definition Classes
    Params
  6. final def extractParamMap(extra: ParamMap): ParamMap

    Extracts the embedded default param values and user-supplied values, and then merges them with extra values from input into a flat param map, where the latter value is used if there exist conflicts, i.e., with ordering: default param values less than user-supplied values less than extra.

    Extracts the embedded default param values and user-supplied values, and then merges them with extra values from input into a flat param map, where the latter value is used if there exist conflicts, i.e., with ordering: default param values less than user-supplied values less than extra.

    Definition Classes
    Params
  7. final def get[T](param: Param[T]): Option[T]

    Optionally returns the user-supplied value of a param.

    Optionally returns the user-supplied value of a param.

    Definition Classes
    Params
  8. final def getDefault[T](param: Param[T]): Option[T]

    Gets the default value of a parameter.

    Gets the default value of a parameter.

    Definition Classes
    Params
  9. final def getOrDefault[T](param: Param[T]): T

    Gets the value of a param in the embedded param map or its default value.

    Gets the value of a param in the embedded param map or its default value. Throws an exception if neither is set.

    Definition Classes
    Params
  10. def getParam(paramName: String): Param[Any]

    Gets a param by its name.

    Gets a param by its name.

    Definition Classes
    Params
  11. final def hasDefault[T](param: Param[T]): Boolean

    Tests whether the input param has a default value set.

    Tests whether the input param has a default value set.

    Definition Classes
    Params
  12. def hasParam(paramName: String): Boolean

    Tests whether this instance contains a param with a given name.

    Tests whether this instance contains a param with a given name.

    Definition Classes
    Params
  13. final def isDefined(param: Param[_]): Boolean

    Checks whether a param is explicitly set or has a default value.

    Checks whether a param is explicitly set or has a default value.

    Definition Classes
    Params
  14. final def isSet(param: Param[_]): Boolean

    Checks whether a param is explicitly set.

    Checks whether a param is explicitly set.

    Definition Classes
    Params
  15. lazy val params: Array[Param[_]]

    Returns all params sorted by their names.

    Returns all params sorted by their names. The default implementation uses Java reflection to list all public methods that have no arguments and return Param.

    Definition Classes
    Params
    Note

    Developer should not use this method in constructor because we cannot guarantee that this variable gets initialized before other params.

  16. def save(path: String): Unit

    Saves this ML instance to the input path, a shortcut of write.save(path).

    Saves this ML instance to the input path, a shortcut of write.save(path).

    Definition Classes
    MLWritable
    Annotations
    @Since( "1.6.0" ) @throws( ... )
  17. final def set[T](param: Param[T], value: T): FeatureHasher.this.type

    Sets a parameter in the embedded param map.

    Sets a parameter in the embedded param map.

    Definition Classes
    Params
  18. def toString(): String
    Definition Classes
    FeatureHasherIdentifiable → AnyRef → Any
    Annotations
    @Since( "3.0.0" )
  19. def transform(dataset: Dataset[_]): DataFrame

    Transforms the input dataset.

    Transforms the input dataset.

    Definition Classes
    FeatureHasherTransformer
    Annotations
    @Since( "2.3.0" )
  20. def transform(dataset: Dataset[_], paramMap: ParamMap): DataFrame

    Transforms the dataset with provided parameter map as additional parameters.

    Transforms the dataset with provided parameter map as additional parameters.

    dataset

    input dataset

    paramMap

    additional parameters, overwrite embedded params

    returns

    transformed dataset

    Definition Classes
    Transformer
    Annotations
    @Since( "2.0.0" )
  21. def transform(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): DataFrame

    Transforms the dataset with optional parameters

    Transforms the dataset with optional parameters

    dataset

    input dataset

    firstParamPair

    the first param pair, overwrite embedded params

    otherParamPairs

    other param pairs, overwrite embedded params

    returns

    transformed dataset

    Definition Classes
    Transformer
    Annotations
    @Since( "2.0.0" ) @varargs()
  22. def transformSchema(schema: StructType): StructType

    Check transform validity and derive the output schema from the input schema.

    Check transform validity and derive the output schema from the input schema.

    We check validity for interactions between parameters during transformSchema and raise an exception if any parameter value is invalid. Parameter value checks which do not depend on other parameters are handled by Param.validate().

    Typical implementation should first conduct verification on schema change and parameter validity, including complex parameter interaction checks.

    Definition Classes
    FeatureHasherPipelineStage
    Annotations
    @Since( "2.3.0" )
  23. val uid: String

    An immutable unique ID for the object and its derivatives.

    An immutable unique ID for the object and its derivatives.

    Definition Classes
    FeatureHasherIdentifiable
    Annotations
    @Since( "2.3.0" )
  24. def write: MLWriter

    Returns an MLWriter instance for this ML instance.

    Returns an MLWriter instance for this ML instance.

    Definition Classes
    DefaultParamsWritableMLWritable

Parameter setters

  1. def setCategoricalCols(value: Array[String]): FeatureHasher.this.type

    Annotations
    @Since( "2.3.0" )
  2. def setInputCols(value: Array[String]): FeatureHasher.this.type

    Annotations
    @Since( "2.3.0" )
  3. def setInputCols(values: String*): FeatureHasher.this.type

    Annotations
    @Since( "2.3.0" )
  4. def setNumFeatures(value: Int): FeatureHasher.this.type

    Annotations
    @Since( "2.3.0" )
  5. def setOutputCol(value: String): FeatureHasher.this.type

    Annotations
    @Since( "2.3.0" )

Parameter getters

  1. def getCategoricalCols: Array[String]

    Annotations
    @Since( "2.3.0" )
  2. final def getInputCols: Array[String]

    Definition Classes
    HasInputCols
  3. final def getNumFeatures: Int

    Definition Classes
    HasNumFeatures
  4. final def getOutputCol: String

    Definition Classes
    HasOutputCol