Regolare i pesi dell’algoritmo

Rilevamento delle frodi in Python

Charlotte Werger

Data Scientist

Pesi bilanciati

model = RandomForestClassifier(class_weight='balanced')
model = RandomForestClassifier(class_weight='balanced_subsample')
model = LogisticRegression(class_weight='balanced')
model = SVC(kernel='linear', class_weight='balanced', probability=True)
Rilevamento delle frodi in Python

Tuning degli iperparametri per il rilevamento frodi

model = RandomForestClassifier(class_weight={0:1,1:4},random_state=1)

model = LogisticRegression(class_weight={0:1,1:4}, random_state=1)
model = RandomForestClassifier(n_estimators=10, 
                               criterion='gini', 
                               max_depth=None, 
                               min_samples_split=2, 
                               min_samples_leaf=1, 
                               max_features='auto',
                               n_jobs=-1, 
                               class_weight=None)
Rilevamento delle frodi in Python

Uso di GridSearchCV

from sklearn.model_selection import GridSearchCV

# Crea la griglia dei parametri param_grid = { 'max_depth': [80, 90, 100, 110], 'max_features': [2, 3], 'min_samples_leaf': [3, 4, 5], 'min_samples_split': [8, 10, 12], 'n_estimators': [100, 200, 300, 1000] }
# Definisci il modello da usare model = RandomForestRegressor()
# Istanzia il modello di grid search grid_search_model = GridSearchCV(estimator = model, param_grid = param_grid, cv = 5, n_jobs = -1, scoring='f1')
Rilevamento delle frodi in Python

Trovare il modello migliore con GridSearchCV

# Esegui il fit della grid search
grid_search_model.fit(X_train, y_train)

# Ottieni i parametri ottimali grid_search_model.best_params_
{'bootstrap': True,
 'max_depth': 80,
 'max_features': 3,
 'min_samples_leaf': 5,
 'min_samples_split': 12,
 'n_estimators': 100}
# Risultati del best_estimator
grid_search.best_estimator_
grid_search.best_score_
Rilevamento delle frodi in Python

Esercitiamoci!

Rilevamento delle frodi in Python

Preparing Video For Download...