Scikit-learn
Scikit-learn is the workhorse for everything that is not deep learning: a uniform estimator API, preprocessing, pipelines, cross-validation, and metrics. Data-science interviews lean on it heavily because it forces you to talk about methodology, not just models.
on this pageshowhide
guide
overview
~1 minScikit-learn is the standard Python library for machine learning that is not deep learning: linear models, trees and ensembles, clustering, preprocessing, validation and metrics, all behind one consistent object interface. Interviewers use it as a lens on methodology. Almost anyone can call a classifier; the questions test whether you can tell an honest validation score from a flattering one, and whether you know which choice — a split, a metric, where a scaler gets fitted — decided the difference. The hub follows the order in which a project is built. The [Estimator API](/topics/ai-scikit-learn-estimator-api) is the contract every object honours, and the reason the rest composes. [Preprocessing and features](/topics/ai-scikit-learn-preprocessing) turns raw columns into a numeric matrix, and [pipelines](/topics/ai-scikit-learn-pipelines) bind those steps to a model so they are fitted, searched and saved together. [Model selection and cross-validation](/topics/ai-scikit-learn-model-selection) decides how data is split and hyperparameters tuned, [supervised estimators](/topics/ai-scikit-learn-supervised) covers the models you reach for and their main dials, and [metrics and evaluation](/topics/ai-scikit-learn-metrics-eval) asks whether the number you report answers the business question. Junior rounds check the vocabulary: what `fit` learns, how to split, how to read a confusion matrix. Senior and principal rounds turn into leakage stories, splitter choices for grouped or temporal data, metric choice under class imbalance, and what belongs in the artifact you ship. Learn the estimator contract and the train-only fitting rule first; nearly every later question is one of them applied.
primer
### One contract for every object Scalers, encoders, models and searches all follow the same interface: hyperparameters go into the constructor, `fit` learns from data, and `transform`, `predict` or `score` use what was learned. Because every object looks alike to the library, it can copy, chain and tune any of them without knowing what they do. A custom estimator is how interviewers test whether you know its rules. ### Configuration and learned state are kept apart What you choose and what the data teaches are stored separately. Constructor arguments are configuration; anything computed during `fit` lives in attributes with a trailing underscore. That split lets the library make an untrained copy of an estimator for every fold or search candidate. ### Fit on training data, and only training data Every statistic a transformer learns — a mean, a category list, a vocabulary, a set of selected features — is information about the data it was fitted on. Fitting any of it on rows later used for evaluation is **leakage**, and the score comes out better than the model deserves. Most methodology questions in this hub are this rule in a new setting. ### The pipeline is the unit you validate and ship A `Pipeline` makes preprocessing and model one estimator. Cross-validation and search then refit every step inside each fold, and the object you persist carries the same transforms the model was trained behind. ### The split must imitate deployment A validation score is only as honest as the split behind it. Rows from the same customer, patient or day, or rows from the future, change what "unseen" means. Picking the splitter is a modelling decision, and interviewers expect you to justify it from how the model will meet new data. ### The metric is the real question A model is tuned toward whatever scorer you hand the search. Accuracy, ranking quality and performance at one decision threshold reward different models, and on imbalanced data they can disagree sharply. Name the cost of each kind of error before naming a metric.
- Estimator
- Any scikit-learn object that learns from data through fit. Transformers, classifiers, regressors, clusterers and searches are all estimators.
- Transformer
- An estimator that maps an input matrix to a new one through transform, such as a scaler, encoder, imputer or feature selector.
- Hyperparameter
- A setting passed to an estimator's constructor and fixed before training, like a regularization strength or tree depth. Searches tune these.
- Fitted attribute
- State an estimator learns during fit, stored under a name ending in an underscore. It is absent until fit has run.
- Data leakage
- Information from evaluation rows reaching training, through preprocessing, feature construction or the split itself. It inflates validation scores and fails silently in production.
- Pipeline
- A chain of transformers ending in an estimator, fitted and used as one object, so every step is refitted inside each training fold.
- ColumnTransformer
- A transformer that sends different column subsets through different preprocessing steps and joins the results into one feature matrix.
- Cross-validation
- Repeatedly training on part of the data and scoring on the held-out rest, then averaging, to estimate performance on unseen data.
- CV splitter
- An object that decides which rows form each training and validation fold, for example by plain folds, class strata, groups or time order.
- Nested cross-validation
- An outer split that scores a whole tuning procedure on folds the inner search never saw, giving an unbiased estimate after tuning.
- Decision threshold
- The cutoff applied to a classifier's score to produce a label. Moving it trades precision against recall without retraining.
Picture a typical tabular project. Numeric and categorical columns go into a `ColumnTransformer`, each subset through its own imputer, scaler or encoder. That transformer becomes the first step of a `Pipeline` whose last step is a model. The pipeline goes into a search object, which takes a CV splitter and a scorer; for every candidate and fold it builds a fresh copy of the pipeline, fits it on the training fold and scores it on the held-out one. The best configuration is refitted on all training data, checked once on a held-out test set, and the fitted pipeline is what gets serialized and served. ```python prep = ColumnTransformer([ ("num", make_pipeline(SimpleImputer(strategy="median"), StandardScaler()), num_cols), ("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols), ]) pipe = Pipeline([("prep", prep), ("model", LogisticRegression(max_iter=1000))]) search = GridSearchCV(pipe, {"model__C": [0.1, 1.0, 10.0]}, cv=GroupKFold(n_splits=5), scoring="average_precision") search.fit(X_train, y_train, groups=customer_ids) ``` Each section of the hub owns one part of that snippet: - **Estimator API** explains why the search can copy and re-parameterize a pipeline it knows nothing about. - **Preprocessing** owns the inner transformers and what each learns from the training fold. - **Pipelines** owns the outer chain and the guarantee that preprocessing is refitted inside every fold. - **Model selection** owns the splitter and the search, and decides what "held out" means here: whole customers, not random rows. - **Supervised estimators** owns the final step and which of its hyperparameters deserve a place in the grid. - **Metrics** owns the `scoring` argument, which quietly decides what "best" means for the whole search. Change one piece and the others feel it: a different model may not need scaling, a temporal dataset needs a different splitter, and a new scorer can pick a different winner from the same grid.
- Estimator API →
The fit, transform and predict contract and the parameter rules that every later section quietly relies on.
- Preprocessing & Features →
How raw columns become features, and what each transformer learns that must come from training rows only.
- Pipelines →
Binding preprocessing to the model so validation and serving use the same fitted steps.
- Model Selection & CV →
Choosing a split that matches deployment and tuning hyperparameters without fooling yourself.
- Supervised Estimators →
The models you reach for first, their key dials and when each one stops scaling.
- Metrics & Evaluation →
Turning predictions into a number that reflects the real cost of each kind of error.
Fitting a scaler, encoder or feature selector on the full dataset before splitting, then reporting the cross-validated score as if it were honest.
Using a random row split when rows share a customer, patient or time period; see Model Selection & CV for splitters that respect structure.
Quoting accuracy on an imbalanced problem, where predicting the majority class alone can look excellent.
Passing
predict()labels to a ranking metric instead of probabilities or decision scores; the call succeeds and returns a misleading number.Reporting the best search score as the expected production performance, when the same folds chose the hyperparameters; that estimate is optimistic.
Persisting only the trained model and rebuilding preprocessing in serving code, so training and serving drift apart.
Doing validation or type conversion inside a custom estimator's
__init__, which breaks cloning and therefore every search that uses it.
This guide assumes a recent 1.x release, 1.6 or later. Most interview questions are version-independent, but several answers changed across the 1.x line, and code written against older releases still circulates: - **1.0** made the histogram-based gradient boosting estimators stable, so they no longer need an experimental import. - **1.2** added the `set_output` API, letting transformers return pandas DataFrames instead of arrays, and introduced `penalty=None` for turning off regularization in `LogisticRegression`. The same release renamed `OneHotEncoder`'s `sparse` argument to `sparse_output`. - **1.3** introduced metadata routing as an opt-in feature, the mechanism for passing values such as sample weights or groups through meta-estimators to the steps that need them. - **1.6** moved estimator tags behind a new `__sklearn_tags__` method, which matters if you maintain a custom estimator. When an answer leans on a default or an argument name, say which release you mean. A tutorial snippet that fails today usually fails because of one of these renames.
Scikit-learn sits on NumPy and SciPy: inputs are arrays, sparse matrices or pandas DataFrames, and outputs are arrays unless you ask for DataFrames. It owns classical machine learning on tabular data that fits in one machine's memory, which covers a large share of real business modelling. Interviewers expect you to place it against neighbours. XGBoost, LightGBM and CatBoost are dedicated gradient-boosting libraries; they ship scikit-learn-compatible wrappers, so they drop into pipelines and searches, and teams usually pick them when boosted trees on large tabular data are the main event. PyTorch, TensorFlow and JAX take over for deep learning on images, text and audio, where scikit-learn deliberately does not compete. Spark MLlib targets data too large for one machine, at the cost of a heavier runtime. Companions such as imbalanced-learn and joblib extend it for resampling and persistence. A strong answer names the workload first, then the tool: scikit-learn is the default baseline and glue, and often the final model too.
explore
- Estimator API6 questions
- Preprocessing & Features6 questions
- Pipelines6 questions
- Model Selection & CV6 questions
- Supervised Estimators6 questions
- Metrics & Evaluation6 questions
questions
page 2 of 2When does Pipeline(memory=...) in scikit-learn actually save time, and what does it cost?
basics
~20 sSetting memory on a Pipeline caches fitted transformers on disk, keyed by the transformer, its parameters and its input. It pays off during a search where many candidates share an identical, expensive prefix — and buys nothing when every candidate changes an early step.
How do you choose between scikit-learn's SelectKBest, SelectFromModel and RFE?
basics
~20 sSelectKBest scores each feature independently against the target and is cheapest. SelectFromModel fits one estimator and thresholds its coefficients or importances. RFE refits repeatedly, dropping the weakest features each round — the most expensive and the only one that reacts to what removal does.
What does class_weight='balanced' compute, and which scikit-learn estimators accept it?
basics
~20 sclass_weight='balanced' sets each class's weight to n_samples / (n_classes * count of that class), so rare classes contribute proportionally more loss. LogisticRegression, SVC, LinearSVC, the tree and forest classifiers and HistGradientBoostingClassifier accept it; GradientBoostingClassifier does not.
When should you use LinearSVC or SGDClassifier instead of SVC in scikit-learn?
basics
~20 sSVC's kernel solver has fit time scaling at least quadratically in the number of samples, so it becomes impractical past roughly tens of thousands of rows. For large linear problems reach for LinearSVC, backed by liblinear, or SGDClassifier, whose cost is linear in the sample count.
Which preprocessing belongs inside a persisted scikit-learn Pipeline versus upstream ETL?
basics
~20 sAnything with state learned from training data — imputer statistics, encoder categories, scaler means, vectorizer vocabularies, selection masks — must live inside the fitted Pipeline so training and serving share one artifact. Stateless business joins and label construction stay upstream.
What are scikit-learn estimator tags, and when do you implement __sklearn_tags__?
basics
~20 sEstimator tags are declarative metadata describing what an estimator can accept and do — sparse input, NaN tolerance, whether y is required, the estimator type. Since scikit-learn 1.6 they are a dataclass returned by sklearn_tags, which you override only when your estimator departs from the defaults.
showing 31–36 of 36