This cheatsheet covers the most essential scikit-learn functions organized by task. Each table shows the function, what it does, and key parameters. Copy the code, modify for your data, and run.
from sklearn.preprocessing import StandardScaler, from sklearn.linear_model import LogisticRegression, etc. Keep your code readable and modular.
1. Data Preprocessing
Transform raw features into machine-learning-ready arrays. Always fit on train, transform on test.
| Code | What it does | Key Parameters |
|---|---|---|
| StandardScaler() | Normalize: mean=0, std=1. Essential for KNN, SVM, neural nets. | scaler.fit_transform(X_train); scaler.transform(X_test) |
| MinMaxScaler() | Scale to [0, 1] range. Use when you need bounded features. | feature_range=(0, 1); scaler.fit(X_train) |
| LabelEncoder() | Encode binary/ordinal categories: {A, B, C} โ {0, 1, 2}. | le.fit_transform(df['gender']); le.inverse_transform([0, 1]) |
| OneHotEncoder() | Multi-class categorical: {A, B, C} โ 3 binary columns. | enc.fit_transform(df[['color']]); drop='first' to prevent collinearity |
| SimpleImputer() | Fill missing values. Strategy: 'mean', 'median', 'most_frequent'. | imputer.fit(X_train); imputer.transform(X_test); strategy='median' |
| train_test_split() | Split data 80/20. Always before scaling. Preserve class balance with stratify. | test_size=0.2; random_state=42; stratify=y for balanced splits |
2. Regression Models
Predict continuous values. Check Rยฒ score and mean absolute error (MAE).
| Code | What it does | Key Parameters |
|---|---|---|
| LinearRegression() | Simple linear fit. No regularization. Fastest. | model.fit(X_train, y_train); model.coef_; model.intercept_ |
| Ridge() | Linear with L2 penalty. Shrinks large coefficients. Reduces overfitting. | alpha=1.0 (higher = more shrinkage); model.fit(X_train, y_train) |
| Lasso() | Linear with L1 penalty. Performs feature selection (zero out weak features). | alpha=0.01; max_iter=10000; useful for high-dimensional data |
| ElasticNet() | Combines Ridge (L2) + Lasso (L1). Balanced regularization. | alpha=0.1; l1_ratio=0.5 (0=Ridge, 1=Lasso); good default hybrid |
3. Classification Models
Predict categories (binary or multi-class). Check accuracy, precision, recall, F1.
| Code | What it does | Key Parameters |
|---|---|---|
| LogisticRegression() | Fast binary/multi-class classifier. Linear boundary. Interpretable. | C=1.0 (inverse regularization); class_weight='balanced'; solver='lbfgs' |
| DecisionTreeClassifier() | Interpretable tree. Fast training. Prone to overfitting. | max_depth=5 (limit splits); min_samples_split=10; criterion='gini' |
| RandomForestClassifier() | Ensemble of trees. Robust, handles imbalance well. Feature importance built-in. | n_estimators=100; max_depth=10; class_weight='balanced'; random_state=42 |
| GradientBoostingClassifier() | Sequential boosting. Often the best accuracy. Slower training. | n_estimators=100; learning_rate=0.1; max_depth=5; subsample=0.8 |
| SVC() | Support Vector Machine. Non-linear with kernel. Excellent for small datasets. | kernel='rbf'; C=1.0; probability=True (for scores); gamma='scale' |
4. Clustering Models
Group similar observations. No labels needed. Check silhouette score.
| Code | What it does | Key Parameters |
|---|---|---|
| KMeans() | Partition into k clusters. Fast, scalable. Assumes spherical clusters. | n_clusters=5; random_state=42; n_init=10 (multiple runs); algorithm='lloyd' |
| AgglomerativeClustering() | Hierarchical clustering. Creates dendrogram. No random state needed. | n_clusters=5; linkage='ward' (or 'complete', 'average'); produces tree structure |
| DBSCAN() | Density-based clustering. Finds arbitrary shapes. Handles noise well. | eps=0.5 (neighborhood radius); min_samples=5; doesn't need k clusters specified |
5. Dimensionality Reduction
Compress features while retaining variance. Useful for visualization and speed.
| Code | What it does | Key Parameters |
|---|---|---|
| PCA() | Principal Component Analysis. Rotate to max variance axes. Unsupervised. | n_components=2 (or float 0.95 for 95% variance); fit_transform(X); check .explained_variance_ratio_ |
6. Evaluation Metrics
Measure model performance. Choose metrics that match your business goal.
| Code | What it does | Key Parameters / Usage |
|---|---|---|
| accuracy_score() | Fraction correct. Good for balanced data. | accuracy_score(y_test, y_pred); 0 to 1 scale |
| precision_score() | TP / (TP + FP). Of all positive predictions, how many correct? | precision_score(y_test, y_pred); use when false positives are costly |
| recall_score() | TP / (TP + FN). Of all true positives, how many caught? | recall_score(y_test, y_pred); use when missing positives is costly |
| f1_score() | Harmonic mean of precision and recall. Balanced metric. | f1_score(y_test, y_pred); use for imbalanced data |
| roc_auc_score() | Area Under ROC Curve. Measures ranking quality. Robust to imbalance. | roc_auc_score(y_test, y_pred_proba); use probabilities not hard labels |
| confusion_matrix() | 4-cell table: TP, FP, FN, TN. Diagnostic tool. | confusion_matrix(y_test, y_pred); returns [[TN, FP], [FN, TP]] |
| classification_report() | Precision, recall, F1 for each class + averages. | classification_report(y_test, y_pred); best for multi-class overview |
| mean_squared_error() | Average squared error. Penalizes large mistakes. For regression. | mean_squared_error(y_test, y_pred); squared so use sqrt for original units |
| mean_absolute_error() | Average absolute error. Linear penalty. Easier to interpret. | mean_absolute_error(y_test, y_pred); same units as target |
| r2_score() | Coefficient of determination. Proportion of variance explained. 0 to 1 (or negative). | r2_score(y_test, y_pred); compare models, higher is better |
| silhouette_score() | Cluster cohesion metric. Higher means better separated clusters. -1 to 1. | silhouette_score(X, labels); only for clustering |
7. Model Selection & Tuning
Find the best model and hyperparameters. Cross-validate to avoid overfitting.
| Code | What it does | Key Parameters |
|---|---|---|
| cross_val_score() | k-fold cross-validation. Returns array of scores. | cross_val_score(model, X, y, cv=5, scoring='f1'); takes mean of folds |
| GridSearchCV() | Exhaustive search over param grid. Finds best combo. Slow but thorough. | GridSearchCV(model, param_grid, cv=5, scoring='f1'); .best_params_, .best_score_ |
| RandomizedSearchCV() | Random sample from param space. Faster than GridSearchCV. Good for large grids. | RandomizedSearchCV(model, distributions, n_iter=30, cv=5); good for tuning |
8. Pipelines & Column Transformers
Chain preprocessing and modeling in one object. Prevents data leakage. Cleaner code.
| Code | What it does | Key Parameters / Example |
|---|---|---|
| Pipeline() | Chain steps: fit on train, predict with all steps. | Pipeline([('scaler', StandardScaler()), ('model', LogisticRegression())]); pipe.fit(X_train, y_train) |
| ColumnTransformer() | Apply different transformers to different column subsets. | ColumnTransformer([('num', scaler, num_cols), ('cat', encoder, cat_cols)]); efficient multi-step prep |
Quick Code Snippets
Snippet 1: Classification pipeline (logistic regression)
from sklearn.preprocessing import StandardScalerfrom sklearn.linear_model import LogisticRegressionfrom sklearn.pipeline import Pipelinefrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score, f1_scoreX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)pipe = Pipeline([('scaler', StandardScaler()), ('model', LogisticRegression(max_iter=1000))])pipe.fit(X_train, y_train)y_pred = pipe.predict(X_test)print(f"Accuracy: {accuracy_score(y_test, y_pred):.3f}, F1: {f1_score(y_test, y_pred):.3f}")
Snippet 2: Regression with GridSearchCV
from sklearn.ensemble import RandomForestRegressorfrom sklearn.model_selection import GridSearchCVfrom sklearn.metrics import r2_score, mean_absolute_errorparam_grid = {'n_estimators': [50, 100, 200], 'max_depth': [5, 10, 15]}rf = RandomForestRegressor(random_state=42)grid = GridSearchCV(rf, param_grid, cv=5, scoring='r2')grid.fit(X_train, y_train)print(f"Best params: {grid.best_params_}, Best R2: {grid.best_score_:.3f}")y_pred = grid.predict(X_test)print(f"Test R2: {r2_score(y_test, y_pred):.3f}")
Snippet 3: Clustering with KMeans
from sklearn.cluster import KMeansfrom sklearn.preprocessing import StandardScalerfrom sklearn.metrics import silhouette_scorescaler = StandardScaler()X_scaled = scaler.fit_transform(X)kmeans = KMeans(n_clusters=5, random_state=42, n_init=10)labels = kmeans.fit_predict(X_scaled)score = silhouette_score(X_scaled, labels)print(f"Silhouette Score: {score:.3f}")
Snippet 4: Dimensionality reduction with PCA
from sklearn.decomposition import PCAfrom sklearn.preprocessing import StandardScalerscaler = StandardScaler()X_scaled = scaler.fit_transform(X)pca = PCA(n_components=0.95) # Keep 95% varianceX_pca = pca.fit_transform(X_scaled)print(f"Original shape: {X.shape}, PCA shape: {X_pca.shape}")print(f"Variance explained: {pca.explained_variance_ratio_.sum():.3f}")
Common Gotchas
2. Scale AFTER train/test split. Scaling before split leaks test statistics into train.
3. Use stratify with imbalanced data. Without stratify, test might have 0% of the rare class.
4. Use drop_first=True with get_dummies. Without it, you create multicollinearity.
5. Choose the right metric. Accuracy lies on imbalanced data. Use F1, AUC, or precision/recall instead.
6. Always cross-validate. A single train/test split is noisy. Use k-fold (cv=5) to be sure.
Ready to build production ML systems?
Join the Best AI Program: The AI-Era Analyst โ 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Analyst โ