Docsity
Top Seller

Guida a un Progetto di Machine Learning: Preprocessing, Modellazione e Valutazione, Dispense di Tecniche Di Intelligenza Artificiale

Toggle sidebar
bg1
Machine-Learning-Project-Guide.md 2025-05-01
1 / 12
B
ack
to
I
n
de
x
🗂
🧪
M
achi
n
e
L
ea
rn
i
n
g
P
ro
jec
t
G
u
ide
W
e
l
c
om
e
to
your
pr
ac
t
ica
l
g
u
ide
f
or
s
e
tt
i
n
g
up
a
m
achi
n
e
-
l
ea
rn
i
n
g
pro
jec
t
i
n
P
yt
h
on
w
i
t
h
t
ab
ul
a
r
da
t
a
—
w
a
l
ki
n
g
you
t
h
rou
gh
e
v
e
ry
e
ss
e
nt
ia
l
st
e
p
f
rom
r
a
w
CS
V
s
to
a
pro
d
u
c
t
i
on
-
r
ead
y
mo
de
l
.
T
hi
s
check
l
i
st
m
a
ps
each
not
eb
oo
k
ce
ll
to
a
c
on
ci
s
e
n
a
rr
a
t
i
v
e
so
you
ca
n
sw
i
t
ch
eff
ortl
e
ssly
be
tw
ee
n
c
o
de
a
n
d
g
u
ida
n
ce
.
I
mports
&
E
nv
i
ronm
e
nt
S
e
tup
B
un
d
l
e
s
e
v
e
ry
c
r
i
t
ica
l
i
mport
a
n
d
s
eed
-
s
e
tt
i
n
g
c
omm
a
n
d
i
nto
on
e
up
f
ront
ce
ll
—
e
l
i
m
i
n
a
t
i
n
g
t
he
h
unt
i
n
g
f
or
s
ca
tt
e
r
ed
i
mports
l
a
t
e
r
a
n
d
e
nsur
i
n
g
each
run
i
s
p
e
r
fec
tly
r
e
pro
d
u
cib
l
e
.
# Add all your necessary imports here
import pandas as pd
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LinearRegression
from sklearn.metrics import accuracy_score, mean_squared_error
from sklearn.model_selection import train_test_split, GridSearchCV
import numpy as np, random, os
# Set the random seeds
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
F
i
l
e
P
a
t
h
C
e
ntr
a
l
i
s
e
s
f
un
da
m
e
nt
a
l
pro
jec
t
c
onst
a
nts
—
da
t
a
s
e
t
p
a
t
h
a
n
d
t
a
r
ge
t
c
olumn
—
i
nto
on
e
lo
ca
t
i
on
.
K
ee
p
i
n
g
t
he
s
e
v
a
r
iab
l
e
s
e
xpl
ici
t
a
n
d
i
n
a
s
i
n
g
l
e
ce
ll
m
i
n
i
m
i
s
e
s
ha
r
d
-
c
o
ded
surpr
i
s
e
s
,
str
ea
ml
i
n
e
s
p
a
r
a
m
e
t
e
r
tw
eak
s
,
a
n
d
e
nsur
e
s
e
v
e
ry
d
ownstr
ea
m
s
c
r
i
pt
r
efe
r
e
n
ce
s
t
he
e
x
ac
t
s
a
m
e
c
on
fig
ur
a
t
i
on
.
FILE_PATH = "./data/features.csv" # e.g.: CSV directory
TARGET_COL = "target" # Replace with your column name
L
o
ad
D
a
t
a
s
e
t
L
o
ad
s
t
he
r
a
w
CS
V
i
nto
m
e
mory
a
n
d
i
mm
edia
t
e
ly
s
e
p
a
r
a
t
e
s
fea
tur
e
s
f
rom
t
he
t
a
r
ge
t
c
olumn
f
or
d
ownstr
ea
m
pro
ce
ss
i
n
g
.
pf3
pf4
pf5
pf8
pf9
pfa

Anteprima parziale del testo

Scarica Guida a un Progetto di Machine Learning: Preprocessing, Modellazione e Valutazione e più Dispense in PDF di Tecniche Di Intelligenza Artificiale solo su Docsity!

