Machine Learning Basics
10 examples to get you started with Classical ML - 7 basic and 3 intermediate.
Prerequisites
uv venv && source .venv/bin/activate
uv pip install "pandas>=2.2" "scikit-learn>=1.5" "numpy>=2.0"- Python 3.14.0 with an isolated virtual environment.
- pandas 2.2+ for tabular data; scikit-learn for classical ML algorithms and pipelines.
Basic Examples
1. Load Data into a DataFrame
Start every project by inspecting shape, dtypes, and missing values.
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris(as_frame=True)
df = iris.frame.assign(species=iris.target_names[iris.target])
print(df.shape, df.dtypes)
print(df.isna().sum())as_frame=Truereturns a pandas DataFrame directly from sklearn datasets.- Check
df.info()anddf.describe()before modeling. - Record the dataset version or source URL for reproducibility.
Related: Datasets & Splits - train/test/validation splits
2. Separate Features and Target
Define X (inputs) and y (label) explicitly.
X = df.drop(columns=["species"])
y = df["species"]
feature_names = list(X.columns)- Features must exclude the target column and any post-outcome columns.
- Keep feature names as a list for later inspection and explainability.
- Categorical targets can stay as strings until an encoder is fitted.
Related: Feature Engineering - encoding and scaling
3. Train/Test Split
Hold out unseen data before fitting anything.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)stratify=ypreserves class proportions in classification tasks.- Fit preprocessors and models on
X_trainonly - never on the full dataset. - The test set is touched once at the end for an unbiased score.
Related: Datasets & Splits - cross-validation
4. Fit a Baseline Classifier
Establish a simple benchmark before tuning complex models.
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
clf = LogisticRegression(max_iter=1000, random_state=42)
clf.fit(X_train, y_train)
preds = clf.predict(X_test)
print("accuracy:", accuracy_score(y_test, preds))- Logistic regression is a strong linear baseline for tabular classification.
- Compare every new model against this baseline score.
max_iter=1000avoids convergence warnings on some datasets.
Related: Supervised Learning - algorithm choices
5. Scale Numeric Features
Many algorithms expect similarly scaled inputs.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test) # fit on train onlyfitlearns mean and std from training data;transformapplies them.- Never call
fit_transformon the test set - that leaks statistics. - Wrap scaling in a
Pipelineto prevent manual mistakes.
Related: Feature Engineering -
ColumnTransformer
6. Build a Leak-Free Pipeline
Chain preprocessing and model into one estimable object.
from sklearn.pipeline import Pipeline
pipe = Pipeline([
("scaler", StandardScaler()),
("clf", LogisticRegression(max_iter=1000, random_state=42)),
])
pipe.fit(X_train, y_train)
print("pipeline accuracy:", accuracy_score(y_test, pipe.predict(X_test)))- A
Pipelineensures test data never influences preprocessing statistics. pipe.predict(X_test)runs scaling then classification in one call.- Serialize the whole pipeline for deployment, not just the model.
Related: Pipelines - composing steps
7. Cross-Validate for Stable Scores
Single train/test splits can be noisy; CV averages multiple folds.
from sklearn.model_selection import cross_val_score
scores = cross_val_score(pipe, X_train, y_train, cv=5, scoring="accuracy")
print(f"CV mean: {scores.mean():.3f} +/- {scores.std():.3f}")- Cross-validation runs on training data only - the test set stays locked.
- Report mean and standard deviation, not just the best fold.
- Use
scoringappropriate to the problem (F1, ROC-AUC, etc.).
Related: Model Evaluation - metrics and curves
Intermediate Examples
8. Encode Categorical Features
Mixed numeric and categorical columns need targeted transforms.
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
num_cols = ["sepal length (cm)", "sepal width (cm)"]
cat_cols = ["petal length (cm)"] # example: treat one column as categorical
preprocessor = ColumnTransformer([
("num", StandardScaler(), num_cols),
("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols),
])ColumnTransformerapplies different transforms to different column groups.handle_unknown="ignore"prevents crashes on unseen categories at inference.- Fit the preprocessor inside a pipeline, never on the full dataset upfront.
Related: Feature Engineering - full encoding patterns
9. Tune Hyperparameters with Grid Search
Search parameter combinations inside cross-validation.
from sklearn.model_selection import GridSearchCV
param_grid = {"clf__C": [0.1, 1.0, 10.0]}
search = GridSearchCV(pipe, param_grid, cv=5, scoring="accuracy")
search.fit(X_train, y_train)
print("best params:", search.best_params_)
print("test accuracy:", accuracy_score(y_test, search.predict(X_test)))- Prefix pipeline step params with the step name (
clf__C). GridSearchCVpicks the best params by CV score on training data.- Evaluate the winner once on the held-out test set.
Related: Hyperparameter Tuning - Optuna and random search
10. Save and Load a Trained Pipeline
Persist the full artifact for reproducible inference.
import joblib
joblib.dump(pipe, "iris_pipeline.joblib")
loaded = joblib.load("iris_pipeline.joblib")
print(loaded.predict(X_test[:3]))- Save the entire pipeline so preprocessing travels with the model.
- Version the artifact alongside training data hash and random seed.
- Load and predict with the same pandas column names used during training.
Related: Pipelines - serialization and deployment
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.