Gli steps di una analisi


Dai dati alla previsione

Non c'è un vero e proprio digramma da seguire per poter fare una analisi e l'imprevisto è sempre dietro l'angolo, detto questo potrebbe essere utile avere una serie di linee guida per poter inquadrare il problema.






Python in Practice

Below we demonstrate a complete analysis pipeline: from data exploration to model evaluation.

1. Data Exploration

import numpy as np
from sklearn.datasets import make_classification

np.random.seed(42)
X, y = make_classification(n_samples=500, n_features=8, n_informative=5,
                           n_redundant=2, random_state=42)

print(f"Dataset shape: {X.shape}")
print(f"Class balance: {np.bincount(y)}")
print(f"\nFeature statistics:")
print(f"  Mean range: [{X.mean(axis=0).min():.2f}, {X.mean(axis=0).max():.2f}]")
print(f"  Std range:  [{X.std(axis=0).min():.2f}, {X.std(axis=0).max():.2f}]")
# Check for missing values
print(f"  Missing values: {np.isnan(X).sum()}")
# Output:
# Dataset shape: (500, 8)
# Class balance: [250 250]
#
# Feature statistics:
#   Mean range: [-0.12, 0.08]
#   Std range:  [0.98, 2.45]
#   Missing values: 0

2. Train/Test Split and Preprocessing

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
print(f"Train: {X_train.shape[0]} samples, Test: {X_test.shape[0]} samples")

scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = scaler.transform(X_test)  # use train statistics!
print(f"After scaling - Train mean: {X_train_s.mean():.4f}, std: {X_train_s.std():.4f}")
# Output:
# Train: 350 samples, Test: 150 samples
# After scaling - Train mean: 0.0000, std: 1.0000

3. Model Selection and Evaluation

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report

models = {
    'Logistic Regression': LogisticRegression(max_iter=1000, random_state=42),
    'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42)
}

print("Cross-validation results:")
for name, model in models.items():
    scores = cross_val_score(model, X_train_s, y_train, cv=5)
    print(f"  {name}: {scores.mean():.3f} +/- {scores.std():.3f}")

# Final evaluation on test set
best_model = RandomForestClassifier(n_estimators=100, random_state=42)
best_model.fit(X_train_s, y_train)
y_pred = best_model.predict(X_test_s)
print(f"\nTest set accuracy: {(y_pred == y_test).mean():.3f}")
# Output:
# Cross-validation results:
#   Logistic Regression: 0.886 +/- 0.025
#   Random Forest: 0.926 +/- 0.019
#
# Test set accuracy: 0.920

Results