Monday, August 3, 2026
HomeData ScienceScikit-learn Tutorial: Machine Learning in Python from Scratch (2026)

Scikit-learn Tutorial: Machine Learning in Python from Scratch (2026)

Table of Content

Scikit-learn Tutorial: Machine Learning in Python from Scratch (2026)

Scikit-learn is the most widely used ML library in Python. It gives you clean, consistent APIs for dozens of algorithms, plus preprocessing, model selection, and evaluation tools. This guide covers everything from raw data to a deployed model.

The fit / predict / transform API

a white board with writing written on it
Photo by Bernd 📷 Dittrich on Unsplash
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler

model = LogisticRegression()
model.fit(X_train, y_train)          # learn from training data
preds = model.predict(X_test)        # make predictions
proba = model.predict_proba(X_test)  # class probabilities

scaler = StandardScaler()
scaler.fit(X_train)                  # compute mean/std from train set only
X_train_s = scaler.transform(X_train)
X_test_s  = scaler.transform(X_test)  # reuse train stats on test

End-to-End Example

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report

data = load_breast_cancer(as_frame=True)
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
print('RF Acc:', rf.score(X_test, y_test))
print(classification_report(y_test, rf.predict(X_test), target_names=data.target_names))

Pipelines: Prevent Data Leakage

a train traveling through a forest filled with lots of trees
Photo by Wolfgang Weiser on Unsplash
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC

# Correct -- Pipeline fits scaler only on training data:
pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('svm', SVC(kernel='rbf', probability=True))
])
pipe.fit(X_train, y_train)
print('Pipeline Acc:', pipe.score(X_test, y_test))

Cross-Validation

from sklearn.model_selection import cross_val_score, StratifiedKFold
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(pipe, X, y, cv=cv, scoring='accuracy')
print(f'CV Accuracy: {scores.mean():.4f} +/- {scores.std():.4f}')

Preprocessing Toolkit

from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder

preprocessor = ColumnTransformer([
    ('num', Pipeline([('imp', SimpleImputer(strategy='median')), ('sc', StandardScaler())]), ['age','income']),
    ('cat', Pipeline([('imp', SimpleImputer(strategy='most_frequent')), ('enc', OneHotEncoder(handle_unknown='ignore'))]), ['city','gender']),
])

Hyperparameter Tuning

from sklearn.model_selection import GridSearchCV
param_grid = {'svm__C':[0.1,1,10], 'svm__gamma':['scale','auto',0.01,0.1]}
gs = GridSearchCV(pipe, param_grid, cv=5, n_jobs=-1)
gs.fit(X_train, y_train)
print('Best:', gs.best_params_)

10 Algorithms to Know

LogisticRegression for fast interpretable classification. Ridge and Lasso for regularised regression. SVC for powerful medium-size datasets. DecisionTreeClassifier for interpretability. RandomForestClassifier as an excellent default. GradientBoostingClassifier for powerful boosting. KNeighborsClassifier for simple baselines. GaussianNB for fast text. KMeans for clustering. PCA for dimensionality reduction.

Conclusion

Master fit/predict/transform, use Pipelines to prevent data leakage, cross-validate for reliable evaluation, and GridSearchCV for tuning. These four concepts let you apply any of scikit-learn’s 50+ estimators confidently.

Leave feedback about this

  • Rating

Latest Posts

List of Categories