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
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 statistics6print(StandardScaler().fit_transform(X_new).ravel())# [-1. 1.]: refit on the new batch
Your custom estimator works when you call fit directly, but inside GridSearchCV the grid values have no effect. The constructor does self.alpha = alpha and self.model = Ridge(alpha). A second version stores self.cols = list(cols) and raises Cannot clone object. What rule is being broken?
MediumThe Estimator API
Sample Answer
The constructor rule: __init__ stores each argument unchanged on an attribute with the same name and does nothing else. get_params reads those attributes by name, and clone builds a fresh instance from them, then checks that each parameter is the same object it passed in. list(cols) creates a new object, so clone raises Cannot clone object ..., as the constructor either does not set or modifies parameter cols. Building Ridge(alpha) in __init__ is the quieter bug: set_params(alpha=10) updates self.alpha but not the inner model, so every grid point trains the same thing. Move validation and object construction into fit.
Python
1from sklearn.base import BaseEstimator, clone
2classSelector(BaseEstimator):3def__init__(self, cols=("a",)):4 self.cols =list(cols)# modifies the parameter5try:6 clone(Selector())7except RuntimeError as e:8print(e)# Cannot clone object Selector(cols=['a']), as the constructor ...
You built a pipeline of StandardScaler and LogisticRegression. How do you tune the regularization strength in a grid search, and how would you compare it against a random forest in the same search?
EasyThe Estimator API
Sample Answer
Nested parameters are addressed as <step>__<param>. make_pipeline names each step after its class in lowercase, so the key is logisticregression__C; pipe.get_params() lists every valid key, and a typo raises Invalid parameter. Whole steps are parameters too. Name the steps with Pipeline and pass a list of grids: one tunes C, another swaps the final estimator for a random forest and sets the scaler to 'passthrough', since trees do not need scaling. The search clones the pipeline for every candidate, so the object you passed in stays unfitted. The refit winner lives in search.best_estimator_.
A model trained on a pandas DataFrame is served by a service that builds a NumPy array, and predictions are wrong with no error. Another service sends a DataFrame with the same columns in a different order and gets a ValueError. Explain both.
MediumThe Estimator API
Sample Answer
When fit on a DataFrame with string column names, estimators record feature_names_in_ and n_features_in_. At prediction time a DataFrame is checked against those names, and different names or a different order raise The feature names should match those that were passed during fit. A NumPy array carries no names, so scikit-learn only emits a UserWarning, X does not have valid feature names, and uses columns by position. If the service assembled the array in another order, the model silently reads the wrong features. Send DataFrames, select columns with X[model.feature_names_in_], and turn that warning into an error in your tests.
Two runs of the same random forest pipeline give different cross-validation scores. Where does randomness enter, and how do you make the experiment reproducible?
MediumThe Estimator API
Sample Answer
Randomness enters through every object with a random_state: bootstrap samples and feature subsampling in the forest, shuffling in the splitter, candidate sampling in RandomizedSearchCV. The default random_state=None draws from NumPy's global state, so results change from run to run. Pass an integer to each one. Splitters only shuffle when asked. KFold defaults to shuffle=False, and setting random_state without shuffle=True raises a ValueError because it would have no effect. That default is a trap of its own, since unshuffled folds on data sorted by date or label are skewed. Finally, report the spread across several seeds rather than a single run.
You train a weighted classifier inside a Pipeline and tune it with GridSearchCV. Which parts of the search actually see sample_weight, and how do you control that explicitly?
HardThe Estimator API
Sample Answer
Without metadata routing, which is still off by default in 1.9, weights travel by convention, and the conventions differ. Pipeline.fit rejects a bare sample_weight; you must write clf__sample_weight=w, which reaches that step's fit only. cross_val_score(..., params={'sample_weight': w}) also weights fitting but scores the folds unweighted, so a weighted model gets selected on an unweighted metric. Turn on routing with sklearn.set_config(enable_metadata_routing=True), and every consumer must declare what it wants: set_fit_request(sample_weight=True) on the estimator and set_score_request(sample_weight=True) on the scorer. Metadata that nobody requested then raises UnsetMetadataPassedError instead of being silently dropped.
Python
1import numpy as np, sklearn
2from sklearn.datasets import make_classification
3from sklearn.linear_model import LogisticRegression
4from sklearn.metrics import get_scorer
5from sklearn.model_selection import GridSearchCV
6sklearn.set_config(enable_metadata_routing=True)7X, y = make_classification(500, weights=[0.8], random_state=0)8w = np.where(y ==1,4.0,1.0)9clf = LogisticRegression().set_fit_request(sample_weight=True)10scorer = get_scorer("accuracy").set_score_request(sample_weight=True)11search = GridSearchCV(clf,{"C":[0.1,1,10]}, scoring=scorer, cv=5)12search.fit(X, y, sample_weight=w)# weights reach fit and scoring
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.
Write preprocessing for a DataFrame with numeric columns age and income (both with missing values) and a categorical column city, feeding a logistic regression. What happens to columns you do not list?
EasyPipelines, ColumnTransformer and Leakage
Sample Answer
Use a ColumnTransformer that routes each column group to its own pipeline: impute and scale the numeric columns, one-hot encode the categorical one, and wrap it with the model in a Pipeline. Columns you do not list are dropped, because remainder='drop' is the default. That is a quiet way to lose a feature, so list columns explicitly or set remainder='passthrough'. Output names are prefixed with the transformer name, such as num__age and cat__city_NY, and get_feature_names_out() returns them. To select by dtype instead of by name, use make_column_selector(dtype_include='number').
A model with a OneHotEncoder has scored fine for months. Today it fails with Found unknown categories ['BOS'] in column 0 during transform. How do you make it robust, and what are the tradeoffs?
MediumPipelines, ColumnTransformer and Leakage
Sample Answer
OneHotEncoder defaults to handle_unknown='error', so any category unseen at fit time fails at transform. handle_unknown='ignore' encodes an unknown value as all zeros, which keeps scoring alive but makes a new city look like the dropped or reference case. For long-tailed columns, a better option is min_frequency or max_categories with handle_unknown='infrequent_if_exist': rare training categories are grouped into an infrequent_sklearn column, and unseen values map there too. Whichever you choose, monitor the share of unknown values in production, since a jump usually means an upstream change. For very high cardinality, consider TargetEncoder.
You target-encode a 200-level categorical feature. A colleague calls enc.fit(X_train, y_train) and then enc.transform(X_train) to build the training features. Training metrics look great and test metrics are poor. What went wrong?
HardPipelines, ColumnTransformer and Leakage
Sample Answer
Target encoding replaces each category with a shrunk mean of the target. Computed on the same rows it is applied to, each row's own label leaks into its feature, and rare categories leak the most. TargetEncoder.fit_transform prevents this with cross fitting: it splits the data into cv=5 folds and encodes each fold with statistics learned on the other folds, while fit learns encodings from all rows for use on new data. So fit(X).transform(X) does not equal fit_transform(X). On pure noise labels the leaky version reaches an AUC near 0.88 on its own training rows, while the cross-fitted one stays near 0.5. Inside a Pipeline, fitting calls fit_transform, so this is handled for you.
Python
1import numpy as np, pandas as pd
2from sklearn.metrics import roc_auc_score
3from sklearn.preprocessing import TargetEncoder
4rng = np.random.RandomState(0)5X = pd.DataFrame({"id": rng.randint(0,200,400).astype(str)})6y = rng.randint(0,2,400)# labels are pure noise7leaky = TargetEncoder().fit(X, y).transform(X)8crossfit = TargetEncoder().fit_transform(X, y)9print(roc_auc_score(y, leaky.ravel()))# about 0.8810print(roc_auc_score(y, crossfit.ravel()))# near 0.5
A grid search over a pipeline with an expensive text vectorizer and 20 classifier settings takes hours, because the vectorizer is refit for every candidate. How do you avoid the repeated work?
MediumPipelines, ColumnTransformer and Leakage
Sample Answer
Pass memory to the Pipeline, either a directory path or a joblib.Memory object. Fitted transformers are then cached, keyed on their parameters and input data, so when only clf__C changes, each fold reuses the vectorizer already fitted on that fold. Caching applies to the steps before the final estimator and costs disk space. One gotcha: with caching enabled, the pipeline clones each transformer before fitting, so the instance you passed in is not the fitted one. Inspect fitted steps through pipe.named_steps after fitting. Precomputing features outside the pipeline is faster still, but it is only safe for transformations that learn nothing from the data.
Your house-price model is trained on np.log1p(price), and the team reports an RMSE of 0.21. Product asks what that means in dollars. What would you change?
MediumPipelines, ColumnTransformer and Leakage
Sample Answer
The RMSE was computed in log space, where 0.21 means a typical error of roughly 20 to 25 percent, not a dollar amount, and every caller must remember to apply np.expm1 to predictions. Wrap the model in TransformedTargetRegressor(regressor=..., func=np.log1p, inverse_func=np.expm1). It fits the regressor on the transformed target, and predict returns values in the original units, so cross_val_score and any metric compute errors in dollars. One caveat: back-transforming a prediction of the mean log price gives something close to the median price, so predictions run low for right-skewed prices. Report a relative error such as MAPE next to RMSE.
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.
Cross-Validation and Hyperparameter Search
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.
Your churn dataset has 20 rows per customer. Cross-validated AUC is 0.92, but the first month in production gives 0.71. What is the likely split problem, and how do you fix it?
MediumCross-Validation and Hyperparameter Search
Sample Answer
Rows from the same customer land in both training and validation folds, so the model partly recognizes customers instead of generalizing to new ones, and per-customer features such as plan or region make that easy. Use GroupKFold with groups=customer_id so each customer appears in exactly one validation fold, or StratifiedGroupKFold to also keep the churn rate balanced across folds. Pass the same groups to cross_val_score or to GridSearchCV.fit. Since 1.6, GroupKFold accepts shuffle=True. In the interview, ask whether production scores new customers or known ones, because that decides which split is honest.
Python
1import numpy as np
2from sklearn.model_selection import GroupKFold
3groups = np.repeat(np.arange(10),2)# 10 customers, 2 rows each4X = np.arange(20).reshape(-1,1)5for train, test in GroupKFold(n_splits=5).split(X, groups=groups):6assertnotset(groups[train])&set(groups[test])# no customer on both sides
You predict each day's total demand over the next 7 days from features available that morning. Design the cross-validation.
MediumCross-Validation and Hyperparameter Search
Sample Answer
Use TimeSeriesSplit, which trains on an expanding window of the past and validates on the block that follows, so no fold trains on the future. Set test_size to the length of the evaluation window you care about. Then set gap: each training row's label covers the 7 days after it, so the last 7 training labels overlap the validation period, and gap=7 drops those rows. max_train_size turns the expanding window into a sliding one if old data is stale. The splitter assumes rows are sorted by time with one row per step. With many series per day, build the splits on dates yourself.
Python
1import numpy as np
2from sklearn.model_selection import TimeSeriesSplit
3tss = TimeSeriesSplit(n_splits=3, test_size=3, gap=2)4for train, test in tss.split(np.arange(15)):5print(train.max(), test)# 3 [6 7 8], then 6 [9 10 11], then 9 [12 13 14]
GridSearchCV reports a best_score_ of 0.89 after trying 200 hyperparameter combinations. Your manager wants to put 0.89 in the launch document. What do you tell them?
HardCross-Validation and Hyperparameter Search
Sample Answer
best_score_ is the mean validation score of the winning candidate, and taking the maximum of 200 noisy estimates biases it upward, because the same folds chose the model and scored it. For an honest number, evaluate once on a test set never used in tuning, or run nested cross-validation. In nested CV an outer loop splits the data, a GridSearchCV tunes on inner folds of each outer training set, and the outer folds score the tuned model. In code that is cross_val_score(GridSearchCV(...), X, y, cv=outer). It scores the tuning procedure as a whole rather than one fixed configuration, and it costs the search times the outer folds. Report the spread across outer folds as well.
You need to tune six hyperparameters of a gradient boosting model on a fixed compute budget. Would you use GridSearchCV? What would you use instead?
MediumCross-Validation and Hyperparameter Search
Sample Answer
A grid with 5 values per parameter is 15,625 candidates times the folds, and most of them vary parameters that barely matter. RandomizedSearchCV samples n_iter candidates (10 by default) from distributions, so every trial tries a new value of every parameter and you set the budget directly. Use scipy.stats.loguniform for scale parameters such as the learning rate, and integer distributions for tree sizes. HalvingRandomSearchCV adds successive halving, training many candidates cheaply and promoting the best, but it is still experimental and needs from sklearn.experimental import enable_halving_search_cv. For boosting, tune learning rate and tree size first and let early stopping pick the number of iterations.
You want a grid search to track ROC AUC, average precision and recall, and to choose the model with the best average precision. How do you set it up, and which error do people hit?
MediumCross-Validation and Hyperparameter Search
Sample Answer
Pass scoring as a dict of names to scorers, for example {'auc': 'roc_auc', 'ap': 'average_precision', 'recall': 'recall'}. With several metrics, refit cannot stay at its default True, because the search does not know which metric chooses the winner, so fit raises a ValueError asking for a scorer key or a callable. Set refit='ap'. Then best_params_, best_score_ and best_estimator_ refer to that metric, and cv_results_ holds mean_test_auc, mean_test_ap and mean_test_recall for every candidate. A callable refit can encode rules such as picking the simplest model within one standard error of the best.
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 threshold7print(roc_auc_score(y, model.predict_proba(X)[:,1]))# about 0.97: full ranking
A model trained on data with 1% positives reaches 99% accuracy. Why is that number meaningless, and which metrics would you report instead?
EasyMetrics, Class Imbalance and Calibration
Sample Answer
Predicting the negative class for every row also scores 99%, so accuracy says nothing about finding positives. Report precision and recall for the positive class at the operating threshold, from classification_report or precision_recall_fscore_support, plus a threshold-free ranking metric. ROC AUC can look strong when negatives are plentiful, because the false positive rate stays small even with many false alarms. Average precision (average_precision_score, a summary of the precision-recall curve) is more informative here, and its no-skill baseline equals the positive rate, 0.01, rather than 0.5. Compare against that baseline, and add a cost-based metric when the business can price errors.
You set class_weight='balanced' on a logistic regression for a 90/10 problem. What weights does that produce, what does it change, and what does it break?
MediumMetrics, Class Imbalance and Calibration
Sample Answer
'balanced' weights each class by n_samples / (n_classes * count), so with 90% negatives and 10% positives the weights are about 0.56 and 5.0. Each positive counts nine times as much as a negative in the loss, which moves the decision boundary into the majority class and raises recall at the default cutoff. The cost is calibration. The model now behaves as if positives were as common as negatives, so predict_proba overstates risk. If you need probabilities, train without weights and tune the threshold, or recalibrate afterwards. Class weights and threshold moving often reach similar precision and recall tradeoffs, and a threshold is easier to change after deployment.
A fraud team says a missed fraud costs 500 dollars and a false alarm costs 5 dollars of review time. Your classifier currently uses predict. How do you choose the decision threshold in scikit-learn?
HardMetrics, Class Imbalance and Calibration
Sample Answer
predict on a binary probabilistic classifier cuts predict_proba at 0.5, which is right only when both errors cost the same and probabilities are calibrated. With calibrated probabilities, the cost-minimizing rule flags a case when p exceeds 5 / (5 + 500), about 0.01. In practice, tune it on data. TunedThresholdClassifierCV, added in 1.5, cross-validates the threshold that maximizes a scorer, and a make_scorer cost function makes it minimize expected cost directly. FixedThresholdClassifier wraps a model with a threshold you already chose. Never tune the threshold on the test set, and revisit it when the fraud rate shifts.
Your gradient boosting model ranks customers well, but among customers it scores at 0.8, only 55% convert. Pricing uses these probabilities directly. How do you diagnose and fix the calibration?
HardMetrics, Class Imbalance and Calibration
Sample Answer
Diagnose on held-out data with calibration_curve or CalibrationDisplay, which bin predictions and compare the mean predicted probability with the observed rate, plus brier_score_loss or log_loss. Fix it with CalibratedClassifierCV. method='sigmoid', the default, fits Platt scaling with two parameters and suits small calibration sets. method='isotonic' fits any monotone map but overfits with far fewer than about 1,000 calibration samples. method='temperature', added in 1.8, rescales logits and handles multiclass natively. Calibrate on data the model never trained on: with cv=5 it fits per fold, and for an already trained model wrap it in FrozenEstimator, since cv='prefit' is no longer accepted.
Python
1from sklearn.calibration import CalibratedClassifierCV, calibration_curve
2from sklearn.datasets import make_classification
3from sklearn.ensemble import HistGradientBoostingClassifier
4from sklearn.frozen import FrozenEstimator
5from sklearn.metrics import brier_score_loss
6X, y = make_classification(6000, random_state=0)7model = HistGradientBoostingClassifier().fit(X[:3000], y[:3000])8cal = CalibratedClassifierCV(FrozenEstimator(model), method="isotonic")9cal.fit(X[3000:4500], y[3000:4500])# rows the model never saw10for m in(model, cal):11 p = m.predict_proba(X[4500:])[:,1]12print(brier_score_loss(y[4500:], p), calibration_curve(y[4500:], p, n_bins=5)[0])
f1_score(y_true, y_pred) raises Target is multiclass but average='binary' on a 3-class problem. Which average should you choose?
EasyMetrics, Class Imbalance and Calibration
Sample Answer
The default average='binary' only works for two classes, so you must choose how to combine per-class scores. 'macro' averages each class's F1 equally, so a rare class the model ignores pulls the score down; use it when every class matters. 'weighted' weights classes by their support and tends to follow the majority classes. 'micro' pools all true positives, false positives and false negatives, and for single-label multiclass data it equals accuracy. average=None returns the per-class array, which is what you should read when debugging. State which average you report, because on imbalanced data the numbers can differ widely.
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'.
A random forest's feature_importances_ gives a customer ID column a clearly nonzero importance. The business asks whether the ID matters. How do you answer?
HardModels, Custom Transformers and Persistence
Sample Answer
feature_importances_ is the mean decrease in impurity, measured on the training data. It favors continuous and high-cardinality features, because they offer many split points that reduce training impurity even when the split is noise, and fully grown trees memorize those splits. It also says nothing about held-out performance. Use sklearn.inspection.permutation_importance on a validation set instead: shuffle one column and measure the score drop. A random ID scores near zero there. Two caveats apply to both methods. Correlated features share or mask each other's importance, so permute correlated groups together. And importance describes the model, not causation.
Python
1import numpy as np
2from sklearn.datasets import make_classification
3from sklearn.ensemble import RandomForestClassifier
4from sklearn.inspection import permutation_importance
5from sklearn.model_selection import train_test_split
6X, y = make_classification(2000, n_features=5, n_informative=3, random_state=0)7X = np.c_[X, np.random.RandomState(0).permutation(len(X))]# column 5: random ID8X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=0)9rf = RandomForestClassifier(random_state=0).fit(X_tr, y_tr)10pi = permutation_importance(rf, X_te, y_te, n_repeats=10, random_state=0)11print(rf.feature_importances_[5], pi.importances_mean[5])# about 0.04 vs about 0.0
You have 5 million rows with missing values and a few categorical columns. Why choose HistGradientBoostingClassifier over GradientBoostingClassifier, and which of its defaults should you know?
MediumModels, Custom Transformers and Persistence
Sample Answer
HistGradientBoostingClassifier bins each feature into at most max_bins=255 bins before training, so split finding scans bins instead of sorted values, which makes it far faster on large data. It handles NaN natively by learning which side of each split missing values go to, so you can skip imputation. With the default categorical_features='from_dtype', pandas category columns get native categorical splits without one-hot encoding. early_stopping='auto' turns early stopping on only when the training set has more than 10,000 samples, holding out validation_fraction=0.1. GradientBoostingClassifier evaluates exact splits, needs imputed input, and is practical only for small datasets.
Write a transformer that clips each numeric column to its 1st and 99th percentiles, learned on training data. It must work inside a Pipeline, survive clone, and support set_output.
HardModels, Custom Transformers and Persistence
Sample Answer
Follow the estimator contract. __init__ only stores lower and upper. fit validates input with validate_data, which also records n_features_in_ and feature_names_in_, learns the percentiles into attributes ending in an underscore, and returns self. transform checks that the object is fitted, validates with reset=False so the column count and names must match, and applies the stored bounds without recomputing them on new data. OneToOneFeatureMixin supplies get_feature_names_out, which set_output(transform='pandas') needs, and TransformerMixin supplies fit_transform. For stateless functions use FunctionTransformer instead, and run check_estimator on the class to confirm it follows the API.
You want every transformer in a pipeline to return pandas DataFrames with readable column names so you can debug features. How do you do it, and why might it fail with a OneHotEncoder?
MediumModels, Custom Transformers and Persistence
Sample Answer
Call pipe.set_output(transform='pandas'), or set it globally with sklearn.set_config(transform_output='pandas'); 'polars' also works. Transformers then return DataFrames named by get_feature_names_out, such as num__age and cat__city_NY, keeping the input's index. The usual failure is sparse output. OneHotEncoder returns a sparse matrix by default, pandas output does not support sparse data, and the call raises a ValueError until you set sparse_output=False. Dense output costs memory on very wide encodings, so keep the default output in production pipelines where that matters. verbose_feature_names_out=False on the ColumnTransformer drops the prefixes when names are unique.
How do you save a trained scikit-learn model for a nightly batch scoring job, and what can go wrong when you load it months later?
EasyModels, Custom Transformers and Persistence
Sample Answer
Persist the whole fitted Pipeline, preprocessing included, with joblib.dump or pickle, and load it with joblib.load. Pickles are tied to library versions. Loading under a different scikit-learn version emits an InconsistentVersionWarning and may misbehave or fail, so record the scikit-learn, NumPy and Python versions next to the file and pin them in the scoring environment. Unpickling can execute arbitrary code, so never load a model file from an untrusted source. The skops.io format restricts what can be loaded, and converting to ONNX lets services without Python score the model. When you upgrade libraries, retrain and save again instead of loading old pickles.
Python
1import joblib
2from sklearn.datasets import make_classification
3from sklearn.linear_model import LogisticRegression
4from sklearn.pipeline import make_pipeline
5from sklearn.preprocessing import StandardScaler
6X, y = make_classification(200, random_state=0)7pipe = make_pipeline(StandardScaler(), LogisticRegression()).fit(X, y)8joblib.dump(pipe,"model.joblib")# preprocessing travels with the model9loaded = joblib.load("model.joblib")# only load files you trust10print((loaded.predict(X)== pipe.predict(X)).all())
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.
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.