Reference · How-to · ~10 min

How to train a scikit-learn classifier

Last updated

Train and evaluate a classic ML model in a few lines — great baseline before reaching for an LLM.

Train and evaluate a classic ML model in a few lines — great baseline before reaching for an LLM.

#Steps

1. **Load tabular data** — CSV with features + label column

2. **Train/test split** — `train_test_split(X, y, test_size=0.2, stratify=y)`

3. **Preprocess** — encode categoricals, scale numerics (fit on train only)

4. **Pick a model** — start with `LogisticRegression` or `RandomForestClassifier`

5. **Fit** — `model.fit(X_train, y_train)`

6. **Evaluate** — `classification_report`, confusion matrix on held-out test

7. **Save** — `joblib.dump(model, "model.joblib")` for reuse

#Sketch

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
print(classification_report(y_test, clf.predict(X_test)))

#Watch out

Leakage from fitting preprocessors on the full dataset — always pipeline through train split first.

**Try the lessons:** `train-a-tiny-model` · `prediction-game` in Lane D