Spark ML pipelines, estimators, and transformers
Databricks Machine Learning Professional · Lesson 1 of 22 · 3:37
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Machine Learning Professional exam. In this lesson: when Spark ML fits and when a single-node library is simpler, estimators and transformers (StringIndexer, OneHotEncoder, VectorAssembler, Imputer, and StandardScaler), matching stages to a use case, and building a pipeline that fits into a pipeline model. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Too big for one machine
Pebblecove Travel keeps years of booking history, hundreds of millions of rows, and the data team wants to predict which new bookings will be cancelled. The training data does not fit in the memory of a single machine, so the team turns to Spark ML, which trains across the whole cluster. This lesson covers when Spark ML is the right choice, estimators and transformers, and how to build a pipeline.
When Spark ML fits
Spark ML is the pyspark dot ml package of Apache Spark MLlib, and it trains models on data that is spread across the nodes of a cluster. It fits when the training data is too large for the memory of one machine, when the data already lives in Spark tables, and when the algorithm you need, like logistic regression, random forests, or gradient boosted trees, exists in the library. It fits less when the data sits comfortably on one machine, where scikit-learn or XGBoost is simpler and often faster, or when the model is a deep neural network, which usually trains with a deep learning framework. It runs on serverless, standard, and dedicated compute. And a common pattern mixes both: Spark prepares the data at scale, and a single node library trains on a sample, when a sample is enough.
Estimators and transformers
Spark ML builds models from stages of two kinds. A transformer has a transform method, which takes a DataFrame and returns a new one, usually with a column added: VectorAssembler, which combines feature columns into one vector column, is a transformer, and so is every fitted model. An estimator has a fit method, which learns from a DataFrame and returns a transformer: StringIndexer learns the categories of a column, Imputer learns a mean or a median, StandardScaler learns a mean and a deviation, and LogisticRegression learns its coefficients. So if a step must learn something from the data, it is an estimator, and if it only applies a fixed rule, it is a transformer.
Matching stages to a use case
For a use case, match each need to a stage. A text category, like the property type, needs StringIndexer, and then OneHotEncoder when the model is linear, while tree models can read the index directly. Numeric columns with gaps need Imputer, and linear or distance based models benefit from StandardScaler. Every algorithm then reads one vector column, built by VectorAssembler, and the target decides the algorithm: a classifier for cancelled or not, and a regressor for a nightly price.
Building a pipeline
A Pipeline chains the stages in order, and it is an estimator itself. Calling fit on the training DataFrame runs the stages one by one: each estimator is fitted, and its transformer passes the data on to the next stage. The result is a PipelineModel, a transformer that holds every fitted stage, so transform applies the same indexing, encoding, and model to new data, with no gap between training and scoring. Fit the pipeline on the training split only, so that statistics like a mean or the list of categories do not leak from the test data. And log the PipelineModel with mlflow dot spark dot log_model, so that a scoring job loads the whole chain at once.
Recap
Let's recap. Choose Spark ML when the data is too large for one machine, already lives in Spark, and the algorithm exists in the library, and choose a single node library otherwise. A transformer maps a DataFrame to a new one, and an estimator learns with fit and returns a transformer. StringIndexer, OneHotEncoder for linear models, Imputer, StandardScaler, and VectorAssembler cover most preparation needs. A Pipeline is an estimator, and its PipelineModel applies every fitted stage to new data. And fit on the training split only, then log the PipelineModel with MLflow.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
The team needs a model for 80,000 host reviews, which fit easily in memory, and the algorithm they want is not in Spark ML. What fits best?
- Spark ML on the full cluster
- A bigger Spark cluster for the job
- A single node library on one machine
- A Ray cluster across eight nodes
Show the answer
A single node library on one machine
The data fits on one machine and the algorithm is missing from Spark ML, so a single node library is the simple choice. “Spark ML on the full cluster” would add the cost of distribution for no gain, and it still lacks the algorithm. “A bigger Spark cluster for the job” only changes the size of the cluster, which does not add the algorithm. And “A Ray cluster across eight nodes” would distribute a task that one machine handles.
Which of these Spark ML stages is an estimator?
- VectorAssembler
- StringIndexer
- A fitted LogisticRegressionModel
- SQLTransformer
Show the answer
StringIndexer
StringIndexer must learn the categories of its column, so it has a fit method that returns a StringIndexerModel. “VectorAssembler” only combines columns with a fixed rule, so it is a transformer. “A fitted LogisticRegressionModel” is a model that is already fitted, and fitted models are transformers. And “SQLTransformer” only runs a SQL statement on the DataFrame, which learns nothing.
A fitted pipeline scores next month’s bookings, and a country appears that its StringIndexer never saw. With default settings, what happens, and what keeps these rows?
- An error · set handleInvalid to keep
- Index zero · nothing to change
- The pipeline refits · nothing to change
- Rows dropped · set it to error
Show the answer
An error · set handleInvalid to keep
By default, StringIndexer raises an error on an unseen label, and setting handleInvalid to keep puts such labels in an extra index. “Index zero · nothing to change” is wrong, since no label gets an index silently by default. “The pipeline refits · nothing to change” is wrong, since a PipelineModel never refits itself during transform. And “Rows dropped · set it to error” is wrong twice: dropping the rows is what the skip setting does, and error is the default that fails.