Challenge - 5 Problems
Spark Classification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of LogisticRegression Model Training
What is the output of the following Spark code snippet that trains a LogisticRegression model and prints the coefficients?
Apache Spark
from pyspark.ml.classification import LogisticRegression from pyspark.sql import SparkSession spark = SparkSession.builder.appName('TestLR').getOrCreate() data = spark.createDataFrame([ (1.0, [0.0, 1.1, 0.1]), (0.0, [2.0, 1.0, -1.0]), (0.0, [2.0, 1.3, 1.0]), (1.0, [0.0, 1.2, -0.5]) ], ["label", "features"]) lr = LogisticRegression(maxIter=10, regParam=0.01) model = lr.fit(data) print(model.coefficients)
Attempts:
2 left
💡 Hint
Look at the type of the coefficients printed by Spark ML LogisticRegression model.
✗ Incorrect
The LogisticRegression model in Spark ML returns coefficients as a DenseVector object, not a plain list.
❓ data_output
intermediateNumber of Trees in RandomForest Model
After training a RandomForestClassifier with numTrees=20 on a dataset, what is the value of the 'numTrees' attribute in the trained model?
Apache Spark
from pyspark.ml.classification import RandomForestClassifier from pyspark.sql import SparkSession spark = SparkSession.builder.appName('TestRF').getOrCreate() data = spark.createDataFrame([ (1.0, [0.0, 1.1]), (0.0, [2.0, 1.0]), (0.0, [2.0, 1.3]), (1.0, [0.0, 1.2]) ], ["label", "features"]) rf = RandomForestClassifier(numTrees=20, maxDepth=5) model = rf.fit(data) print(model.getNumTrees())
Attempts:
2 left
💡 Hint
The numTrees parameter controls how many trees are built in the forest.
✗ Incorrect
The getNumTrees method returns the number of trees in the trained RandomForest model, which matches the numTrees parameter.
🔧 Debug
advancedIdentify the Error in LogisticRegression Prediction Code
What error will the following code raise when trying to predict with a LogisticRegression model?
Apache Spark
from pyspark.ml.classification import LogisticRegression from pyspark.sql import SparkSession spark = SparkSession.builder.appName('TestLR').getOrCreate() data = spark.createDataFrame([ (1.0, [0.0, 1.1, 0.1]) ], ["label", "features"]) lr = LogisticRegression(maxIter=10) model = lr.fit(data) predictions = model.transform([0.0, 1.0]) print(predictions.collect())
Attempts:
2 left
💡 Hint
Check the input type passed to model.transform method.
✗ Incorrect
The transform method expects a DataFrame, but a list is passed, causing a TypeError.
❓ visualization
advancedInterpreting Feature Importances from RandomForest
Given a trained RandomForestClassifier model, which option correctly describes the output of model.featureImportances?
Apache Spark
from pyspark.ml.classification import RandomForestClassifier from pyspark.sql import SparkSession spark = SparkSession.builder.appName('TestRF').getOrCreate() data = spark.createDataFrame([ (1.0, [0.0, 1.1, 0.1]), (0.0, [2.0, 1.0, -1.0]), (0.0, [2.0, 1.3, 1.0]), (1.0, [0.0, 1.2, -0.5]) ], ["label", "features"]) rf = RandomForestClassifier(numTrees=10) model = rf.fit(data) print(model.featureImportances)
Attempts:
2 left
💡 Hint
Check the type and properties of featureImportances in Spark ML RandomForest model.
✗ Incorrect
The featureImportances attribute returns a DenseVector with scores for each feature that sum to 1.0.
🚀 Application
expertChoosing the Best Model Based on Evaluation Metrics
You trained both LogisticRegression and RandomForest models on the same dataset. The LogisticRegression model has an accuracy of 0.85 and AUC of 0.88. The RandomForest model has an accuracy of 0.83 and AUC of 0.90. Which model should you choose if your priority is to maximize the ability to rank positive cases higher than negative ones?
Attempts:
2 left
💡 Hint
AUC measures ranking quality, accuracy measures overall correctness.
✗ Incorrect
AUC (Area Under ROC Curve) measures how well the model ranks positive cases higher than negative ones. Since RandomForest has higher AUC, it is better for ranking.
