Challenge - 5 Problems
Feature Transformer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of VectorAssembler on simple DataFrame
Given the following Spark DataFrame and VectorAssembler code, what is the output of the transformed DataFrame's 'features' column?
Apache Spark
from pyspark.sql import SparkSession from pyspark.ml.feature import VectorAssembler spark = SparkSession.builder.getOrCreate() data = [(1, 2.0, 3.0), (4, 5.0, 6.0)] columns = ['id', 'f1', 'f2'] df = spark.createDataFrame(data, columns) assembler = VectorAssembler(inputCols=['f1', 'f2'], outputCol='features') output = assembler.transform(df).select('features').collect()
Attempts:
2 left
💡 Hint
VectorAssembler combines specified columns into a single vector column in the order given.
✗ Incorrect
VectorAssembler takes the columns 'f1' and 'f2' and combines them into a vector. The 'id' column is not included. So the output vectors are [2.0, 3.0] and [5.0, 6.0].
❓ data_output
intermediateEffect of StandardScaler on feature vectors
After applying StandardScaler with mean centering and scaling to unit variance on the following feature vectors, what is the resulting scaled vector for the first row?
Apache Spark
from pyspark.ml.feature import StandardScaler from pyspark.ml.linalg import Vectors from pyspark.sql import SparkSession spark = SparkSession.builder.getOrCreate() data = [(Vectors.dense([1.0, 0.0, -1.0]),), (Vectors.dense([2.0, 1.0, 1.0]),)] df = spark.createDataFrame(data, ['features']) scaler = StandardScaler(inputCol='features', outputCol='scaledFeatures', withMean=True, withStd=True) scalerModel = scaler.fit(df) scaledData = scalerModel.transform(df).select('scaledFeatures').collect()
Attempts:
2 left
💡 Hint
StandardScaler subtracts the mean and divides by standard deviation for each feature.
✗ Incorrect
The mean vector is [1.5, 0.5, 0.0] and std deviation vector is [0.7071, 0.7071, 1.4142] (sample std dev, divide by n-1). The first vector scaled is ((1-1.5)/0.7071, (0-0.5)/0.7071, (-1-0)/1.4142) ≈ (-0.7071, -0.7071, -0.7071).
🔧 Debug
advancedIdentify the error in VectorAssembler usage
What error will this Spark code raise when trying to assemble features?
Apache Spark
from pyspark.ml.feature import VectorAssembler assembler = VectorAssembler(inputCols=['a', 'b'], outputCol='features') data = [(1, 2), (3, 4)] df = spark.createDataFrame(data, ['a', 'c']) assembler.transform(df).show()
Attempts:
2 left
💡 Hint
Check if all inputCols exist in the DataFrame.
✗ Incorrect
The DataFrame has columns 'a' and 'c', but VectorAssembler expects 'a' and 'b'. Missing 'b' causes AnalysisException.
🚀 Application
advancedChoosing correct parameters for StandardScaler
You have a dataset with features on very different scales and some features centered around zero. Which StandardScaler parameters should you use to ensure all features have zero mean and unit variance?
Attempts:
2 left
💡 Hint
Zero mean means centering, unit variance means scaling by std deviation.
✗ Incorrect
Setting withMean=True centers data to zero mean. Setting withStd=True scales data to unit variance. Both are needed for zero mean and unit variance.
🧠 Conceptual
expertUnderstanding VectorAssembler output schema
After applying VectorAssembler on a DataFrame with columns ['x', 'y', 'z'], what is the data type of the output column 'features'?
Attempts:
2 left
💡 Hint
VectorAssembler outputs a single vector column compatible with MLlib.
✗ Incorrect
VectorAssembler outputs a column of type VectorUDT, which is Spark MLlib's vector type used for machine learning pipelines.
