Estimators, transformers, pipelines, and imbalance
Databricks Machine Learning Associate · Lesson 12 of 19 · 1:38:15
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 Associate exam. In this lesson: Spark ML transformers and estimators, pipelines and pipeline models, imbalanced data, and mitigation methods: class weights, oversampling, undersampling, and decision thresholds. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Ninety eight percent, and useless
Juniper Rides wants to flag the trips likely to end with a damaged scooter, but only two trips in a hundred do. The first model reaches ninety eight percent accuracy, and never flags a single trip, while its preprocessing is a list of notebook cells, run by hand, and slightly differently at scoring time. This lesson covers transformers and estimators, how pipelines chain them, and how to handle imbalanced data.
Transformers and estimators
Spark ML builds on two kinds of objects. A transformer has a transform method, which takes a DataFrame and returns a new one, usually with an added column, like the VectorAssembler, or a trained model that adds predictions. An estimator has a fit method, which learns from a DataFrame and returns a transformer: a LogisticRegression, a StringIndexer, or an Imputer is an estimator. So fitting a LogisticRegression returns a LogisticRegressionModel, a transformer whose transform method adds the prediction and probability columns. Scikit-learn follows the same idea, with fit, transform, and predict methods.
Pipelines
A Pipeline chains the steps of a workflow, its stages, in order, like a StringIndexer, a OneHotEncoder, a VectorAssembler, and a classifier. The Pipeline is itself an estimator: fit runs the stages in order on the training data, fitting each estimator, and transforming the data for the next stage. It returns a PipelineModel, a transformer that applies every fitted stage, so the same steps run at training and at inference. Fitting the whole pipeline on the training data only avoids leakage, and logging the PipelineModel with MLflow keeps the preprocessing and the model together. In scikit-learn, a Pipeline plays the same role, with fit and predict on the whole chain.
Imbalanced data
Data is imbalanced when one class is rare, like the two trips in a hundred that end with a damaged scooter. Accuracy then misleads: a model that always predicts no damage is ninety eight percent accurate, and useless. So watch precision, recall, F1, or the area under the precision recall curve, which focus on the rare class. And split with stratification, so that the training and test sets keep the same class proportions. In scikit-learn, the stratify parameter of train_test_split does this.
Mitigation methods
Class weights make the rare class count more during training: class_weight set to balanced, in scikit-learn, weights each class by the inverse of its frequency. In Spark ML, many classifiers take a weightCol, a column with a weight for each row, like a higher weight for the damaged trips. Undersampling drops part of the majority class, which is fast, but discards data. Oversampling repeats rows of the minority class, or creates synthetic ones, as SMOTE does, and it must touch only the training data, after the split. And lowering the decision threshold, below one half, flags more of the rare class, which trades precision for recall.
Recap
Let's recap. A transformer turns a DataFrame into another with transform, and an estimator learns with fit, and returns a transformer. A Pipeline is an estimator that chains stages, and its fit method returns a PipelineModel, which applies the same steps at inference. With imbalanced data, accuracy misleads, so watch precision, recall, and F1, and split with stratification. Class weights, undersampling, oversampling, and a lower threshold all help the rare class. And resample only the training data, after the split.
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.
Which of these Spark ML objects is a transformer, which works without calling fit?
- StringIndexer
- VectorAssembler
- Imputer
- LogisticRegression
Show the answer
VectorAssembler
The VectorAssembler only combines columns, so it learns nothing, and transform works directly. “StringIndexer” learns the list of values, so it is an estimator. “Imputer” learns the mean, the median, or the mode, so it is an estimator too. And “LogisticRegression” learns coefficients, and its fit method returns the model.
A data scientist calls fit on a Pipeline with four stages, using the training data. What do they get, and how do they score the test set?
- A Pipeline: call fit again on the test set
- Four models: apply each one by hand
- Predictions: they are already in the test set
- A PipelineModel: call transform on the test set
Show the answer
A PipelineModel: call transform on the test set
Fit returns a PipelineModel, and its transform method applies every fitted stage to the test set. “A Pipeline: call fit again on the test set” would fit the stages again, on the test data, which leaks it into the model. “Four models: apply each one by hand” is wrong, since the PipelineModel holds the fitted stages, and applies them in one call. And “Predictions: they are already in the test set” is wrong, since fit returns a model, and the predictions come from transform.
One payment in 200 is fraudulent. A data scientist oversamples the fraud cases, and only then splits the data into training and test sets. What is the problem?
- Oversampling must replace class weights
- The test set needs more oversampling
- Copies of fraud rows reach the test set
- Fraud is too rare to oversample
Show the answer
Copies of fraud rows reach the test set
Resampling before the split puts copies of the same fraud rows in both sets, so the model is tested on rows it has already seen, and the score looks better than it should. “Oversampling must replace class weights” is wrong, since both methods are valid, and they can be combined. “The test set needs more oversampling” is wrong, since the test set should keep the real proportions. And “Fraud is too rare to oversample” is wrong, since rare classes are exactly where oversampling helps.