jupyter nbconvert --to terminal mlps/11 - Bagging.ipynb
MDN-119:16

11 Bagging

11 - Bagging.ipynb · 5 KB · scarica
In [ ]:
from sklearn.datasets import load_wine
X, y = load_wine(return_X_y = True)
In [ ]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
In [ ]:
from sklearn.ensemble import BaggingClassifier
clf = BaggingClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
In [ ]:
from sklearn.metrics import classification_report, confusion_matrix
print("Matrice di confusione:")
print(confusion_matrix(y_test,y_pred))
Matrice di confusione:
[[13  2  0]
 [ 0 13  1]
 [ 0  2 14]]
In [ ]:
print("\nClassification Report:")
print(classification_report(y_test,y_pred))
Classification Report:
              precision    recall  f1-score   support

           0       1.00      0.87      0.93        15
           1       0.76      0.93      0.84        14
           2       0.93      0.88      0.90        16

    accuracy                           0.89        45
   macro avg       0.90      0.89      0.89        45
weighted avg       0.90      0.89      0.89        45

Random Forest

In [ ]:
from sklearn.ensemble import RandomForestClassifier
rnd = RandomForestClassifier()
rnd.fit(X_train, y_train)
y_pred = rnd.predict(X_test)
In [ ]:
print("Matrice di confusione:")
print(confusion_matrix(y_test,y_pred))
print("\nClassification Report:")
print(classification_report(y_test,y_pred))
Matrice di confusione:
[[14  1  0]
 [ 0 13  1]
 [ 0  0 16]]

Classification Report:
              precision    recall  f1-score   support

           0       1.00      0.93      0.97        15
           1       0.93      0.93      0.93        14
           2       0.94      1.00      0.97        16

    accuracy                           0.96        45
   macro avg       0.96      0.95      0.95        45
weighted avg       0.96      0.96      0.96        45

Extremely Random Trees Classifier

In [ ]:
from sklearn.ensemble import ExtraTreesClassifier
xtree = ExtraTreesClassifier()
xtree.fit(X_train, y_train)
y_pred = xtree.predict(X_test)
In [ ]:
print("Matrice di confusione:")
print(confusion_matrix(y_test,y_pred))
print("\nClassification Report:")
print(classification_report(y_test,y_pred))
Matrice di confusione:
[[15  0  0]
 [ 0 14  0]
 [ 0  0 16]]

Classification Report:
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        15
           1       1.00      1.00      1.00        14
           2       1.00      1.00      1.00        16

    accuracy                           1.00        45
   macro avg       1.00      1.00      1.00        45
weighted avg       1.00      1.00      1.00        45