Scikit-learn Interview Questions

Dan Lee's profile image
Dan LeeData & AI Lead
Last update
Scikit-learn Interview Questions

Scikit-learn interviews are rarely about remembering which class does what. They show you a notebook with a 0.95 cross-validation score and ask why production shows 0.70, or a custom transformer that breaks inside GridSearchCV. Data scientists, machine learning engineers and applied scientists face these questions because scikit-learn is still the default toolkit for tabular modeling.

Candidates slip in a few predictable places: preprocessing fit on the full dataset, splits that ignore groups or time, predict used where a probability or a tuned threshold belongs, and defaults such as C=1.0 or impurity-based importances taken on faith. Each of these looks harmless in a code review.

The 30 questions below cover the estimator API, pipelines and leakage, cross-validation and search, metrics and calibration, and model-specific behavior including custom transformers and persistence. Answers were checked against scikit-learn 1.9, with short code you can run. A preparation plan, a quiz and an FAQ follow the questions.

Intermediate30 questions

Scikit-learn Interview Questions

Practice questions on scikit-learn's estimator API, leak-free pipelines, cross-validation, metrics and calibration, with answers checked against scikit-learn 1.9.

Data ScientistMachine Learning EngineerApplied ScientistData AnalystQuantitative Researcher

The Estimator API

Every scikit-learn object follows one contract. Hyperparameters go in the constructor, fit learns from data and stores the result in attributes ending in an underscore, and predict or transform applies what was learned. Interviewers probe this because most pipeline bugs, from leakage to broken grid searches, violate it somewhere.

The idea that matters most is the split between parameters and fitted state. clone, get_params and every search tool depend on copying an estimator's parameters without its fitted state, so code that blurs the two fails in confusing ways.

The Estimator API

A scoring script calls scaler.fit_transform(X_new) on each incoming batch "because it is the same as fit and then transform". Predictions drift badly. What is wrong?

EasyThe Estimator API

Sample Answer

fit learns parameters from whatever data it receives, here mean_ and scale_, and transform applies them. fit_transform does both, so calling it at scoring time re-learns the statistics from the new batch. The new features are standardized differently from the training features the model saw, and a one-row batch even comes back as all zeros. Fit once on training data, persist the fitted scaler with the model (ideally as one Pipeline), and call only transform or predict at scoring time. Also note that fit_transform equals fit(X).transform(X) for most transformers but not all; TargetEncoder differs on purpose.

Python
1import numpy as np
2from sklearn.preprocessing import StandardScaler
3X_train, X_new = np.array([[1.0], [2.0], [3.0]]), np.array([[10.0], [11.0]])
4scaler = StandardScaler().fit(X_train)
5print(scaler.transform(X_new).ravel())                # [ 9.8  11.02]: training statistics
6print(StandardScaler().fit_transform(X_new).ravel())  # [-1.  1.]: refit on the new batch
Learn this in the Advanced Machine Learning course

Pipelines, ColumnTransformer and Leakage

Leakage is the most common reason a model that looked great offline disappoints in production, and scikit-learn interviews test it directly. Expect to be shown preprocessing code and asked whether its cross-validation score can be trusted.

The one idea to hold onto: anything that learns from data, whether a scaler, an imputer, an encoder or a feature selector, must be fit inside each training fold. Putting every such step in a Pipeline and cross-validating the whole pipeline guarantees that.

Pipelines, ColumnTransformer and Leakage

On a dataset with 100 rows and 10,000 random features, a teammate keeps the 20 features most associated with the label, then cross-validates a logistic regression and reports 90% accuracy. The labels are coin flips. What happened?

MediumPipelines, ColumnTransformer and Leakage

Sample Answer

Feature selection saw the labels of every row, including the rows later used as validation folds. Among 10,000 noise features, some correlate with the labels by chance, and the selector picks exactly those, so the validation folds are no longer unseen. Put the selector inside a Pipeline so it is refit on each training fold, and the score falls to chance. Scaling the full dataset before splitting also leaks, though only a little. Feature selection and target encoding leak a lot. The working rule: every step that learns anything in fit belongs inside the cross-validated pipeline.

