Complete the code to import the CrossValidator class from Spark ML.
from pyspark.ml.tuning import [1]
The CrossValidator class is used for hyperparameter tuning with cross-validation in Spark ML.
Complete the code to create a parameter grid for tuning the maxDepth parameter of a DecisionTreeClassifier.
paramGrid = ParamGridBuilder().addGrid(dt.[1], [2, 4, 6]).build()
The maxDepth parameter controls the maximum depth of the decision tree and is commonly tuned.
Fix the error in the CrossValidator initialization by filling the correct evaluator parameter.
cv = CrossValidator(estimator=dt, estimatorParamMaps=paramGrid, evaluator=[1], numFolds=3)
For classification tasks with multiple classes, MulticlassClassificationEvaluator is the correct evaluator.
Fill both blanks to fit the CrossValidator model and get the best model.
cvModel = cv.[1](trainData) bestModel = cvModel.[2]
First, fit the CrossValidator on training data. Then access the bestModel attribute to get the best tuned model.
Fill all three blanks to evaluate the best model on test data and print the accuracy.
predictions = bestModel.[1](testData) evaluator = MulticlassClassificationEvaluator(labelCol='label', predictionCol='prediction', metricName=[2]) accuracy = evaluator.[3](predictions) print(f"Test Accuracy = {accuracy}")
Use transform to get predictions, set metricName to accuracy, and call evaluate to compute accuracy.
