Challenge - 5 Problems
CrossValidator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of CrossValidator with ParamGridBuilder
What will be the output type of the following Spark ML pipeline after fitting with CrossValidator?
Apache Spark
from pyspark.ml.classification import LogisticRegression from pyspark.ml.tuning import CrossValidator, ParamGridBuilder from pyspark.ml.evaluation import BinaryClassificationEvaluator lr = LogisticRegression() paramGrid = ParamGridBuilder()\ .addGrid(lr.regParam, [0.1, 0.01])\ .addGrid(lr.elasticNetParam, [0.0, 0.5, 1.0])\ .build() cv = CrossValidator(estimator=lr, estimatorParamMaps=paramGrid, evaluator=BinaryClassificationEvaluator(), numFolds=3) cvModel = cv.fit(trainingData) print(type(cvModel))
Attempts:
2 left
💡 Hint
CrossValidator.fit returns a model that contains the best model and metrics.
✗ Incorrect
The fit method of CrossValidator returns a CrossValidatorModel object, which holds the best model found during tuning and the metrics for all parameter combinations.
❓ data_output
intermediateNumber of models trained in CrossValidator
Given a ParamGrid with 3 values for regParam and 2 values for maxIter, and numFolds=4 in CrossValidator, how many models will be trained during fitting?
Apache Spark
from pyspark.ml.classification import LogisticRegression from pyspark.ml.tuning import CrossValidator, ParamGridBuilder lr = LogisticRegression() paramGrid = ParamGridBuilder()\ .addGrid(lr.regParam, [0.01, 0.1, 1.0])\ .addGrid(lr.maxIter, [10, 20])\ .build() cv = CrossValidator(estimator=lr, estimatorParamMaps=paramGrid, evaluator=None, numFolds=4)
Attempts:
2 left
💡 Hint
Multiply number of parameter combinations by number of folds.
✗ Incorrect
There are 3 regParam values and 2 maxIter values, so 3*2=6 parameter combinations. With 4 folds, total models trained = 6*4 = 24.
🔧 Debug
advancedIdentify the error in CrossValidator setup
What error will the following code raise when running cv.fit(trainingData)?
Apache Spark
from pyspark.ml.classification import LogisticRegression from pyspark.ml.tuning import CrossValidator, ParamGridBuilder from pyspark.ml.evaluation import BinaryClassificationEvaluator lr = LogisticRegression() paramGrid = ParamGridBuilder()\ .addGrid(lr.regParam, [0.1, 0.01])\ .build() cv = CrossValidator(estimator=lr, estimatorParamMaps=paramGrid, evaluator=BinaryClassificationEvaluator(), numFolds=0) cvModel = cv.fit(trainingData)
Attempts:
2 left
💡 Hint
CrossValidator requires at least 2 folds for cross-validation.
✗ Incorrect
numFolds=0 is invalid because cross-validation requires at least 2 folds. Spark raises a ValueError indicating this.
🚀 Application
advancedChoosing the best model from CrossValidatorModel
After fitting a CrossValidator, how can you extract the best model to use for predictions?
Apache Spark
cvModel = cv.fit(trainingData) bestModel = ???
Attempts:
2 left
💡 Hint
Check the attribute that stores the best fitted model in CrossValidatorModel.
✗ Incorrect
The best fitted model is stored in the attribute bestModel of the CrossValidatorModel object.
🧠 Conceptual
expertEffect of evaluator metric on CrossValidator results
Which statement best describes how the evaluator metric affects the CrossValidator's model selection?
Attempts:
2 left
💡 Hint
Think about what role the evaluator plays in cross-validation.
✗ Incorrect
The evaluator computes a metric (like accuracy or AUC) on validation folds. CrossValidator uses this metric to pick the best model.
