Skip to content
IT glossary
Data and MLUpdated December 3, 2026

scikit-learn

A Python library for preparing data, training machine learning models, and checking how well they perform on examples they did not train on. The package is called scikit-learn, but you import it as `sklearn`.

Also known as: sklearn

What scikit-learn does

Scikit-learn gives Python developers a common interface for classical machine learning. You can train a model to predict a number, assign a class, or find groups in unlabeled data. The library also handles much of the work around the model: encoding categories, scaling features, splitting datasets, calculating metrics, and searching for hyperparameters. It is often a first choice for tabular data and simple text models, not a general-purpose framework for training large neural networks.

How the API fits together

For a supervised task, X is the feature matrix: each row is one example and each column is a feature. y holds the known answers, such as the sale price of each home. A call to fit(X, y) learns from those examples. For a predictive model, predict(X_new) then generates answers for new rows.

A transformer also has a fit method, but its job is to learn how to change the inputs. For instance, StandardScaler learns a mean and a standard deviation for each feature. transform applies those stored statistics, while fit_transform learns and applies them in one call. A Pipeline chains input transformations and a final model so that training and prediction use the same sequence of steps.

Example: predicting house prices

This example compares a basic linear regression with a baseline that always predicts the mean sale price from the training set. Both are evaluated on the same held-out rows. Here, X contains numeric features with no missing values, and y contains sale prices.

from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
for model in (DummyRegressor(strategy="mean"), LinearRegression()):
    model.fit(X_train, y_train)
    predictions = model.predict(X_test)
    print(type(model).__name__, mean_absolute_error(y_test, predictions))

Mean absolute error is the average size of the prediction error, in the same currency as the prices. The baseline makes it easier to tell whether the regression is learning useful information from the features. A random split is only appropriate if it reflects the way the model will be used. To predict future sale prices from past sales, a chronological split is usually a better test.

Common interview question: where should preprocessing happen?

Suppose you want to fill missing values using each column's median and compare models with cross-validation. If you calculate those medians on the full dataset first, they already contain information from the validation rows. That is data leakage, even though you never used their labels. Put the imputer and model in a Pipeline and pass the whole pipeline to GridSearchCV or another cross-validation tool. On each fold, the imputer will learn its medians from the training portion and reuse them on the validation portion. Keep a separate test set for the final evaluation rather than using it to pick settings.