The industry standard library for machine learning in Python. Provides simple and efficient tools for predictive data analysis, covering classification, regression, clustering, dimensionality reduction, model selection, and preprocessing.
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
The industry standard library for machine learning in Python. Provides simple and efficient tools for predictive data analysis, covering classification, regression, clustering, dimensionality reduction, model selection, and preprocessing.
version
1.4
license
BSD-3-Clause
scikit-learn - Machine Learning in Python
A robust library for classical machine learning. It features a uniform API: all objects share the same interface for fitting, transforming, and predicting.
When to Use
Classification: Detecting categories (Spam vs. Ham, Disease diagnosis).
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.metrics import classification_report, mean_squared_error
Basic Pattern - Train/Predict
from sklearn.ensemble import RandomForestClassifier
# 1. Prepare data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 2. Instantiate and fit
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
# 3. Predict and evaluate
predictions = model.predict(X_test)
print(classification_report(y_test, predictions))
Critical Rules
✅ DO
Split before anything - Always use train_test_split before looking at data properties.
Use Pipelines - Combine preprocessing and modeling to prevent data leakage.
Scale your data - Models like SVM, KNN, and Linear Regression require feature scaling.
Check for Imbalance - Use stratify=y in train_test_split for classification.
Cross-Validate - Don't trust a single train/test split; use cross_val_score.
Handle Missing Values - Use SimpleImputer or similar before fitting models.
Standardize Categories - Use OneHotEncoder for nominal or OrdinalEncoder for ordinal data.
❌ DON'T
Fit on test data - Never call .fit() or .fit_transform() on the test set.
Use Categorical data as-is - Scikit-learn requires numerical input; encode strings first.
Ignore Class Imbalance - Accuracy is misleading for imbalanced datasets; use F1-score or AUC.
Overfit - Don't keep tuning hyperparameters until the test score is perfect.
Ignore Random State - Set random_state for reproducibility during experiments.
Anti-Patterns (NEVER)
# ❌ BAD: Data Leakage (Fitting scaler on the whole dataset)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X) # Data from "future" test set leaks into training!
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y)
# ✅ GOOD: Fit scaler only on training data
X_train, X_test, y_train, y_test = train_test_split(X, y)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test) # Use training mean/std# ❌ BAD: Repeating preprocessing manually# (Error-prone and hard to maintain)# ✅ GOOD: Use Pipelines (Automates everything safely)
pipe = Pipeline([
('scaler', StandardScaler()),
('model', RandomForestClassifier())
])
pipe.fit(X_train, y_train)
from sklearn.base import BaseEstimator, TransformerMixin
classLogTransformer(BaseEstimator, TransformerMixin):
def__init__(self, columns=None):
self.columns = columns
deffit(self, X, y=None):
returnselfdeftransform(self, X):
X_copy = X.copy()
for col inself.columns:
X_copy[col] = np.log1p(X_copy[col])
return X_copy
Performance Optimization
Using n_jobs
# Use all CPU cores for training/tuning
model = RandomForestClassifier(n_jobs=-1)
grid = GridSearchCV(model, param_grid, n_jobs=-1)
Working with Large Data (partial_fit)
from sklearn.linear_model import SGDClassifier
# Online learning (incremental fit)
model = SGDClassifier()
for X_chunk, y_chunk in data_stream:
model.partial_fit(X_chunk, y_chunk, classes=np.unique(y_all))
Common Pitfalls and Solutions
Imbalanced Classes
# ❌ Problem: Model predicts only the majority class# ✅ Solution: Adjust class weights
model = RandomForestClassifier(class_weight='balanced')
# OR use SMOTE from imbalanced-learn library
Convergence Warnings
# ❌ Problem: "ConvergenceWarning: Liblinear failed to converge"# ✅ Solution: Increase max_iter or scale data
model = LogisticRegression(max_iter=2000)
# Often solved by applying StandardScaler first!
Categorical Values in Test Set not in Train
# ❌ Problem: ValueError when unseen categories appear in test# ✅ Solution: Use handle_unknown in OneHotEncoder
encoder = OneHotEncoder(handle_unknown='ignore')
Scikit-learn is the backbone of Python ML. Its API is so successful that many other libraries (XGBoost, LightGBM) mimic it.