VectorIndexer¶
- 
class pyspark.ml.feature.VectorIndexer(*, maxCategories: int = 20, inputCol: Optional[str] = None, outputCol: Optional[str] = None, handleInvalid: str = 'error')[source]¶
- Class for indexing categorical feature columns in a dataset of Vector. - This has 2 usage modes:
- Automatically identify categorical features (default behavior)
- This helps process a dataset of unknown vectors into a dataset with some continuous features and some categorical features. The choice between continuous and categorical is based upon a maxCategories parameter. 
- Set maxCategories to the maximum number of categorical any categorical feature should have. 
- E.g.: Feature 0 has unique values {-1.0, 0.0}, and feature 1 values {1.0, 3.0, 5.0}. If maxCategories = 2, then feature 0 will be declared categorical and use indices {0, 1}, and feature 1 will be declared continuous. 
 
 
- Index all features, if all features are categorical
- If maxCategories is set to be very large, then this will build an index of unique values for all features. 
- Warning: This can cause problems if features are continuous since this will collect ALL unique values to the driver. 
- E.g.: Feature 0 has unique values {-1.0, 0.0}, and feature 1 values {1.0, 3.0, 5.0}. If maxCategories >= 3, then both features will be declared categorical. 
 
 
 - This returns a model which can transform categorical features to use 0-based indices. 
- Index stability:
- This is not guaranteed to choose the same category index across multiple runs. 
- If a categorical feature includes value 0, then this is guaranteed to map value 0 to index 0. This maintains vector sparsity. 
- More stability may be added in the future. 
 
