Challenge - 5 Problems
Spark ML Pipeline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a simple Spark ML Pipeline
What is the output of the following Spark ML Pipeline code snippet when applied to the given DataFrame?
Apache Spark
from pyspark.ml import Pipeline from pyspark.ml.feature import Tokenizer, HashingTF from pyspark.sql import SparkSession spark = SparkSession.builder.getOrCreate() data = spark.createDataFrame([ (0, "I love Spark"), (1, "Spark is great"), (2, "I enjoy learning ML") ], ["id", "sentence"]) tokenizer = Tokenizer(inputCol="sentence", outputCol="words") hashingTF = HashingTF(inputCol="words", outputCol="features", numFeatures=10) pipeline = Pipeline(stages=[tokenizer, hashingTF]) model = pipeline.fit(data) result = model.transform(data).select("id", "features").collect()
Attempts:
2 left
💡 Hint
Focus on how Tokenizer splits sentences and how HashingTF maps words to feature indices.
✗ Incorrect
Tokenizer splits each sentence into words. HashingTF converts these words into a sparse vector of fixed length 10 using a hash function. The indices and values in the SparseVector correspond to hashed word features.
❓ data_output
intermediateNumber of stages in a Spark ML Pipeline
Given the following pipeline construction code, how many stages does the pipeline contain?
Apache Spark
from pyspark.ml.feature import StringIndexer, VectorAssembler from pyspark.ml.classification import LogisticRegression from pyspark.ml import Pipeline indexer = StringIndexer(inputCol="category", outputCol="categoryIndex") assembler = VectorAssembler(inputCols=["feature1", "feature2", "categoryIndex"], outputCol="features") lr = LogisticRegression(featuresCol="features", labelCol="label") pipeline = Pipeline(stages=[indexer, assembler, lr]) num_stages = len(pipeline.getStages())
Attempts:
2 left
💡 Hint
Count each transformer and estimator added to the stages list.
✗ Incorrect
The pipeline has three stages: StringIndexer, VectorAssembler, and LogisticRegression. Each is a stage in the pipeline.
🔧 Debug
advancedIdentify the error in this Spark ML Pipeline code
What error will this Spark ML Pipeline code raise when executed?
Apache Spark
from pyspark.ml.feature import Tokenizer, HashingTF from pyspark.ml import Pipeline spark = SparkSession.builder.getOrCreate() tokenizer = Tokenizer(inputCol="text", outputCol="words") hashingTF = HashingTF(inputCol="words", outputCol="features") pipeline = Pipeline(stages=[tokenizer, hashingTF]) # DataFrame missing 'text' column data = spark.createDataFrame([(0, "hello world")], ["id", "sentence"]) model = pipeline.fit(data)
Attempts:
2 left
💡 Hint
Check if the input column names in the pipeline match the DataFrame columns.
✗ Incorrect
The Tokenizer expects an input column named 'text', but the DataFrame has 'sentence'. This mismatch causes an AnalysisException.
🚀 Application
advancedChoosing the correct pipeline stage order
You want to build a Spark ML Pipeline to classify text data. Which pipeline stage order is correct?
Attempts:
2 left
💡 Hint
Think about the data flow: raw text to tokens, tokens to features, features to model.
✗ Incorrect
The correct order is to first tokenize the text, then convert tokens to features, and finally apply the classifier.
🧠 Conceptual
expertEffect of caching in Spark ML Pipelines
What is the main benefit of caching intermediate DataFrames in a Spark ML Pipeline during iterative model tuning?
Attempts:
2 left
💡 Hint
Consider how caching affects repeated computations in Spark.
✗ Incorrect
Caching stores intermediate DataFrames in memory, so repeated computations during tuning reuse these results, speeding up processing.
