Let’s stroll by means of an instance utilizing the well-known Iris dataset with stacking in Python.
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
# Load the Iris dataset
information = load_iris()
X = information.information
y = information.goal
# Cut up the information into coaching and testing units
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Outline the bottom fashions
model1 = RandomForestClassifier(n_estimators=100, random_state=42)
model2 = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, random_state=42)
# Prepare the bottom fashions
model1.match(X_train, y_train)
model2.match(X_train, y_train)
# Generate predictions
pred1 = model1.predict(X_train)
pred2 = model2.predict(X_train)
# Stack predictions to create a brand new dataset for the meta-learner
stacked_predictions = np.column_stack((pred1, pred2))
# Outline the meta-learner
meta_learner = LogisticRegression()# Prepare the meta-learner
meta_learner.match(stacked_predictions, y_train)
# Generate take a look at set predictions from base fashions
test_pred1 = model1.predict(X_test)
test_pred2 = model2.predict(X_test)
# Stack take a look at predictions
stacked_test_predictions = np.column_stack((test_pred1, test_pred2))# Make closing prediction utilizing the meta-learner
final_predictions = meta_learner.predict(stacked_test_predictions)
# Calculate accuracy
accuracy = accuracy_score(y_test, final_predictions)
print(f"Stacking Mannequin Accuracy: {accuracy:.4f}")