- TODO: Future extensions: The following functionality is planned for the future:
- Preserve metadata in transform; if a feature’s metadata is already present, do not recompute. 
- Specify certain features to not index, either via a parameter or via existing metadata. 
- Add warning if a categorical feature has only 1 category. 
 
 - New in version 1.4.0. - Examples - >>> from pyspark.ml.linalg import Vectors >>> df = spark.createDataFrame([(Vectors.dense([-1.0, 0.0]),), ... (Vectors.dense([0.0, 1.0]),), (Vectors.dense([0.0, 2.0]),)], ["a"]) >>> indexer = VectorIndexer(maxCategories=2, inputCol="a") >>> indexer.setOutputCol("indexed") VectorIndexer... >>> model = indexer.fit(df) >>> indexer.getHandleInvalid() 'error' >>> model.setOutputCol("output") VectorIndexerModel... >>> model.transform(df).head().output DenseVector([1.0, 0.0]) >>> model.numFeatures 2 >>> model.categoryMaps {0: {0.0: 0, -1.0: 1}} >>> indexer.setParams(outputCol="test").fit(df).transform(df).collect()[1].test DenseVector([0.0, 1.0]) >>> params = {indexer.maxCategories: 3, indexer.outputCol: "vector"} >>> model2 = indexer.fit(df, params) >>> model2.transform(df).head().vector DenseVector([1.0, 0.0]) >>> vectorIndexerPath = temp_path + "/vector-indexer" >>> indexer.save(vectorIndexerPath) >>> loadedIndexer = VectorIndexer.load(vectorIndexerPath) >>> loadedIndexer.getMaxCategories() == indexer.getMaxCategories() True >>> modelPath = temp_path + "/vector-indexer-model" >>> model.save(modelPath) >>> loadedModel = VectorIndexerModel.load(modelPath) >>> loadedModel.numFeatures == model.numFeatures True >>> loadedModel.categoryMaps == model.categoryMaps True >>> loadedModel.transform(df).take(1) == model.transform(df).take(1) True >>> dfWithInvalid = spark.createDataFrame([(Vectors.dense([3.0, 1.0]),)], ["a"]) >>> indexer.getHandleInvalid() 'error' >>> model3 = indexer.setHandleInvalid("skip").fit(df) >>> model3.transform(dfWithInvalid).count() 0 >>> model4 = indexer.setParams(handleInvalid="keep", outputCol="indexed").fit(df) >>> model4.transform(dfWithInvalid).head().indexed DenseVector([2.0, 1.0]) - Methods - clear(param)- Clears a param from the param map if it has been explicitly set. - copy([extra])- Creates a copy of this instance with the same uid and some extra params. - explainParam(param)- 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. - extractParamMap([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 < user-supplied values < extra. - fit(dataset[, params])- Fits a model to the input dataset with optional parameters. - fitMultiple(dataset, paramMaps)- Fits a model to the input dataset for each param map in paramMaps. - Gets the value of handleInvalid or its default value. - Gets the value of inputCol or its default value. - Gets the value of maxCategories or its default value. - getOrDefault(param)- 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. - getParam(paramName)- Gets a param by its name. - hasDefault(param)- Checks whether a param has a default value. - hasParam(paramName)- Tests whether this instance contains a param with a given (string) name. - isDefined(param)- Checks whether a param is explicitly set by user or has a default value. - isSet(param)- Checks whether a param is explicitly set by user. - load(path)- Reads an ML instance from the input path, a shortcut of read().load(path). - read()- Returns an MLReader instance for this class. - save(path)- Save this ML instance to the given path, a shortcut of ‘write().save(path)’. - set(param, value)- Sets a parameter in the embedded param map. - setHandleInvalid(value)- Sets the value of - handleInvalid.- setInputCol(value)- Sets the value of - inputCol.- setMaxCategories(value)- Sets the value of - maxCategories.- setOutputCol(value)- Sets the value of - outputCol.- setParams(self, \*[, maxCategories, …])- Sets params for this VectorIndexer. - write()- Returns an MLWriter instance for this ML instance. - Attributes - Returns all params ordered by name. - Methods Documentation - 
clear(param: pyspark.ml.param.Param) → None¶
- Clears a param from the param map if it has been explicitly set. 
 - 
copy(extra: Optional[ParamMap] = None) → JP¶
- 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. - Parameters
- extradict, optional
- Extra parameters to copy to the new instance 
 
- Returns
- JavaParams
- Copy of this instance 
 
 
 - 
explainParam(param: Union[str, pyspark.ml.param.Param]) → str¶
- Explains a single param and returns its name, doc, and optional default value and user-supplied value in a string. 
 - 
explainParams() → str¶
- Returns the documentation of all params with their optionally default values and user-supplied values. 
 - 
extractParamMap(extra: Optional[ParamMap] = None) → 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 < user-supplied values < extra. - Parameters
- extradict, optional
- extra param values 
 
- Returns
- dict
- merged param map 
 
 
 - 
fit(dataset: pyspark.sql.dataframe.DataFrame, params: Union[ParamMap, List[ParamMap], Tuple[ParamMap], None] = None) → Union[M, List[M]]¶
- Fits a model to the input dataset with optional parameters. - New in version 1.3.0. - Parameters
- datasetpyspark.sql.DataFrame
- input dataset. 
- paramsdict or list or tuple, optional
- 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. 
 
- dataset
- Returns
- Transformeror a list of- Transformer
- fitted model(s) 
 
 
 - 
fitMultiple(dataset: pyspark.sql.dataframe.DataFrame, paramMaps: Sequence[ParamMap]) → Iterator[Tuple[int, M]]¶
- Fits a model to the input dataset for each param map in paramMaps. - New in version 2.3.0. - Parameters
- datasetpyspark.sql.DataFrame
- input dataset. 
- paramMapscollections.abc.Sequence
- A Sequence of param maps. 
 
- dataset
- Returns
- _FitMultipleIterator
- A 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. 
 
 
 - 
getHandleInvalid() → str¶
- Gets the value of handleInvalid or its default value. 
 - 
getInputCol() → str¶
- Gets the value of inputCol or its default value. 
 - 
getMaxCategories() → int¶
- Gets the value of maxCategories or its default value. - New in version 1.4.0. 
 - 
getOrDefault(param: Union[str, pyspark.ml.param.Param[T]]) → Union[Any, T]¶
- Gets the value of a param in the user-supplied param map or its default value. Raises an error if neither is set. 
 - 
getOutputCol() → str¶
- Gets the value of outputCol or its default value. 
 - 
getParam(paramName: str) → pyspark.ml.param.Param¶
- Gets a param by its name. 
 - 
hasDefault(param: Union[str, pyspark.ml.param.Param[Any]]) → bool¶
- Checks whether a param has a default value. 
 - 
hasParam(paramName: str) → bool¶
- Tests whether this instance contains a param with a given (string) name. 
 - 
isDefined(param: Union[str, pyspark.ml.param.Param[Any]]) → bool¶
- Checks whether a param is explicitly set by user or has a default value. 
 - 
isSet(param: Union[str, pyspark.ml.param.Param[Any]]) → bool¶
- Checks whether a param is explicitly set by user. 
 - 
classmethod load(path: str) → RL¶
- Reads an ML instance from the input path, a shortcut of read().load(path). 
 - 
classmethod read() → pyspark.ml.util.JavaMLReader[RL]¶
- Returns an MLReader instance for this class. 
 - 
save(path: str) → None¶
- Save this ML instance to the given path, a shortcut of ‘write().save(path)’. 
 - 
set(param: pyspark.ml.param.Param, value: Any) → None¶
- Sets a parameter in the embedded param map. 
 - 
setHandleInvalid(value: str) → pyspark.ml.feature.VectorIndexer[source]¶
- Sets the value of - handleInvalid.
 - 
setInputCol(value: str) → pyspark.ml.feature.VectorIndexer[source]¶
- Sets the value of - inputCol.
 - 
setMaxCategories(value: int) → pyspark.ml.feature.VectorIndexer[source]¶
- Sets the value of - maxCategories.- New in version 1.4.0. 
 - 
setOutputCol(value: str) → pyspark.ml.feature.VectorIndexer[source]¶
- Sets the value of - outputCol.
 - 
setParams(self, \*, maxCategories=20, inputCol=None, outputCol=None, handleInvalid="error")[source]¶
- Sets params for this VectorIndexer. - New in version 1.4.0. 
 - 
write() → pyspark.ml.util.JavaMLWriter¶
- Returns an MLWriter instance for this ML instance. 
 - Attributes Documentation - 
handleInvalid= Param(parent='undefined', name='handleInvalid', doc="How to handle invalid data (unseen labels or NULL values). Options are 'skip' (filter out rows with invalid data), 'error' (throw an error), or 'keep' (put invalid data in a special additional bucket, at index of the number of categories of the feature).")¶
 - 
inputCol= Param(parent='undefined', name='inputCol', doc='input column name.')¶
 - 
maxCategories= Param(parent='undefined', name='maxCategories', doc='Threshold for the number of values a categorical feature can take (>= 2). If a feature is found to have > maxCategories values, then it is declared continuous.')¶
 - 
outputCol= Param(parent='undefined', name='outputCol', doc='output column name.')¶
 - 
params¶
- Returns all params ordered by name. The default implementation uses - dir()to get all attributes of type- Param.