A Hands-On Guide to scikit-learn: From Data Preparation to Ensemble Models

scikit-learn, commonly imported as sklearn, is an open-source machine learning library for Python. It builds on NumPy, SciPy, and matplotlib to provide efficient, well-tested implementations of many popular algorithms. The library is designed around three core principles: consistency of its estimator interface, inspection of learned parameters, and easy composition via pipelines. These principles make it a reliable tool for tasks ranging from simple linear models to advanced ensemble methods.

Installation

pip install scikit-learn

Basic Workflow

A typical machine learning task follows a predictable flow: loading data, splitting it, preprocessing, training a model, and evalutaing. Below is a compact example using the digits dataset:

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report

data = load_digits()
X, y = data.data, data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

clf = LogisticRegression(max_iter=5000)
clf.fit(X_train_scaled, y_train)

y_pred = clf.predict(X_test_scaled)
print(classification_report(y_test, y_pred))

Core Components and Common Methods

Data Preprocessing

Feature Scaling

Standardising and normalising features helps gradient‑based and distance‑based algorithms.

from sklearn.preprocessing import StandardScaler, MinMaxScaler
import numpy as np

X = np.random.randn(100, 3) * 10 + 5   # 100 samples, 3 features

# Standardisation (mean=0, std=1)
std_scaler = StandardScaler()
X_std = std_scaler.fit_transform(X)

# Min‑max scaling (range [0, 1])
mm_scaler = MinMaxScaler()
X_mm = mm_scaler.fit_transform(X)

Caution: Fit the scaler only on the training data, then use that same object to transform validation or test sets. Never call fit_transform on30 the17 test21 set.

Encoding Categorical Variables

Convert categories to numeric representations.

from sklearn.preprocessing import OneHotEncoder, LabelEncoder

# One‑hot encoding
categories = np.array([['red', 'circle'], ['blue', 'square'], ['green', 'circle']])
ohe = OneHotEncoder(sparse_output=False)
encoded = ohe.fit_transform(categories)
print(ohe.get_feature_names_out())

# Label encoding for targets
labels = np.array(['small', 'medium', 'large', 'small', 'medium'])
le = LabelEncoder()
integer_labels = le.fit_transform(labels)
print(le.classes_)

Feature Selection

Univariate Selection

SelectKBest retains the top features based on a scoring function.

from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.datasets import load_wine

X_wine, y_wine = load_wine(return_X_y=True)
selector = SelectKBest(score_func=f_classif, k=5)
X_selected = selector.fit_transform(X_wine, y_wine)

Recursive Feature Elimination

RFE removes features iteratively using an external estimator.

from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier

rfe = RFE(estimator=RandomForestClassifier(random_state=7), n_features_to_select=8)
X_rfe = rfe.fit_transform(X_wine, y_wine)

Model Selection and Evaluation

from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, f1_score

X_train, X_val, y_train, y_val = train_test_split(X_wine, y_wine, test_size=0.3, stratify=y_wine, random_state=1)

# Cross‑validation
clf = LogisticRegression(max_iter=3000)
scores = cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy')
print(f'CV accuracy: {scores.mean():.2f}')

# Hold‑out evaluation
clf.fit(X_train, y_train)
preds = clf.predict(X_val)
print(f'Accuracy: {accuracy_score(y_val, preds):.2f}')
print(confusion_matrix(y_val, preds))

Supervised Learning

Classification

sklearn includes a wide range of classifiers:

from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB

models = {
    'SVM': SVC(kernel='rbf'),
    'kNN': KNeighborsClassifier(n_neighbors=7),
    'RF': RandomForestClassifier(n_estimators=120, random_state=13),
    'NB': GaussianNB()
}

for name, model in models.items():
    model.fit(X_train, y_train)
    acc = accuracy_score(y_val, model.predict(X_val))
    print(f'{name}: {acc:.2f}')

Regression

from sklearn.linear_model import LinearRegression, Ridge
from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import make_regression

X_reg, y_reg = make_regression(n_samples=200, n_features=6, noise=0.2, random_state=99)
X_r_train, X_r_test, y_r_train, y_r_test = train_test_split(X_reg, y_reg, test_size=0.2, random_state=99)

lin_reg = LinearRegression()
lin_reg.fit(X_r_train, y_r_train)

rf_reg = RandomForestRegressor(n_estimators=100, random_state=42)
rf_reg.fit(X_r_train, y_r_train)