Python
1import numpy as np
2from sklearn.feature_selection import SelectKBest, f_classif
3from sklearn.linear_model import LogisticRegression
4from sklearn.model_selection import cross_val_score
5from sklearn.pipeline import make_pipeline
6rng = np.random.RandomState(0)
7X, y = rng.randn(100, 10_000), rng.randint(0, 2, 100)
8X_leaky = SelectKBest(f_classif, k=20).fit_transform(X, y)
9print(cross_val_score(LogisticRegression(), X_leaky, y).mean())   # about 0.90
10pipe = make_pipeline(SelectKBest(f_classif, k=20), LogisticRegression())
11print(cross_val_score(pipe, X, y).mean())                         # about 0.57
Learn this in the Advanced Machine Learning course

A cross-validation score is only as good as the split behind it. Interviewers check whether your splits mirror how the model will be used, with groups kept together and time respected, and whether you know how far a tuned score can be trusted.

The key idea is that validation data must be as unseen as production data. If the same user, the same period or the same tuning loop touches both sides of a split, the score is optimistic.

You call cross_val_score(model, X, y, cv=5) on a classifier and on a regressor. What splits does each one get, and when does the default bite you?

EasyCross-Validation and Hyperparameter Search

Sample Answer

With an integer cv, classifiers get StratifiedKFold(5) and everything else gets KFold(5), and neither shuffles by default. Stratification keeps class proportions similar across folds, which matters for rare classes. No shuffling means each fold is a contiguous block of rows. If the data is sorted by date, label or customer, each fold covers a different slice and scores swing between folds. Pass KFold(5, shuffle=True, random_state=0) or StratifiedKFold with the same arguments when rows are exchangeable. When they are not, pass a group or time-aware splitter; shuffling time-ordered data hides leakage instead of fixing it.

Learn this in the Advanced Machine Learning course

Metrics, Class Imbalance and Calibration

Most real classification problems are imbalanced, and scikit-learn's default outputs, accuracy and predict at a 0.5 cutoff, are rarely what the business needs. Interviewers check whether you separate ranking quality, decision thresholds and probability quality.

Keep those apart. ROC AUC and average precision judge ranking, the threshold turns scores into decisions based on costs, and calibration decides whether a predicted 0.2 really means a 20% chance. Many mistakes come from fixing one of them with a tool built for another.

Metrics, Class Imbalance and Calibration

After a refactor, a fraud model's reported ROC AUC drops from 0.97 to 0.94, although the model did not change. The new code calls roc_auc_score(y, model.predict(X)). What is wrong?

EasyMetrics, Class Imbalance and Calibration

Sample Answer

ROC AUC measures how well scores rank positives above negatives across all thresholds, so it needs continuous scores. predict returns hard 0 or 1 labels at the default cutoff, which collapses the ROC curve to a single operating point and usually lowers the number. Pass model.predict_proba(X)[:, 1], or decision_function(X) for models without probabilities. The same applies to average_precision_score and log_loss. A scorer string such as scoring='roc_auc' in cross_val_score picks the right method for you, which is one reason to prefer scorer strings over hand-written metric calls.

Python
1from sklearn.datasets import make_classification
2from sklearn.linear_model import LogisticRegression
3from sklearn.metrics import roc_auc_score
4X, y = make_classification(2000, weights=[0.95], random_state=0)
5model = LogisticRegression().fit(X, y)
6print(roc_auc_score(y, model.predict(X)))              # about 0.94: one threshold
7print(roc_auc_score(y, model.predict_proba(X)[:, 1]))  # about 0.97: full ranking
Learn this in the Advanced Machine Learning course

Models, Custom Transformers and Persistence

The last group covers using specific estimators well and shipping them: defaults that change results, the right way to extend scikit-learn with your own transformer, and how a fitted pipeline reaches production.

The common thread is that defaults are decisions. C=1.0 in logistic regression, impurity-based importances in random forests and pickle as a storage format each make a choice for you, and interviewers expect you to question it.

Models, Custom Transformers and Persistence

A logistic regression on raw features raises a ConvergenceWarning, and its coefficients change noticeably when one feature is converted from dollars to thousands of dollars. Explain both, and say how you would configure the model in scikit-learn 1.9.

MediumModels, Custom Transformers and Persistence

Sample Answer

LogisticRegression is regularized by default with an L2 penalty and C=1.0, where smaller C means stronger regularization. The penalty treats every coefficient alike, so a feature's units change how much it is shrunk. Unscaled features also make the optimization badly conditioned, and the default lbfgs solver stops at max_iter=100 with a warning. On the breast cancer dataset it hits that limit unscaled and converges in about 19 iterations after StandardScaler. Put the scaler in a pipeline and tune C. Since 1.8 the penalty argument is deprecated: use l1_ratio (0 for L2, 1 for L1, in between for elastic net) and C=np.inf for no penalty. L1 needs solver='saga' or 'liblinear'.

