← Back to Glossary

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

Copy code

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.

Copy code

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.

Related terms

FAQS

Not directly. scikit-learn focuses on classical machine learning algorithms; deep neural networks are typically built with frameworks like PyTorch or TensorFlow, though scikit-learn is often used alongside them for preprocessing and evaluation.

scikit-learn itself only works with in-memory arrays and DataFrames — it has no database connectivity. A query engine like DuckDB is commonly used to pull and shape data from files or databases into the NumPy array or DataFrame that scikit-learn expects.