jupyter nbconvert --to terminal dsml2/27.7_Blending.ipynb
MDN-118:08

27.7 Blending

27.7_Blending.ipynb · 22 KB · scarica
In [1]:
# Caricamento del dataset Iris
from sklearn.datasets import load_iris
data = load_iris()
X = data.data
y = data.target
In [2]:
from sklearn.model_selection import train_test_split

# Suddivido in training e validation set
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.3, random_state=42)
In [3]:
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier

# Creazione dei modelli base
models = [
    ('Logistic Regression', LogisticRegression(max_iter=200)),
    ('Decision Tree', DecisionTreeClassifier()),
    ('SVM', SVC(probability=True)),
    ('KNN', KNeighborsClassifier())
]

# Addestramento dei modelli base sui dati di addestramento
for name, model in models:
    model.fit(X_train, y_train)
    print(f"{name} addestrato.")
Logistic Regression addestrato.
Decision Tree addestrato.
SVM addestrato.
KNN addestrato.
In [4]:
import numpy as np

# Raccolta delle predizioni dei modelli base sui dati di validazione
base_predictions = np.zeros((X_valid.shape[0], len(models)))

# Probabilità per la classe positiva
for i, (name, model) in enumerate(models):
    base_predictions[:, i] = model.predict_proba(X_valid)[:, 1]
In [5]:
base_predictions
array([[0.81234375, 1.        , 0.87538496, 1.        ],
       [0.05804404, 0.        , 0.04312175, 0.        ],
       [0.0020913 , 0.        , 0.00326365, 0.        ],
       [0.77325137, 1.        , 0.87437805, 1.        ],
       [0.75120593, 1.        , 0.75856015, 1.        ],
       [0.04995858, 0.        , 0.0395134 , 0.        ],
       [0.90414505, 1.        , 0.9605367 , 1.        ],
       [0.14735785, 0.        , 0.07934313, 0.        ],
       [0.78045275, 1.        , 0.82566881, 0.6       ],
       [0.94081859, 1.        , 0.97576985, 1.        ],
       [0.22528229, 0.        , 0.15289004, 0.2       ],
       [0.03730607, 0.        , 0.02919117, 0.        ],
       [0.03083752, 0.        , 0.02297275, 0.        ],
       [0.04399294, 0.        , 0.03289149, 0.        ],
       [0.02347812, 0.        , 0.02297187, 0.        ],
       [0.66339848, 1.        , 0.75784424, 0.8       ],
       [0.0267218 , 0.        , 0.0062844 , 0.        ],
       [0.94516449, 1.        , 0.97697186, 1.        ],
       [0.82015949, 1.        , 0.90865537, 1.        ],
       [0.04143847, 0.        , 0.01072347, 0.        ],
       [0.04129335, 0.        , 0.03680138, 0.        ],
       [0.38668616, 0.        , 0.37271068, 0.2       ],
       [0.04373049, 0.        , 0.03991497, 0.        ],
       [0.05208954, 0.        , 0.01377546, 0.        ],
       [0.02036834, 0.        , 0.01163381, 0.        ],
       [0.10545077, 0.        , 0.04743747, 0.        ],
       [0.06981434, 0.        , 0.01458239, 0.        ],
       [0.01948706, 0.        , 0.00499333, 0.        ],
       [0.03903522, 0.        , 0.0342221 , 0.        ],
       [0.05053069, 0.        , 0.04189287, 0.        ],
       [0.00680116, 0.        , 0.00992814, 0.        ],
       [0.01931124, 0.        , 0.02521095, 0.        ],
       [0.89000213, 1.        , 0.9382455 , 1.        ],
       [0.03535015, 0.        , 0.03142237, 0.        ],
       [0.01740305, 0.        , 0.02015512, 0.        ],
       [0.28521131, 0.        , 0.1851437 , 0.4       ],
       [0.81216921, 1.        , 0.89776313, 1.        ],
       [0.03587571, 0.        , 0.02779305, 0.        ],
       [0.02190388, 0.        , 0.0198014 , 0.        ],
       [0.01693999, 0.        , 0.01762636, 0.        ],
       [0.19404769, 0.        , 0.12554995, 0.2       ],
       [0.73709261, 1.        , 0.86674359, 0.8       ],
       [0.75166289, 1.        , 0.80435875, 1.        ],
       [0.01824777, 0.        , 0.01935135, 0.        ],
       [0.0333894 , 0.        , 0.0262952 , 0.        ]])
In [6]:
# Addestramento del metamodello (regressione logistica) sui risultati dei modelli base
meta_model = LogisticRegression()
meta_model.fit(base_predictions, y_valid)
LogisticRegression()
In [7]:
final_predictions = meta_model.predict(base_predictions)
In [8]:
from sklearn.metrics import classification_report, confusion_matrix
print("Matrice di confusione:")
print(confusion_matrix(y_valid,final_predictions))
print("\nClassification Report:")
print(classification_report(y_valid,final_predictions))
Matrice di confusione:
[[19  0  0]
 [ 0 13  0]
 [11  0  2]]

Classification Report:
              precision    recall  f1-score   support

           0       0.63      1.00      0.78        19
           1       1.00      1.00      1.00        13
           2       1.00      0.15      0.27        13

    accuracy                           0.76        45
   macro avg       0.88      0.72      0.68        45
weighted avg       0.85      0.76      0.69        45