Learn this in the Advanced Machine Learning course

How to Prepare for Scikit-learn Interviews

Cross-validate the whole pipeline, every time

Make it a reflex: preprocessing, feature selection and the model live in one Pipeline, and only that object goes into cross_val_score or a search. Then practice choosing the splitter, grouped or time-ordered, before you write any model code. Cross-validation done right works through the leakage cases interviewers like to plant.

Separate ranking, thresholds and probabilities

For any classifier, be able to say which metric judges its ranking, how you would pick the threshold from error costs, and how you would check calibration. Practice with TunedThresholdClassifierCV and CalibratedClassifierCV on the same dataset. The lessons Choosing a threshold with costs and Calibration cover both.

Work one imbalanced problem end to end

Take a dataset with a 1 to 2 percent positive rate and compare class weights, resampling and threshold tuning on average precision and expected cost. Interviewers often ask which you would choose and why, and having real numbers helps. See Imbalanced data and the case study Diagnose a fraud model.

Know the tree ensemble defaults

Learn what max_features, min_samples_leaf, max_bins and early_stopping default to and why, and how impurity-based importance differs from permutation importance. These come up in nearly every applied ML loop. Use Random forests, Tuning boosted trees and Feature importance done right.

Write an estimator from scratch

Implement a small transformer and a simple model that follow the scikit-learn contract, and run check_estimator on them. It is the fastest way to internalize clone, fitted attributes and set_output, and some interviews ask for it directly. Implement from scratch and Logistic regression internals are good practice.

How Ready Are You for Scikit-learn Interviews?

1 / 6
Estimator API

Your custom estimator's __init__ converts cols with list(cols). What happens inside GridSearchCV?

Frequently Asked Questions

Is scikit-learn asked in data science interviews?

Yes, often indirectly. Take-home assignments and live modeling rounds for data scientist and machine learning engineer roles usually expect scikit-learn, and interviewers read your code for pipelines, correct cross-validation and sensible metrics. Conceptual rounds ask about leakage, class imbalance, calibration and tree ensembles, which are easiest to explain with scikit-learn's API in mind. Product analytics roles test it less, but a take-home model is still common.

How long does it take to prepare for scikit-learn interview questions?

If you already build models with scikit-learn, one to two weeks of focused practice covers the API contract, pipelines, splitters, threshold tuning and calibration. If you mostly know the theory, plan on three to four weeks and write code every day: build a leak-free pipeline, a grouped or time-based validation, and a custom transformer. Interviewers notice quickly whether you have actually run these tools.

What level of scikit-learn do I need for a data scientist interview?

You should build a Pipeline with a ColumnTransformer from memory, choose and justify a cross-validation splitter, tune hyperparameters without leaking, and pick metrics and thresholds for imbalanced data. Senior roles add calibration, nested cross-validation, permutation importance and writing custom estimators. You do not need to memorize every class, but you should know the defaults that change results, such as regularization in LogisticRegression.

Do I need scikit-learn if I mostly use XGBoost, LightGBM or PyTorch?

Yes. Even when the model comes from another library, scikit-learn usually supplies the splitters, pipelines, metrics, calibration and search tools around it, and XGBoost and LightGBM both ship scikit-learn compatible estimators. Interviewers also use scikit-learn as a shared vocabulary for questions about leakage and validation. Knowing its contract makes it easy to plug any model into a correct evaluation setup.

Will I have to write scikit-learn code live in an interview?

Often. Common formats are a take-home with a notebook you defend afterwards, a live session where you build and validate a model on a small dataset, and a code review where you find the leakage or metric bug in someone else's script. A few interviews ask you to implement a simple estimator from scratch with fit and predict. Practice typing pipelines without documentation.

Which scikit-learn version should I study in 2026?

Study the current 1.x behavior, 1.9 as of late 2026. Recent changes worth knowing are TunedThresholdClassifierCV for threshold tuning, FrozenEstimator replacing cv='prefit' in calibration, the deprecation of penalty in LogisticRegression in favor of l1_ratio, set_output for pandas and polars output, and metadata routing for sample_weight and groups. Answers built on removed options stand out to interviewers.

Dan Lee's profile image

Written by

Dan Lee

Data & AI Lead

Dan is a seasoned data scientist and ML coach with 10+ years of experience at Google, PayPal, and startups. He has helped candidates land top-paying roles and offers personalized guidance to accelerate your data career.

Connect on LinkedIn