Which feature of Spark MLlib primarily allows it to process large datasets efficiently?
Think about how big data is handled across multiple computers.
Spark MLlib uses distributed computing, which means it splits data across many machines to process in parallel, enabling efficient handling of large datasets.
What is the output of the following Spark MLlib code snippet?
from pyspark.sql import SparkSession from pyspark.ml.feature import VectorAssembler spark = SparkSession.builder.appName('test').getOrCreate() data = [(1, 2.0, 3.0), (2, 5.0, 6.0)] df = spark.createDataFrame(data, ['id', 'feature1', 'feature2']) assembler = VectorAssembler(inputCols=['feature1', 'feature2'], outputCol='features') output = assembler.transform(df) output.select('id', 'features').collect()
VectorAssembler creates a DenseVector column combining features.
The VectorAssembler combines specified columns into a single DenseVector column named 'features'. The output shows each id with its feature vector.
Given a Spark DataFrame with 100 partitions, after applying an MLlib transformation that does not change partitioning, how many partitions will the resulting DataFrame have?
Consider if transformations change partition count by default.
Most MLlib transformations preserve the number of partitions unless explicitly changed, so the output DataFrame keeps 100 partitions.
What error will this Spark MLlib pipeline code raise?
from pyspark.sql import SparkSession from pyspark.ml import Pipeline from pyspark.ml.feature import StringIndexer, VectorAssembler spark = SparkSession.builder.appName('test').getOrCreate() stages = [StringIndexer(inputCol='category', outputCol='categoryIndex'), VectorAssembler(inputCols=['categoryIndex', 'value'], outputCol='features')] pipeline = Pipeline(stages=stages) data = [(0, 'a', 1.0), (1, 'b', 2.0)] df = spark.createDataFrame(data, ['id', 'category', 'value']) model = pipeline.fit(df) result = model.transform(df) result.show()
Check if SparkSession is created before using it.
The code creates a SparkSession named 'spark', so no NameError occurs. The pipeline stages are valid, so the code runs successfully.
You want to train a machine learning model on a dataset too large for one machine's memory. Which Spark MLlib feature helps you train the model efficiently at scale?
Think about how Spark handles big data and ML training.
Spark MLlib uses distributed DataFrames and pipelines to split data and computation across many machines, enabling scalable model training.
