K-fold cross validation performs model selection by splitting the dataset into a set of non-overlapping randomly partitioned folds which are used as separate training and test datasets e.g., with k=3 folds, K-fold cross validation will generate 3 (training, test) dataset pairs, each of which uses 2/3 of the data for training and 1/3 for testing. Each fold is used as the test set exactly once.
New in version 1.4.0.
Examples
>>> from pyspark.ml.classification import LogisticRegression >>> from pyspark.ml.evaluation import BinaryClassificationEvaluator >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.tuning import CrossValidator, ParamGridBuilder, CrossValidatorModel >>> import tempfile >>> dataset = spark.createDataFrame( ... [(Vectors.dense([0.0]), 0.0), ... (Vectors.dense([0.4]), 1.0), ... (Vectors.dense([0.5]), 0.0), ... (Vectors.dense([0.6]), 1.0), ... (Vectors.dense([1.0]), 1.0)] * 10, ... ["features", "label"]) >>> lr = LogisticRegression() >>> grid = ParamGridBuilder().addGrid(lr.maxIter, [0, 1]).build() >>> evaluator = BinaryClassificationEvaluator() >>> cv = CrossValidator(estimator=lr, estimatorParamMaps=grid, evaluator=evaluator, ... parallelism=2) >>> cvModel = cv.fit(dataset) >>> cvModel.getNumFolds() 3 >>> float(cvModel.avgMetrics[0]) 0.5 >>> path = tempfile.mkdtemp() >>> model_path = path + "/model" >>> cvModel.write().save(model_path) >>> cvModelRead = CrossValidatorModel.read().load(model_path) >>> cvModelRead.avgMetrics [0.5, ... >>> evaluator.evaluate(cvModel.transform(dataset)) 0.8333... >>> evaluator.evaluate(cvModelRead.transform(dataset)) 0.8333...
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 a randomly generated 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 collectSubModels or its default value.
Gets the value of estimator or its default value.
Gets the value of estimatorParamMaps or its default value.
Gets the value of evaluator or its default value.
Gets the value of foldCol or its default value.
Gets the value of numFolds 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 parallelism or its default value.
getParam
(paramName)
Gets a param by its name.
getSeed
()
Gets the value of seed or its default value.
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.
setCollectSubModels
(value)
Sets the value of collectSubModels
.
setEstimator
(value)
Sets the value of estimator
.
setEstimatorParamMaps
(value)
Sets the value of estimatorParamMaps
.
setEvaluator
(value)
Sets the value of evaluator
.
setFoldCol
(value)
Sets the value of foldCol
.
setNumFolds
(value)
Sets the value of numFolds
.
setParallelism
(value)
Sets the value of parallelism
.
setParams
(*[, estimator, ...])
setParams(self, *, estimator=None, estimatorParamMaps=None, evaluator=None, numFolds=3, seed=None, parallelism=1, collectSubModels=False, foldCol=""): Sets params for cross validator.
setSeed
(value)
Sets the value of seed
.
write
()
Returns an MLWriter instance for this ML instance.
Attributes
Methods Documentation
Clears a param from the param map if it has been explicitly set.
Creates a copy of this instance with a randomly generated uid and some extra params. This copies creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over.
New in version 1.4.0.
Extra parameters to copy to the new instance
CrossValidator
Copy 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.DataFrame
input 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 Transformer
fitted model(s)
Fits a model to the input dataset for each param map in paramMaps.
New in version 2.3.0.
pyspark.sql.DataFrame
input dataset.
collections.abc.Sequence
A Sequence of param maps.
_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.
Gets the value of collectSubModels or its default value.
Gets the value of estimator or its default value.
New in version 2.0.0.
Gets the value of estimatorParamMaps or its default value.
New in version 2.0.0.
Gets the value of evaluator or its default value.
New in version 2.0.0.
Gets the value of foldCol or its default value.
New in version 3.1.0.
Gets the value of numFolds or its default value.
New in version 1.4.0.
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 parallelism or its default value.
Gets a param by its name.
Gets the value of seed or its default value.
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.
New in version 2.3.0.
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 collectSubModels
.
Sets the value of estimator
.
New in version 2.0.0.
Sets the value of estimatorParamMaps
.
New in version 2.0.0.
Sets the value of evaluator
.
New in version 2.0.0.
Sets the value of foldCol
.
New in version 3.1.0.
Sets the value of numFolds
.
New in version 1.4.0.
Sets the value of parallelism
.
setParams(self, *, estimator=None, estimatorParamMaps=None, evaluator=None, numFolds=3, seed=None, parallelism=1, collectSubModels=False, foldCol=ââ): Sets params for cross validator.
New in version 1.4.0.
Sets the value of seed
.
Returns an MLWriter instance for this ML instance.
New in version 2.3.0.
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4