QuestionQ6

VerifiedUsing Spark ML

A Data Scientist is creating a pipeline to train multiple machine-learning models. They have a dataframe containing a group column, many feature columns, and one label column.

Their requirements are:

  • An independent model for each group
  • Direct use of the data in the columns without transformation
  • Flexibility to select from many different model types
  • Efficient distributed-computing capabilities

They are deciding between the pyspark.ml library and pandas UDFs through groupby().ApplyInPandas() to train the models. They choose the groupBy().ApplyInPandas() approach.

What is a valid reason for this choice?

Explanation

GroupedData.applyInPandas() applies a Python function to each group using a pandas DataFrame and combines the returned pandas DataFrames into a Spark DataFrame. The training function can therefore invoke model-training libraries available in Python, such as scikit-learn or PyTorch. Its output must be a pandas DataFrame matching the declared schema; it cannot return arbitrary custom model objects. Spark also warns that group data can be loaded into memory, creating an out-of-memory risk for large or skewed groups.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!