🤖 Scikit-learn

What is scikit-learn?

scikit-learn (sklearn) is Python’s core general-purpose machine learning library — consistent APIs for preprocessing, supervised/unsupervised models, model selection, and evaluation, all built on top of NumPy and interoperable with pandas DataFrames.

import sklearn
from sklearn.model_selection import train_test_split

Version note

Assumes scikit-learn ≥ 1.3. Key modern conventions: set_output(transform="pandas") for DataFrame-native pipelines, and consistent get_feature_names_out() across transformers.


📂 Folder Contents

#NoteCovers
0101-Estimator-API-BasicsThe fit/predict/transform contract, core conventions
0202-Preprocessing-ScalingScalers, encoders, imputers
0303-Train-Test-Split-Cross-Validationtrain_test_split, KFold, cross_val_score
0404-Pipelines-ColumnTransformerPipeline, ColumnTransformer, make_pipeline
0505-Linear-ModelsLinear/Logistic Regression, Ridge, Lasso, ElasticNet
0606-Tree-Ensemble-ModelsDecision Trees, Random Forest, Gradient Boosting, Voting/Stacking
0707-SVMSupport Vector Machines, kernels
0808-KNN-Naive-Bayesk-Nearest Neighbors, Naive Bayes variants
0909-ClusteringKMeans, DBSCAN, Hierarchical/Agglomerative
1010-Dimensionality-ReductionPCA, t-SNE, LDA, feature reduction
1111-Model-Evaluation-MetricsClassification/regression metrics, confusion matrix
1212-Hyperparameter-TuningGridSearchCV, RandomizedSearchCV, learning curves
1313-Feature-SelectionSelectKBest, RFE, feature importance
1414-Text-Feature-ExtractionCountVectorizer, TfidfVectorizer
1515-Neural-Networks-MLPMLPClassifier/MLPRegressor
1616-Model-Persistencejoblib, pickling models
1717-Common-Errors-GotchasData leakage, scaling order, common ValueErrors

🗺️ Conceptual Map

graph TD
    A[scikit-learn] --> B[Preprocessing]
    A --> C[Model Selection]
    A --> D[Supervised Learning]
    A --> E[Unsupervised Learning]
    A --> F[Evaluation]

    B --> B1[Scalers / Encoders]
    B --> B2[Imputers]
    B --> B3[Pipelines]

    C --> C1[train_test_split]
    C --> C2[Cross-Validation]
    C --> C3[Hyperparameter Tuning]

    D --> D1[Linear Models]
    D --> D2[Tree / Ensemble]
    D --> D3[SVM]
    D --> D4[KNN / Naive Bayes]
    D --> D5[Neural Nets]

    E --> E1[Clustering]
    E --> E2[Dimensionality Reduction]

    F --> F1[Classification Metrics]
    F --> F2[Regression Metrics]

⚡ Quick Reference — Typical Workflow

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 
pipe = Pipeline([
    ("scaler", StandardScaler()),
    ("model", LogisticRegression())
])
 
pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_test)
 
print(accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))

  • Pandas Reference — data loading/cleaning before feeding into sklearn
  • NumPy Reference — sklearn arrays, math under the hood
  • ML Study Notes — Cost Function, Linear Regression theory that these models implement

How to use this vault section

Same skeleton throughout: Definition → Syntax → Key Parameters → Examples → Notes/Gotchas. Ctrl/Cmd+O and type “Sklearn” to jump between notes.