scikit-learn
Overview
scikit-learn is a Python machine learning library built on NumPy, SciPy, and matplotlib, providing simple and efficient tools for predictive data analysis. It implements a huge range of classical algorithms behind a uniform estimator API (fit, predict, transform) plus utilities for preprocessing, model selection, and evaluation. Read this when doing tabular/classical ML, building reproducible Pipelines, or tuning and validating models.
Official sources
Install / setup
pip install -U scikit-learn
Command from the official install guide: https://scikit-learn.org/stable/install.html
Core concepts
- Estimator — any object with
.fit(X, y); the unifying abstraction across all algorithms.
- Transformer — estimator with
.transform() (scalers, encoders, vectorizers) for feature engineering.
- Predictor — estimator with
.predict() (and often .predict_proba/.score) for inference.
- Pipeline — chains transformers + a final estimator so fit/transform happen in order and only on training folds.
- ColumnTransformer — applies different transformers to different column subsets in one object.
- Model selection —
train_test_split, cross_val_score, GridSearchCV/RandomizedSearchCV for evaluation and tuning.
- Metrics —
sklearn.metrics for accuracy, precision/recall, ROC AUC, RMSE, R², etc.
Best practices
Common pitfalls
- Data leakage from scaling/encoding before the split → wrap preprocessing in a Pipeline and fit inside CV.
- Using accuracy on imbalanced classes → prefer precision/recall, F1, or ROC/PR AUC.
- Passing pandas columns of mixed dtypes → encode categoricals (e.g.
OneHotEncoder) via ColumnTransformer first.
Examples
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
pipe = Pipeline([
("scale", StandardScaler()),
("clf", LogisticRegression(max_iter=1000)),
])
print(cross_val_score(pipe, X, y, cv=5).mean())
Further reading
Related skills
- ../pandas — DataFrames feeding scikit-learn estimators
- ../numpy — array backbone for X/y inputs
- ../jupyter — interactive environment for ML experiments
1---2name: scikit-learn3description: scikit-learn is the standard Python library for classical (non-deep) machine learning, offering consistent fit/predict/transform APIs for classification, regression, clustering, dimensionality reduction, model selection, and preprocessing built on NumPy/SciPy. Consult this when choosing an estimator, building Pipelines and ColumnTransformers, doing cross-validation and GridSearchCV/hyperparameter tuning, computing metrics, encoding/scaling features, or avoiding data leakage in train/test splits.4domain: stack5category: aiml6tags: [scikit-learn, machine-learning, sklearn, classification, regression, clustering, python]7official_sources:8 - https://scikit-learn.org/stable/9 - https://github.com/scikit-learn/scikit-learn10 - https://scikit-learn.org/stable/install.html11verified: 2026-06-1712metadata:13 source: https://skillmd.com/skills/gabriel7silva/scikit-learn14---1516# scikit-learn1718## Overview19scikit-learn is a Python machine learning library built on NumPy, SciPy, and matplotlib, providing simple and efficient tools for predictive data analysis. It implements a huge range of classical algorithms behind a uniform estimator API (`fit`, `predict`, `transform`) plus utilities for preprocessing, model selection, and evaluation. Read this when doing tabular/classical ML, building reproducible Pipelines, or tuning and validating models.2021## Official sources22- Docs: https://scikit-learn.org/stable/23- Repo: https://github.com/scikit-learn/scikit-learn24- Install: https://scikit-learn.org/stable/install.html2526## Install / setup27```bash28pip install -U scikit-learn29```30Command from the official install guide: https://scikit-learn.org/stable/install.html3132## Core concepts33- **Estimator** — any object with `.fit(X, y)`; the unifying abstraction across all algorithms.34- **Transformer** — estimator with `.transform()` (scalers, encoders, vectorizers) for feature engineering.35- **Predictor** — estimator with `.predict()` (and often `.predict_proba`/`.score`) for inference.36- **Pipeline** — chains transformers + a final estimator so fit/transform happen in order and only on training folds.37- **ColumnTransformer** — applies different transformers to different column subsets in one object.38- **Model selection** — `train_test_split`, `cross_val_score`, `GridSearchCV`/`RandomizedSearchCV` for evaluation and tuning.39- **Metrics** — `sklearn.metrics` for accuracy, precision/recall, ROC AUC, RMSE, R², etc.4041## Best practices42- Put all preprocessing in a `Pipeline` so cross-validation refits transforms per fold and prevents leakage (https://scikit-learn.org/stable/modules/compose.html).43- Fit scalers/encoders on training data only; never fit on the full dataset before splitting (https://scikit-learn.org/stable/common_pitfalls.html).44- Use `GridSearchCV`/`RandomizedSearchCV` with a `cv` strategy rather than manual loops (https://scikit-learn.org/stable/modules/grid_search.html).45- Set `random_state` for reproducible splits and estimators (https://scikit-learn.org/stable/common_pitfalls.html).4647## Common pitfalls48- Data leakage from scaling/encoding before the split → wrap preprocessing in a Pipeline and fit inside CV.49- Using accuracy on imbalanced classes → prefer precision/recall, F1, or ROC/PR AUC.50- Passing pandas columns of mixed dtypes → encode categoricals (e.g. `OneHotEncoder`) via ColumnTransformer first.5152## Examples53```python54from sklearn.pipeline import Pipeline55from sklearn.preprocessing import StandardScaler56from sklearn.linear_model import LogisticRegression57from sklearn.model_selection import cross_val_score58from sklearn.datasets import load_iris5960X, y = load_iris(return_X_y=True)61pipe = Pipeline([62 ("scale", StandardScaler()),63 ("clf", LogisticRegression(max_iter=1000)),64])65print(cross_val_score(pipe, X, y, cv=5).mean())66```6768## Further reading69- https://scikit-learn.org/stable/user_guide.html — comprehensive user guide70- https://scikit-learn.org/stable/auto_examples/index.html — runnable example gallery71- https://scikit-learn.org/stable/common_pitfalls.html — common pitfalls and recommended practices7273## Related skills74- ../pandas — DataFrames feeding scikit-learn estimators75- ../numpy — array backbone for X/y inputs76- ../jupyter — interactive environment for ML experiments