BaggingClassifier: por dentro

Métodos de Ensemble em Python

Román de las Heras

Data Scientist, Appodeal

Funções heterogêneas vs homogêneas

Função de ensemble heterogêneo

het_est = HeterogeneousEnsemble(
    estimators=[('est1', est1), ('est2', est2), ...],
    # additional parameters
)

Função de ensemble homogêneo

hom_est = HomogeneousEnsemble(
    est_base,
    n_estimators=chosen_number,
    # additional parameters
)
Métodos de Ensemble em Python

BaggingClassifier

Exemplo de Bagging Classifier:

# Instantiate the base estimator ("weak" model)
clf_dt = DecisionTreeClassifier(max_depth=3)
# Build the Bagging classifier with 5 estimators
clf_bag = BaggingClassifier(
    clf_dt,
    n_estimators=5
)
# Fit the Bagging model to the training set
clf_bag.fit(X_train, y_train)
# Make predictions on the test set
y_pred = clf_bag.predict(X_test)
Métodos de Ensemble em Python

BaggingRegressor

Exemplo de Bagging Regressor:

# Instantiate the base estimator ("weak" model)
reg_lr = LinearRegression()
# Build the Bagging regressor with 10 estimators
reg_bag = BaggingRegressor(
    reg_lr
)
# Fit the Bagging model to the training set
reg_bag.fit(X_train, y_train)
# Make predictions on the test set
y_pred = reg_bag.predict(X_test)
Métodos de Ensemble em Python

Pontuação out-of-bag

  • Calcular as previsões individuais usando todos os estimadores para os quais a instância ficou fora da amostra
  • Combinar as previsões individuais
  • Avaliar a métrica nessas previsões:
    • Classificação: accuracy
    • Regressão: R^2
clf_bag = BaggingClassifier(
    clf_dt,
    oob_score=True
)
clf_bag.fit(X_train, y_train)
print(clf_bag.oob_score_)
0.9328125
pred = clf_bag.predict(X_test)
print(accuracy_score(y_test, pred))
0.9625
Métodos de Ensemble em Python

Agora é com você!

Métodos de Ensemble em Python

Preparing Video For Download...