Scikit-learn
Overview
This skill provides comprehensive guidance for machine learning tasks using scikit-learn, the industry-standard Python library for classical machine learning. Use this skill for classification, regression, clustering, dimensionality reduction, preprocessing, model evaluation, and building production-ready ML pipelines.
Installation
# Install scikit-learn using uv
uv uv pip install scikit-learn
# Optional: Install visualization dependencies
uv uv pip install matplotlib seaborn
# Commonly used with
uv uv pip install pandas numpy
When to Use This Skill
Use the scikit-learn skill when:
- Building classification or regression models
- Performing clustering or dimensionality reduction
- Preprocessing and transforming data for machine learning
- Evaluating model performance with cross-validation
- Tuning hyperparameters with grid or random search
- Creating ML pipelines for production workflows
- Comparing different algorithms for a task
- Working with both structured (tabular) and text data
- Need interpretable, classical machine learning approaches
Quick Start
Classification Example
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
# Preprocess
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train_scaled, y_train)
# Evaluate
y_pred = model.predict(X_test_scaled)
print(classification_report(y_test, y_pred))
Complete Pipeline with Mixed Data
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import GradientBoostingClassifier
# Define feature types
numeric_features = ['age', 'income']
categorical_features = ['gender', 'occupation']
# Create preprocessing pipelines
numeric_transformer = Pipeline([
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
])
categorical_transformer = Pipeline([
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
# Combine transformers
preprocessor = ColumnTransformer([
('num', numeric_transformer, numeric_features),
('cat', categorical_transformer, categorical_features)
])
# Full pipeline
model = Pipeline([
('preprocessor', preprocessor),
('classifier', GradientBoostingClassifier(random_state=42))
])
# Fit and predict
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
Core Capabilities
1. Supervised Learning
Comprehensive algorithms for classification and regression tasks.
Key algorithms:
- Linear models: Logistic Regression, Linear Regression, Ridge, Lasso, ElasticNet
- Tree-based: Decision Trees, Random Forest, Gradient Boosting
- Support Vector Machines: SVC, SVR with various kernels
- Ensemble methods: AdaBoost, Voting, Stacking
- Neural Networks: MLPClassifier, MLPRegressor
- Others: Naive Bayes, K-Nearest Neighbors
When to use:
- Classification: Predicting discrete categories (spam detection, image classification, fraud detection)
- Regression: Predicting continuous values (price prediction, demand forecasting)
See: references/supervised_learning.md for detailed algorithm documentation, parameters, and usage examples.
2. Unsupervised Learning
Discover patterns in unlabeled data through clustering and dimensionality reduction.
Clustering algorithms:
- Partition-based: K-Means, MiniBatchKMeans
- Density-based: DBSCAN, HDBSCAN, OPTICS
- Hierarchical: AgglomerativeClustering
- Probabilistic: Gaussian Mixture Models
- Others: MeanShift, SpectralClustering, BIRCH
Dimensionality reduction:
- Linear: PCA, TruncatedSVD, NMF
- Manifold learning: t-SNE, UMAP, Isomap, LLE
- Feature extraction: FastICA, LatentDirichletAllocation
When to use:
- Customer segmentation, anomaly detection, data visualization
- Reducing feature dimensions, exploratory data analysis
- Topic modeling, image compression
See: references/unsupervised_learning.md for detailed documentation.
3. Model Evaluation and Selection
Tools for robust model evaluation, cross-validation, and hyperparameter tuning.
Cross-validation strategies:
- KFold, StratifiedKFold (classification)
- TimeSeriesSplit (temporal data)
- GroupKFold (grouped samples)
Hyperparameter tuning:
- GridSearchCV (exhaustive search)
- RandomizedSearchCV (random sampling)
- HalvingGridSearchCV (successive halving)
Metrics:
1---2name: scikit-learn3description: Machine learning in Python with scikit-learn. Use for classification, regression, clustering, model evaluation, and ML pipelines. 4category: Document Processing5source: antigravity6tags: [python, api, ai, workflow, template, design, document, image, cro]7url: https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/scikit-learn8metadata:9 source: https://skillmd.com/skills/sickn33/scikit-learn10---111213# Scikit-learn1415## Overview1617This skill provides comprehensive guidance for machine learning tasks using scikit-learn, the industry-standard Python library for classical machine learning. Use this skill for classification, regression, clustering, dimensionality reduction, preprocessing, model evaluation, and building production-ready ML pipelines.1819## Installation2021```bash22# Install scikit-learn using uv23uv uv pip install scikit-learn2425# Optional: Install visualization dependencies26uv uv pip install matplotlib seaborn2728# Commonly used with29uv uv pip install pandas numpy30```3132## When to Use This Skill3334Use the scikit-learn skill when:3536- Building classification or regression models37- Performing clustering or dimensionality reduction38- Preprocessing and transforming data for machine learning39- Evaluating model performance with cross-validation40- Tuning hyperparameters with grid or random search41- Creating ML pipelines for production workflows42- Comparing different algorithms for a task43- Working with both structured (tabular) and text data44- Need interpretable, classical machine learning approaches4546## Quick Start4748### Classification Example4950```python51from sklearn.model_selection import train_test_split52from sklearn.preprocessing import StandardScaler53from sklearn.ensemble import RandomForestClassifier54from sklearn.metrics import classification_report5556# Split data57X_train, X_test, y_train, y_test = train_test_split(58 X, y, test_size=0.2, stratify=y, random_state=4259)6061# Preprocess62scaler = StandardScaler()63X_train_scaled = scaler.fit_transform(X_train)64X_test_scaled = scaler.transform(X_test)6566# Train model67model = RandomForestClassifier(n_estimators=100, random_state=42)68model.fit(X_train_scaled, y_train)6970# Evaluate71y_pred = model.predict(X_test_scaled)72print(classification_report(y_test, y_pred))73```7475### Complete Pipeline with Mixed Data7677```python78from sklearn.pipeline import Pipeline79from sklearn.compose import ColumnTransformer80from sklearn.preprocessing import StandardScaler, OneHotEncoder81from sklearn.impute import SimpleImputer82from sklearn.ensemble import GradientBoostingClassifier8384# Define feature types85numeric_features = ['age', 'income']86categorical_features = ['gender', 'occupation']8788# Create preprocessing pipelines89numeric_transformer = Pipeline([90 ('imputer', SimpleImputer(strategy='median')),91 ('scaler', StandardScaler())92])9394categorical_transformer = Pipeline([95 ('imputer', SimpleImputer(strategy='most_frequent')),96 ('onehot', OneHotEncoder(handle_unknown='ignore'))97])9899# Combine transformers100preprocessor = ColumnTransformer([101 ('num', numeric_transformer, numeric_features),102 ('cat', categorical_transformer, categorical_features)103])104105# Full pipeline106model = Pipeline([107 ('preprocessor', preprocessor),108 ('classifier', GradientBoostingClassifier(random_state=42))109])110111# Fit and predict112model.fit(X_train, y_train)113y_pred = model.predict(X_test)114```115116## Core Capabilities117118### 1. Supervised Learning119120Comprehensive algorithms for classification and regression tasks.121122**Key algorithms:**123- **Linear models**: Logistic Regression, Linear Regression, Ridge, Lasso, ElasticNet124- **Tree-based**: Decision Trees, Random Forest, Gradient Boosting125- **Support Vector Machines**: SVC, SVR with various kernels126- **Ensemble methods**: AdaBoost, Voting, Stacking127- **Neural Networks**: MLPClassifier, MLPRegressor128- **Others**: Naive Bayes, K-Nearest Neighbors129130**When to use:**131- Classification: Predicting discrete categories (spam detection, image classification, fraud detection)132- Regression: Predicting continuous values (price prediction, demand forecasting)133134**See:** `references/supervised_learning.md` for detailed algorithm documentation, parameters, and usage examples.135136### 2. Unsupervised Learning137138Discover patterns in unlabeled data through clustering and dimensionality reduction.139140**Clustering algorithms:**141- **Partition-based**: K-Means, MiniBatchKMeans142- **Density-based**: DBSCAN, HDBSCAN, OPTICS143- **Hierarchical**: AgglomerativeClustering144- **Probabilistic**: Gaussian Mixture Models145- **Others**: MeanShift, SpectralClustering, BIRCH146147**Dimensionality reduction:**148- **Linear**: PCA, TruncatedSVD, NMF149- **Manifold learning**: t-SNE, UMAP, Isomap, LLE150- **Feature extraction**: FastICA, LatentDirichletAllocation151152**When to use:**153- Customer segmentation, anomaly detection, data visualization154- Reducing feature dimensions, exploratory data analysis155- Topic modeling, image compression156157**See:** `references/unsupervised_learning.md` for detailed documentation.158159### 3. Model Evaluation and Selection160161Tools for robust model evaluation, cross-validation, and hyperparameter tuning.162163**Cross-validation strategies:**164- KFold, StratifiedKFold (classification)165- TimeSeriesSplit (temporal data)166- GroupKFold (grouped samples)167168**Hyperparameter tuning:**169- GridSearchCV (exhaustive search)170- RandomizedSearchCV (random sampling)171- HalvingGridSearchCV (successive halving)172173**Metrics:**174- **Cla