DATABRICKS-MACHINE-LEARNING-ASSOCIATE practice question 5 of 15

Which of the Spark operations can be used to randomly split a Spark DataFrame into a training DataFrame and a test DataFrame for downstream use?

Full set: 74 questions
Answer options
A: TrainValidationSplit
B: DataFrame.where
C: CrossValidator
D: TrainValidationSplitModel
E: DataFrame.randomSplit Correct answer
Correct Answer:
E. DataFrame.randomSplit
Explanation

The DataFrame.randomSplit() method is the designated Spark operation for randomly partitioning a DataFrame into multiple, non-overlapping subsets. It accepts a list of weights that define the approximate proportions for each split and an optional seed for reproducibility. This function is the standard and most direct approach for creating training and test DataFrames from a single source DataFrame in a Spark-based machine learning workflow. The resulting splits are returned as a list of DataFrames.

Why Incorrect

A. TrainValidationSplit is a high-level hyperparameter tuning class that internally performs a single split, but it is not the fundamental DataFrame operation itself.
B. DataFrame.where is a filtering operation used to select rows based on a specific condition; it does not perform a random split.
C. CrossValidator is a hyperparameter tuning class that performs k-fold cross-validation, creating multiple splits, not a single train/test split operation.
D. TrainValidationSplitModel is the object returned after fitting a TrainValidationSplit estimator; it is a result, not an operation for splitting data.

References

1. Apache Spark Official Documentation. The PySpark API reference for pyspark.sql.DataFrame.randomSplit explicitly states its purpose: "Randomly splits this DataFrame with the provided weights." It provides a clear example of creating two splits.

Source: Apache Spark 3.5.0 Documentation

pyspark.sql.DataFrame.randomSplit.

2. Databricks Official Documentation. The documentation on preparing data for machine learning demonstrates this exact use case. It shows an example: traindf

testdf = originaldf.randomSplit([.8

.2]

seed=42)

directly confirming its use for creating training and test sets.

Source: Databricks Documentation > Machine Learning > Get started > Tutorial: ML end-to-end on Databricks > Step 2: Prepare the data.

3. University Courseware. Reputable data science courses that use Spark consistently teach randomSplit for this purpose. For example

materials from UC Berkeley's Data 100 course show randomSplit as the standard method for creating training and test sets in PySpark.

Source: University of California

Berkeley

Data 100: Principles & Techniques of Data Science

Course Materials (e.g.

Fall 2022

Lecture 20 on Parallel Processing).

Scroll to Top