print(f'Linear R^2 on test: {lin_reg.score(X_r_test, y_r_test):.3f}')
print(f'RF R^2 on test: {rf_reg.score(X_r_test, y_r_test):.3f}')

Unsupervised Learning – Clustering

from sklearn.cluster import KMeans, AgglomerativeClustering
from sklearn.datasets import make_blobs

X_blob, y_blob = make_blobs(n_samples=300, centers=4, n_features=3, random_state=0)

kmeans = KMeans(n_clusters=4, n_init='auto', random_state=0)
labels_km = kmeans.fit_predict(X_blob)

agg = AgglomerativeClustering(n_clusters=4)
labels_agg = agg.fit_predict(X_blob)

Hyperparameter Tuning

Systematically searching for the best hyperparameters can dramatically improve performance.

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV

param_grid = {
    'kernel': ['linear', 'rbf', 'poly'],
    'C': [0.1, 1, 10],
    'gamma': ['scale', 'auto']
}
grid = GridSearchCV(SVC(), param_grid, cv=5, scoring='accuracy', n_jobs=-1)
grid.fit(X_train, y_train)
print(f'Best params: {grid.best_params_}')

# Randomised search – useful when the parameter space is large
from scipy.stats import uniform, randint
param_dist = {
    'n_estimators': randint(50, 300),
    'max_depth': [5, 10, 20, None],
    'learning_rate': uniform(0.01, 0.3)
}
random_s = RandomizedSearchCV(
    RandomForestRegressor(random_state=0),
    param_distributions=param_dist,
    n_iter=20, cv=4, random_state=42
)
random_s.fit(X_r_train, y_r_train)

Pipelines

Pipelines chain preprocessing and modeling steps, ensuring the same transformations are applied for training and prediction.

from sklearn.pipeline import Pipeline

pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('clf', LogisticRegression(max_iter=2000))
])

param_grid_pipe = {'clf__C': [0.01, 0.1, 1.0]}
pipe_cv = GridSearchCV(pipe, param_grid_pipe, cv=5)
pipe_cv.fit(X_train, y_train)
print(f'Best pipeline accuracy: {pipe_cv.best_score_:.2f}')

Ensemble Methods

Combining multiple learners often leads to better generalisation.

from sklearn.ensemble import VotingClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier

# Voting ensemble
vote_clf = VotingClassifier(
    estimators=[
        ('lr', LogisticRegression(max_iter=2000)),
        ('rf', RandomForestClassifier(n_estimators=100, random_state=1)),
        ('svm', SVC(kernel='rbf', probability=True))
    ],
    voting='soft'  # uses predicted probabilities
)
vote_clf.fit(X_train, y_train)
print(f'Voting accuracy: {accuracy_score(y_val, vote_clf.predict(X_val)):.2f}')

# AdaBoost
ada = AdaBoostClassifier(
    estimator=DecisionTreeClassifier(max_depth=2),
    n_estimators=150,
    learning_rate=0.8,
    random_state=14
)
ada.fit(X_train, y_train)

Model Persistence

Save trained models for later use without retraining.

import joblib

joblib.dump(pipe_cv.best_estimator_, 'final_model.pkl')
loaded_model = joblib.load('final_model.pkl')

Key Recommendations

  • Always split data before any preprocessing to avoid data leakage.
  • Use stratify in train_test_split for imbalanced classification tasks.
  • Choose metric carefully: accuracy can be misleading for skewed class distributions; prefer precision, recall, or F1-score where appropriate.
  • Scale features for disatnce‑based models (kNN, SVM) and gradient‑based optimisation.
  • Utilise n_jobs=-1 in grid search or ensemble methods to leverage multiple CPU cores.
  • Encoding:awn categoricals with OneHotEncoder for most models; label encoding is mostly for tree‑based models (but12 still17 requires caution).
  • When17 using pipelines for hyperparameter search, the parameter names follow the pattern <step_name>__<parameter_name> (double underscore).

Community and Resources

The scikit‑learn project offers12 extensive12 documentation with13 tutorials, a user guide, and42 API reference on its official website. The source code and issue tracker are hosted on GitHub. User and developer mailing lists provide13 discussion forums, and the scikit-learn tag on Stack Overflow is very active. Together, these resources form a supportive ecosystem for both beginners and advanced practitioners.

Tags: scikit-learn Machine Learning python Data Science model evaluation

Posted on Sat, 11 Jul 2026 17:22:19 +0000 by bitt3n