-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_generator.py
More file actions
36 lines (28 loc) · 1.16 KB
/
model_generator.py
File metadata and controls
36 lines (28 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# model_generator.py
# Questo script allena un modello semplice e lo salva in un file .pkl
# per essere usato dalla nostra API. Non fa parte del servizio, è solo propedeutico.
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
import joblib
print("1. Creazione del dataset fittizio e training del modello...")
# Creiamo un dataset di esempio per un problema di churn (abbandono clienti)
# Features: eta, sessioni_mensili, spesa_totale
X, y = make_classification(
n_samples=1000,
n_features=3,
n_informative=2,
n_redundant=0,
n_classes=2,
random_state=42
)
# Alleniamo un modello di classificazione
model = RandomForestClassifier(random_state=42)
model.fit(X, y)
print("2. Modello allenato con successo.")
# Salviamo il modello addestrato in un file
joblib.dump(model, 'modello.pkl')
print("3. Modello salvato come 'modello.pkl'.")
# Test rapido per verificare che il modello funzioni
test_input = [[35, 20, 500]] # Esempio: [eta, sessioni_mensili, spesa_totale]
prediction = model.predict(test_input)
print(f"\nVerifica: una previsione per l'input {test_input} darebbe come risultato: {prediction[0]}")