Complete the code to calculate the accuracy of a classification model using Spark MLlib.
from pyspark.ml.evaluation import MulticlassClassificationEvaluator evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName=[1]) accuracy = evaluator.evaluate(predictions) print(f"Accuracy: {accuracy}")
The metricName parameter should be set to "accuracy" to calculate the accuracy score of the classification model.
Complete the code to compute the root mean squared error (RMSE) for a regression model in Spark.
from pyspark.ml.evaluation import RegressionEvaluator evaluator = RegressionEvaluator(labelCol="label", predictionCol="prediction", metricName=[1]) rmse = evaluator.evaluate(predictions) print(f"RMSE: {rmse}")
To calculate root mean squared error, set metricName to "rmse" in RegressionEvaluator.
Fix the error in the code to compute the area under the ROC curve for a binary classification model.
from pyspark.ml.evaluation import BinaryClassificationEvaluator evaluator = BinaryClassificationEvaluator(labelCol="label", rawPredictionCol=[1], metricName="areaUnderROC") auc = evaluator.evaluate(predictions) print(f"AUC: {auc}")
The rawPredictionCol should be set to "rawPrediction" because the evaluator expects raw prediction scores for ROC calculation.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ["apple", "cat", "banana", "dog"] lengths = { [1] : [2] for word in words if len(word) > 3 }
The dictionary comprehension maps each word (key) to its length (value) for words longer than 3 characters.
Fill all three blanks to create a dictionary comprehension that maps each uppercase word to its length only if the length is greater than 3.
words = ["apple", "cat", "banana", "dog"] lengths = { [1] : [2] for word in words if [3] 3 }
The dictionary comprehension maps each uppercase word to its length only if the length is greater than 3.
