f in x
Scikit-learn from Scratch: Classification, Regression, and Clustering with Python — Practical Guide
> cd .. / HUB_EDITORIALE
Analisi dei dati e metriche

Scikit-learn from Scratch: Classification, Regression, and Clustering with Python — Practical Guide

[2026-06-05] Author: Ing. Calogero Bono

If you've ever tried machine learning with Python and got lost among DataFrames, arrays, and parameters, you're not alone. We at Meteora Web see it often: developers jumping from one tutorial to another, but when a real dataset lands, they don't know where to start. Let's begin with a concrete problem: you have a CSV of customers and want to predict who will buy, or group similar products. Scikit-learn is the answer. In this guide we show how it's done, why it works, and what it costs in terms of resources.

Why scikit-learn over other frameworks?

It's not a trend. We choose scikit-learn for real projects because it has a mature, stable, well-tested ecosystem. PyTorch and TensorFlow are for deep learning: if you have 1000 rows of tabular data, you're bringing a hydraulic press to crack a nut. Scikit-learn gives you classic models (logistic regression, random forest, SVM, KMeans) with a uniform interface, excellent documentation, and native integration with pandas and numpy. Plus, computational cost is low: you can train a model on a laptop without GPU. For most business problems, it's the right choice.

Classification: from preprocessing to model

Classification assigns a category to an observation. Example: a customer buys (1) or doesn't (0). The workflow is always the same: load data, separate features and target, split into training and test, preprocess, train, evaluate.

Loading and preparing data

We use pandas to read the CSV and numpy for array manipulation. Suppose we have a dataset with columns: age, income, previous_spend, purchased.

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load data
df = pd.read_csv('customers.csv')
X = df[['age', 'income', 'previous_spend']]
y = df['purchased']
# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Common mistake: forgetting to scale features. If income is 50000 and age is 30, models like SVM or logistic regression give more weight to income. The standard scaler (mean=0, std=1) fixes that.

Classification models

Try three models: Logistic Regression (linear, fast, interpretable), Random Forest (non-linear, robust), SVM with RBF kernel (powerful but slower). Scikit-learn has them all with .fit() and .predict().

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
# Pipeline with scaling
from sklearn.pipeline import make_pipeline
models = {
    'LogisticRegression': make_pipeline(StandardScaler(), LogisticRegression()),
    'RandomForest': RandomForestClassifier(n_estimators=100, random_state=42),
    'SVM': make_pipeline(StandardScaler(), SVC(kernel='rbf'))
}
for name, model in models.items():
    model.fit(X_train, y_train)
    acc = model.score(X_test, y_test)
    print(f'{name}: {acc:.3f}')

Metrics that matter: accuracy is fine for balanced classes. With imbalanced data (e.g., 95% don't buy), look at precision, recall, F1-score. Use classification_report and confusion_matrix.

Cross-validation to avoid overfitting

A single split can mislead. Scikit-learn provides cross_val_score for multi-fold evaluation.

from sklearn.model_selection import cross_val_score
scores = cross_val_score(RandomForestClassifier(), X_train, y_train, cv=5)
print(f'Mean accuracy: {scores.mean():.3f} (+/- {scores.std():.3f})')

Regression: predicting continuous values

If instead you want to predict house prices or next month's revenue, you use regression. Same workflow: split, scale, train, evaluate with mean squared error (MSE) or R².

Linear regression and variants

Linear regression is the starting point. But watch out: if you have many correlated features (multicollinearity) or few observations, use Ridge or Lasso (L2 or L1 regularization).

from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.metrics import mean_squared_error, r2_score
model = make_pipeline(StandardScaler(), LinearRegression())
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f'MSE: {mean_squared_error(y_test, y_pred):.2f}')
print(f'R²: {r2_score(y_test, y_pred):.2f}')

Practical tip: use RidgeCV and LassoCV to automatically find the best regularization parameter via cross-validation. Scikit-learn has it all integrated.

Regression evaluation

