---
title: "scikit-learn"
description: "scikit-learn is a widely used Python library for classical machine learning, providing a consistent API for classification, regression, clustering, and preprocessing built on NumPy and SciPy."
canonical: "https://motherduck.com/glossary/scikit-learn/"
related:
  - title: "DuckLake Architecture Deep Dive"
    url: "https://motherduck.com/blog/ducklake-architecture-deep-dive/"
  - title: "Data Science & AI | MotherDuck Docs"
    url: "https://motherduck.com/docs/integrations/data-science-ai/"
  - title: "DuckDB & Python | End-To-End Data Engineering Project (1/3) | MotherDuck"
    url: "https://motherduck.com/videos/duckdb-python-end-to-end-data-engineering-project-13/"
---

# scikit-learn

> scikit-learn is a widely used Python library for classical machine learning, providing a consistent API for classification, regression, clustering, and preprocessing built on NumPy and SciPy.

## Overview

scikit-learn (often imported as `sklearn`) is the standard library for classical machine learning in Python. It implements a large, consistent set of algorithms for classification, regression, clustering, dimensionality reduction, and model selection, all built around a uniform `fit` / `predict` / `transform` API. It is not designed for deep learning (that's the domain of PyTorch or TensorFlow) but remains the go-to tool for tabular data problems: linear models, tree-based models, gradient boosting-style ensembles, SVMs, and preprocessing pipelines.

## Basic usage

```python
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = RandomForestClassifier(n_estimators=200)
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
```

scikit-learn's `Pipeline` and `ColumnTransformer` classes let you chain preprocessing (scaling, encoding, imputation) and modeling steps into a single reusable object.

## Feature engineering with DuckDB

scikit-learn expects clean, numeric feature matrices (typically NumPy arrays or pandas DataFrames), and getting raw data into that shape — joins, filters, window functions, aggregations, one-hot encoding of categories — is often most efficiently done in SQL before it ever reaches Python. DuckDB is well suited to this preprocessing step: it can read Parquet or CSV files directly, run the heavy aggregation and feature-engineering SQL, and hand off the resulting table as a pandas DataFrame or NumPy array straight into a scikit-learn pipeline.

```python
import duckdb

features = duckdb.sql('''
    SELECT customer_id,
           count(*) AS num_orders,
           avg(amount) AS avg_order_value
    FROM read_parquet('orders.parquet')
    GROUP BY ALL
''').df()

X = features.drop(columns=["customer_id"])
```

This keeps feature computation fast and out of Python loops, while leaving modeling to scikit-learn.
