Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Spark session needed for MLlib.
Apache Spark
from pyspark.sql import SparkSession spark = SparkSession.builder.appName([1]).getOrCreate()
Attempts:
3 left
💡 Hint
Common Mistakes
Not putting quotes around the app name string.
Using a variable name without quotes.
✗ Incorrect
The appName must be a string, so it needs quotes around it.
2fill in blank
mediumComplete the code to load data as a DataFrame from a CSV file.
Apache Spark
df = spark.read.format([1]).option("header", "true").load("data.csv")
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong format like "json" or "parquet" for CSV files.
✗ Incorrect
To read a CSV file, the format must be set to "csv".
3fill in blank
hardFix the error in the code to create a VectorAssembler for features.
Apache Spark
from pyspark.ml.feature import VectorAssembler assembler = VectorAssembler(inputCols=[1], outputCol="features")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single string with commas instead of a list.
Passing unquoted column names.
✗ Incorrect
inputCols expects a list of column names as strings, so use a Python list with double quotes.
4fill in blank
hardFill both blanks to train a LogisticRegression model with features and label.
Apache Spark
from pyspark.ml.classification import LogisticRegression lr = LogisticRegression(featuresCol=[1], labelCol=[2]) model = lr.fit(trainingData)
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up featuresCol and labelCol names.
Using column names not present in the DataFrame.
✗ Incorrect
featuresCol should be "features" and labelCol should be "label" to match the DataFrame columns.
5fill in blank
hardFill all three blanks to create a pipeline with VectorAssembler and LogisticRegression, then fit it.
Apache Spark
from pyspark.ml import Pipeline assembler = VectorAssembler(inputCols=[1], outputCol=[2]) lr = LogisticRegression(featuresCol=[3], labelCol="label") pipeline = Pipeline(stages=[assembler, lr]) model = pipeline.fit(trainingData)
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for outputCol and featuresCol.
Passing a string instead of a list for inputCols.
✗ Incorrect
inputCols needs a list of columns, outputCol and featuresCol must be the same string "features".
