| [ Web Proxy ] |
| Viewing: https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.feature.OneHotEncoder.html | [Back] [Original] |
Site Navigation
Site Navigation
Section Navigation
A one-hot encoder that maps a column of category indices to a column of binary vectors, with
at most a single one-value per row that indicates the input category index.
For example with 5 categories, an input value of 2.0 would map to an output vector of
[0.0, 0.0, 1.0, 0.0].
The last category is not included by default (configurable via dropLast),
because it makes the vector entries sum up to one, and hence linearly dependent.
So an input value of 4.0 maps to [0.0, 0.0, 0.0, 0.0].
When handleInvalid is configured to keep, an extra category indicating invalid
values is added as last category. So when dropLast is true, invalid values are
encoded as all-zeros vector.
New in version 2.3.0.
See also
StringIndexerfor converting categorical values into category indices
Notes
This is different from scikit-learns OneHotEncoder, which keeps all categories. The output vectors are sparse.
When encoding multi-column by using inputCols and
outputCols params, input/output cols come in pairs, specified by the order in
the arrays, and each pair is treated independently.
Examples
>>> from pyspark.ml.linalg import Vectors
>>> df = spark.createDataFrame([(0.0,), (1.0,), (2.0,)], ["input"])
>>> ohe = OneHotEncoder()
>>> ohe.setInputCols(["input"])
OneHotEncoder...
>>> ohe.setOutputCols(["output"])
OneHotEncoder...
>>> model = ohe.fit(df)
>>> model.setOutputCols(["output"])
OneHotEncoderModel...
>>> model.getHandleInvalid()
'error'
>>> model.transform(df).head().output
SparseVector(2, {0: 1.0})
>>> single_col_ohe = OneHotEncoder(inputCol="input", outputCol="output")
>>> single_col_model = single_col_ohe.fit(df)
>>> single_col_model.transform(df).head().output
SparseVector(2, {0: 1.0})
>>> ohePath = temp_path + "/ohe"
>>> ohe.save(ohePath)
>>> loadedOHE = OneHotEncoder.load(ohePath)
>>> loadedOHE.getInputCols() == ohe.getInputCols()
True
>>> modelPath = temp_path + "/ohe-model"
>>> model.save(modelPath)
>>> loadedModel = OneHotEncoderModel.load(modelPath)
>>> loadedModel.categorySizes == model.categorySizes
True
>>> loadedModel.transform(df).take(1) == model.transform(df).take(1)
True
Methods
|
Clears a param from the param map if it has been explicitly set. |
|
Creates a copy of this instance with the same uid and some extra params. |
|
Explains a single param and returns its name, doc, and optional default value and user-supplied value in a string. |
Returns the documentation of all params with their optionally default values and user-supplied values. |
|
|
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 < user-supplied values < extra. |
|
Fits a model to the input dataset with optional parameters. |
|
Fits a model to the input dataset for each param map in paramMaps. |
Gets the value of dropLast or its default value. |
|
Gets the value of handleInvalid or its default value. |
|
Gets the value of inputCol or its default value. |
|
Gets the value of inputCols or its default value. |
|
|
Gets the value of a param in the user-supplied param map or its default value. |
Gets the value of outputCol or its default value. |
|
Gets the value of outputCols or its default value. |
|
|
Gets a param by its name. |
|
Checks whether a param has a default value. |
|
Tests whether this instance contains a param with a given (string) name. |
|
Checks whether a param is explicitly set by user or has a default value. |
|
Checks whether a param is explicitly set by user. |
|
Reads an ML instance from the input path, a shortcut of read().load(path). |
|
Returns an MLReader instance for this class. |
|
Save this ML instance to the given path, a shortcut of 'write().save(path)'. |
|
Sets a parameter in the embedded param map. |
|
Sets the value of |
|
Sets the value of |
|
Sets the value of |
|
Sets the value of |
|
Sets the value of |
|
Sets the value of |
|
Sets params for this OneHotEncoder. |
|
Returns an MLWriter instance for this ML instance. |
Attributes
Returns all params ordered by name. |
Methods Documentation
Clears a param from the param map if it has been explicitly set.
Creates a copy of this instance with the same uid and some extra params. This implementation first calls Params.copy and then make a copy of the companion Java pipeline component with extra params. So both the Python wrapper and the Java pipeline component get copied.
Extra parameters to copy to the new instance
JavaParamsCopy of this instance
Explains a single param and returns its name, doc, and optional default value and user-supplied value in a string.
Returns the documentation of all params with their optionally default values and user-supplied values.
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 < user-supplied values < extra.
extra param values
merged param map
Fits a model to the input dataset with optional parameters.
New in version 1.3.0.
pyspark.sql.DataFrameinput dataset.
an optional param map that overrides embedded params. If a list/tuple of param maps is given, this calls fit on each param map and returns a list of models.
Transformer or a list of Transformerfitted model(s)
Fits a model to the input dataset for each param map in paramMaps.
New in version 2.3.0.
pyspark.sql.DataFrameinput dataset.
collections.abc.SequenceA Sequence of param maps.
_FitMultipleIteratorA thread safe iterable which contains one model for each param map. Each call to next(modelIterator) will return (index, model) where model was fit using paramMaps[index]. index values may not be sequential.
Gets the value of dropLast or its default value.
New in version 2.3.0.
Gets the value of handleInvalid or its default value.
Gets the value of inputCol or its default value.
Gets the value of inputCols or its default value.
Gets the value of a param in the user-supplied param map or its default value. Raises an error if neither is set.
Gets the value of outputCol or its default value.
Gets the value of outputCols or its default value.
Gets a param by its name.
Checks whether a param has a default value.
Tests whether this instance contains a param with a given (string) name.
Checks whether a param is explicitly set by user or has a default value.
Checks whether a param is explicitly set by user.
Reads an ML instance from the input path, a shortcut of read().load(path).
Returns an MLReader instance for this class.
Save this ML instance to the given path, a shortcut of write().save(path).
Sets a parameter in the embedded param map.
Sets the value of handleInvalid.
New in version 3.0.0.
Sets the value of outputCols.
New in version 3.0.0.
Sets params for this OneHotEncoder.
New in version 2.3.0.
Returns an MLWriter instance for this ML instance.
Attributes Documentation
Returns all params ordered by name. The default implementation
uses dir() to get all attributes of type
Param.
A unique id for the object.
Copyright @ 2026 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.
Created using Sphinx 4.5.0.
Built with the PyData Sphinx Theme 0.13.3.
| Web Proxy Viewer | New URL | Original Page |