Back to Index 🗂

🧪 Machine Learning Project Guide

Welcome to your practical guide for setting up a machine-learning project in Python with tabular data—

walking you through every essential step from raw CSVs to a production-ready model. This checklist maps

each notebook cell to a concise narrative so you can switch effortlessly between code and guidance.

Imports & Environment Setup

Bundles every critical import and seed-setting command into one upfront cell—eliminating the hunting for

scattered imports later and ensuring each run is perfectly reproducible.

Add all your necessary imports here

import pandas as pd from sklearn.preprocessing import OneHotEncoder, LabelEncoder from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.linear_model import LinearRegression from sklearn.metrics import accuracy_score, mean_squared_error from sklearn.model_selection import train_test_split, GridSearchCV import numpy as np, random, os

Set the random seeds

SEED = 42 random.seed(SEED) np.random.seed(SEED)

File Path

Centralises fundamental project constants—dataset path and target column—into one location. Keeping

these variables explicit and in a single cell minimises hard-coded surprises, streamlines parameter tweaks,

and ensures every downstream script references the exact same configuration.

FILE_PATH = "./data/features.csv" # e.g.: CSV directory TARGET_COL = "target" # Replace with your column name

Load Dataset

Loads the raw CSV into memory and immediately separates features from the target column for

downstream processing.

Load the raw data set from disk and handle a missing-file error

gracefully try:

Attempt to read the CSV file located at FILE_PATH

data = pd.read_csv(FILE_PATH) print(f"Data successfully loaded from {FILE_PATH}.")

Raised if the file does not exist at the specified path

except FileNotFoundError: raise FileNotFoundError(f"File not found: {FILE_PATH}")

Attempt to separate features and target in the DataFrame

try:

Drop the target column to obtain the feature matrix X

X = data.drop(columns=[TARGET_COL])

Extract the target vector y

y = data[TARGET_COL]

Basic sanity-check prints

print(f"Features (X) shape: {X.shape}") # rows × feature-columns print(f"Target (y) shape: {y.shape}") # rows × 1 (or Series length)

Handle the case where the provided TARGET_COL is not present

except KeyError: raise KeyError(f"Target column '{TARGET_COL}' not found in the dataset.")

Data Exploration and Visualization

Generates two high-impact plots to ground your intuition before modelling.

Class distribution chart: A seaborn.countplot shows how many samples belong to each target class (in case you are facing a classification task) or target values (in case you are facing a regression task). This quickly surfaces any class imbalance that may require resampling, re-weighting, or alternative evaluationmetrics.

plt.figure(figsize=(8, 5)) sns.countplot(x=y, palette='viridis')

Add value counts on top of the bars

for i, v in enumerate(pd.Series(y).value_counts()): plt.text(i, v, str(v), ha='center', va='bottom', fontsize=12)

plt.title('Distribution of Classes', fontsize=16) plt.xlabel('Class', fontsize=14)

Fit the encoder on the categorical columns and transform them

X_encoded = encoder.fit_transform(X[categorical_columns])

Get the generated one-hot column names, e.g. "color_red",

"color_blue" encoded_columns = encoder.get_feature_names_out(categorical_columns)

Wrap the encoded array in a DataFrame aligned to the original index

X_encoded_df = pd.DataFrame(X_encoded, columns=encoded_columns, index=X.index)

Replace the original categorical columns with their one-hot

expansion X = pd.concat([X.drop(columns=categorical_columns), X_encoded_df], axis=1)

Encode the target vector y if it is categorical

if y.dtype in ['object', 'category']:

Multiclass target: more than two unique labels

if len(y.unique()) > 2:

One-hot encode y, producing one column per class

encoder_y = OneHotEncoder(sparse_output=False) y_encoded = encoder_y.fit_transform(y.values.reshape(-1, 1)) encoded_columns_y = encoder_y.get_feature_names_out([TARGET_COL])

Convert back to DataFrame for consistent indexing

y = pd.DataFrame(y_encoded, columns=encoded_columns_y, index=y.index) else:

Binary target: encode as 0/1 with LabelEncoder

encoder_y = LabelEncoder() y = pd.Series(encoder_y.fit_transform(y), index=y.index)

Missing-value strategies: such as mean/median imputation, learned models, or flagging with a “missing” indicator—ensure that the absence of a value doesnʼt silently skew results or shrink the training set. Proper handling keeps useful rows in play, reduces variance, and helps the model learn any predictive signal hidden in the pattern of missingness itself. The logic implemented in our example is:

. Percentage of Missing Values:

→ 0 %: No action needed → >70%: Drop the column entirely → 5-70%: Create missing indicator + imputation → <5%: Simple imputation

. Data Type:

Numerical Columns: → Imputation using median values → Missing indicators for columns with >5% missing

Categorical Columns:

→ Imputation using mode (most frequent value) for low missingness

→ Imputation with 'Missing' category for higher missingness

→ Missing indicators for columns with >5% missing

Summarize the amount of missing data in every feature

missing_stats = pd.DataFrame({

Total count of NaNs per column

'Total Missing': X.isnull().sum(),

Percentage of NaNs relative to the full data set

'Percent Missing': (X.isnull().sum() / len(X) * 100).round(2) })

Display only the columns that actually contain missing values,

ordered by % missing missing_stats[missing_stats['Total Missing'] > 0].sort_values( 'Percent Missing', ascending=False )

Decide how to handle each column’s missing data

if len(missing_stats) > 0: print("Missing Value Statistics:") print(missing_stats) print("\nShape before handling missing values:", X.shape)

Iterate over every column in the feature matrix

for column in X.columns:

Percentage of missing values in this column

missing_pct = (X[column].isnull().sum() / len(X)) * 100

Skip columns with no missing data

if missing_pct == 0: continue

Drop columns with > 70 % missing

elif missing_pct > 70: X = X.drop(columns=[column]) print(f"\nDropped column '{column}' with {missing_pct:.1f}% missing values")

Numeric columns

elif pd.api.types.is_numeric_dtype(X[column]): if missing_pct < 5:

Low missingness → simple median imputation

X[column] = X[column].fillna(X[column].median()) print(f"\nImputed '{column}' with median") else:

Higher missingness → add flag then median-impute

X[f'{column}_missing'] = X[column].isnull().astype(int) X[column] = X[column].fillna(X[column].median())

model = RandomForestClassifier(random_state=RANDOM_SEED)

grid_params = { 'model__n_estimators': [50, 100, 200], 'model__max_depth': [5, 10, 20], }

model = SVC(random_state=RANDOM_SEED)

grid_params = { 'model__C': [0.1, 1, 10], 'model__kernel': ['linear', 'rbf', 'poly'], 'model__gamma': ['scale', 'auto'], }

model = MLPClassifier(random_state=RANDOM_SEED)

grid_params = { 'model__hidden_layer_sizes': [(50,), (100,), (50, 50)], 'model__activation': ['relu', 'tanh'], 'model__solver': ['adam', 'sgd'], 'model__alpha': [0.0001, 0.001, 0.01], }

model = LinearRegression()

grid_params = { 'model__fit_intercept': [True, False], 'model__normalize': [True, False] }

model = SVR()

grid_params = { 'model__C': [0.1, 1, 10], 'model__kernel': ['linear', 'rbf', 'poly'], 'model__gamma': ['scale', 'auto'], }

Building Pipelines

A pipeline is a conveyor belt that feeds your raw features through a fixed sequence of transformations and

finally into an estimator. Wrapping the whole journey in a single scikit-learn Pipeline object delivers cleaner

code and deplyment portability

Inside the pipeline, every step is an independent transformer and you can build them as you prefere. A good

structure is:

Feature scaling: Puts numerical variables on a common scale so that algorithms that rely on distance or gradient magnitude donʼt get dominated by large-unit features. Dimensionality reduction: Shrinks the feature space, improving interpretability and often boosting generalisation by cutting noise. Two broad flavours are Feature Selection, based on different statistical tests, or Principal Component Analysis. Model: the predefined model to investigate.

Here two pipeline examples:

Pipeline 1: Standardise → Select top-K features → Train model

NUM_SELECTED_FEATURES = 2 # keep only the two most informative predictors

pipeline = Pipeline([

Feature scaling: centre each numeric column at μ=0 and scale to σ=

('scaler', StandardScaler()),

Univariate feature selection:

- f_classif = ANOVA F-test (categorical target vs continuous

feature)

- k = how many best-scoring features to retain

('feature_selection', SelectKBest(f_classif, k=NUM_SELECTED_FEATURES)),

Estimator placeholder (any classifier/regressor assigned to model)

('model', model) ])

Pipeline 2: Standardise → PCA dimensionality reduction → Model

VARIANCE = 0.95 # retain 95 % of the original variance in the principal components

pipeline = Pipeline([

Feature scaling (required before PCA to give all dimensions equal

weight) ('scaler', StandardScaler()),

Principal Component Analysis:

- n_components = 0.95 → keep enough components to explain 95 %

variance ('pca', PCA(n_components=VARIANCE)),

NUM_FOLD = 5 # k-fold cross-validation

Stratified train/test split so each class keeps the same proportion

X_train, X_test, y_train, y_test = train_test_split( X, y, train_size = TRAIN_SIZE, stratify = y, random_state = RANDOM_SEED )

Grid-search object that will tune the hyper-parameters in grid_params

grid_search = GridSearchCV( estimator = pipeline, # full preprocessing+model chain param_grid = grid_params, # dictionary of parameter ranges scoring = 'accuracy', # or 'neg_mean_squared_error' for regression tasks cv = NUM_FOLD, # number of CV folds n_jobs = -1 # run folds in parallel on all CPU cores )

Run the exhaustive search across folds

grid_search.fit(X_train, y_train)

Pipeline retrained on the full CV training split with the best

parameters best_pipeline = grid_search.best_estimator_

Collect mean and std of test scores for every parameter set

cv_results = pd.DataFrame(grid_search.cv_results_) mean_cv_scores = cv_results['mean_test_score'] std_cv_scores = cv_results['std_test_score']

We can inspect the results for each fold based on the task:

Classification:

print("Cross-Validation Results:") for i, (mean, std) in enumerate(zip(mean_cv_scores, std_cv_scores)): print(f"Parameter set {i+1}: Mean Accuracy = {mean:.4f}, Std = {std:.4f}")

Regression:

print("Cross-Validation Results:") for i, (mean, std) in enumerate(zip(mean_cv_scores, std_cv_scores)): print(f"Parameter set {i+1}: Mean MSE = {mean:.4f}, Std = {std:.4f}")

Evaluation

After hyper-parameter tuning we lock the winning pipeline and run it once on the held-out test set—data

that played no part in training or cross-validation. This single pass simulates real-world deployment:

features go through the exact same preprocessing, the estimator produces predictions, and we compare

them to the true labels.

For classification we inspect overall accuracy, a per-class precision/recall/F 1 report, and a confusion matrix that spotlights where the model confuses classes.

Generate predictions for the unseen test features

y_pred = best_pipeline.predict(X_test)

Compute overall accuracy

accuracy = accuracy_score(y_test, y_pred)

Detailed precision / recall / F1 per class

report = classification_report(y_test, y_pred)

Confusion matrix counts true vs. predicted labels

conf_matrix = confusion_matrix(y_test, y_pred)

print(f"\nTest Accuracy: {accuracy:.4f}") print("\nClassification Report:") print(report)

Visualise the confusion matrix for easier interrogation

plt.figure(figsize=(8, 6)) sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False, xticklabels=[f"Class {i}" for i in range(len(conf_matrix))], yticklabels=[f"Class {i}" for i in range(len(conf_matrix))]) plt.xlabel('Predicted Labels') plt.ylabel('True Labels') plt.title('Confusion Matrix') plt.show()

For regression we look at error magnitudes (MSE & MAE) and the proportion of variance explained (R²). These metrics provide an honest snapshot of generalisation performance and guide any final tweaks or business decisions.

from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

Predict numeric targets for the test set

y_pred = best_pipeline.predict(X_test)