R² is the proportion of variance explained. An R² of 0.8 means the model explains 80% of variability. MSE is in the same unit as the target. For comparisons, also use MAE (Mean Absolute Error), which is less sensitive to outliers.

Clustering: grouping without labels

When you have no labels (unsupervised learning), you want to find natural groups in the data. Examples: segment customers, group similar items, detect anomalies.

K-Means: simple and fast

K-Means is a workhorse: choose k (number of clusters), assign each point to the nearest centroid, recalculate centroids, repeat. Scikit-learn does it in one line.

from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)  # X scaled!
labels = kmeans.labels_
centers = kmeans.cluster_centers_

How to choose k? Use the elbow method: train with k from 1 to 10 and compute inertia (sum of squared distances within clusters). The point where the curve flattens is the optimal k. Scikit-learn has KMeans.inertia_.

inertias = []
for k in range(1, 11):
    kmeans = KMeans(n_clusters=k, random_state=42)
    kmeans.fit(X_scaled)
    inertias.append(kmeans.inertia_)

Alternatively, use the silhouette score (higher = better separation). Scikit-learn: from sklearn.metrics import silhouette_score.

DBSCAN: density-based clustering

DBSCAN doesn't require specifying k and handles outliers. It identifies dense regions and separates them. Parameters are eps (radius) and min_samples (minimum points to form a core). Great for irregularly shaped data.

from sklearn.cluster import DBSCAN
db = DBSCAN(eps=0.5, min_samples=5)
db.fit(X_scaled)
labels = db.labels_  # -1 = noise (outlier)

Caution: DBSCAN doesn't scale well with many points (complexity O(n²)). For large datasets, use HDBSCAN or sample first.

Going to production: pipelines, saving models, and scaling

A model in a notebook serves no one. To use it in production, create a complete pipeline (scaling + model) and save it with joblib. We at Meteora Web do this for clients with Django dashboards or FastAPI APIs.

import joblib
from sklearn.pipeline import Pipeline
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('classifier', RandomForestClassifier())
])
pipeline.fit(X_train, y_train)
# Save to disk
joblib.dump(pipeline, 'model_customer.pkl')
# Load elsewhere
loaded_pipe = joblib.load('model_customer.pkl')
new_prediction = loaded_pipe.predict(new_data)

Scalability: scikit-learn is not designed for streaming huge data. For datasets larger than RAM, consider IncrementalPCA, SGDClassifier (stochastic), or switch to distributed frameworks. But for most SMEs, 100k rows are manageable.

In summary — what to do now

  1. Install scikit-learn: pip install scikit-learn pandas numpy. That's it.
  2. Pick a small dataset (e.g., Iris, Boston housing from sklearn.datasets) and apply the workflow: load, split, scale, train, evaluate.
  3. Don't forget scaling for all distance- or gradient-based models. Metrics like MSE or accuracy must be interpreted in business context.
  4. Use cross-validation for every important decision (parameters, model selection).
  5. Put it in production with pipelines and joblib. A model is useful only when someone queries it.

We at Meteora Web use scikit-learn daily for data analysis and automation projects. Want to dive deeper? Read our guide on Chain of Thought Prompting to understand how AI reasons, or download the full code of this article from our repository. But remember: the perfect model is useless if it doesn't solve a real problem. Start with the data, measure the return, and only then optimize.

Sponsored Protocol

Ing. Calogero Bono

> AUTHOR_EXTRACTED

Ing. Calogero Bono

Co-founder di Meteora Web. Ingegnere informatico, sviluppo ecosistemi digitali ad alte prestazioni. AI, automazione, SEO tecnica e infrastrutture web. Scrivo di tecnologia per rendere complesso… semplice.

[ Read Full Dossier ]

Hai bisogno di applicare questa strategia?

Esegui il protocollo di contatto per iniziare un progetto con noi.

> INIZIA_PROGETTO

Sponsored

> MW_JOURNAL

> READ_ALL()