Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Tensorflow XLA: The Fusion Compiler for Tensorflow
Next article icon

Implementation of Stacking - ML

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Stacking is a ensemble learning technique used to improve performance of models by combining the predictions of multiple models. In this article, we will see how to implement a Stacking Classifier on a classification dataset using Python.

For better understanding about stacking refer to: Stacking in Machine Learning

Before its implementation we need to install these packages for our implementation using following commands:

pip install mlxtend 
pip install pandas 
pip install -U scikit-learn

Step 1: Importing the required Libraries 

We will import pandas, matplotlib and scikit learn for this.

python
import pandas as pd import matplotlib.pyplot as plt from mlxtend.plotting import plot_confusion_matrix from mlxtend.classifier import StackingClassifier from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.neighbors import KNeighborsClassifier from sklearn.naive_bayes import GaussianNB  from sklearn.metrics import confusion_matrix from sklearn.metrics import accuracy_score 

Step 2: Loading the following Dataset 

You can Download the dataset from this link Heart Dataset.

python
df = pd.read_csv('heart.csv')                       X = df.drop('target', axis = 1) y = df['target']  df.head()   

Output: 
 


Step 3: Split the Data into Training and Testing Sets

  • test_size = 0.2: Specifies that 20% of the data should be used for testing, leaving 80% for training.
  • random_state = 42: Ensures reproducibility by setting a fixed seed for random number generation.
python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)  

Step 4: Standardize the Data

In this step the data is standardized using the StandardScaler to ensure that features have a mean of 0 and a standard deviation of 1.

  • var_transform: Specifies the list of feature columns that need to be standardized.
  • X_train[var_transform]: Applies the fit_transform method to standardize the selected columns in the training data.
  • X_test[var_transform]: Applies the transform method to standardize the corresponding columns in the test data using the scaling parameters from the training data.
python
sc = StandardScaler()    var_transform = ['thalach', 'age', 'trestbps', 'oldpeak', 'chol'] X_train[var_transform] = sc.fit_transform(X_train[var_transform])    X_test[var_transform] = sc.transform(X_test[var_transform])            print(X_train.head()) 

Output: 

Step 5: Build First Layer Estimators 

The first layer consists of base models. For this example we’ll use K-Nearest Neighbors classifier and Naive Bayes classifier.

python
KNC = KNeighborsClassifier()    NB = GaussianNB()               

Step 6: Training and Evaluating KNeighborsClassifier

Let's train and evaluate the KNeighborsClassifier.

python
model_kNeighborsClassifier = KNC.fit(X_train, y_train)   pred_knc = model_kNeighborsClassifier.predict(X_test)    

Evaluation: 

python
acc_knc = accuracy_score(y_test, pred_knc)   print('accuracy score of KNeighbors Classifier is:', acc_knc * 100) 

Output: 

accuracy score of KNeighbors Classifier is: 80.4878048780

Step 7: Training and Evaluating Naive Bayes Classifier

python
model_NaiveBayes = NB.fit(X_train, y_train) pred_nb = model_NaiveBayes.predict(X_test) 

Evaluation:

python
acc_nb = accuracy_score(y_test, pred_nb) print('Accuracy of Naive Bayes Classifier:', acc_nb * 100) 

Output: 

Accuracy of Naive Bayes Classifier: 80.0

Step 8: Implementing the Stacking Classifier 

Now, we combine the base models using a Stacking Classifier. The meta-model will be a logistic regression model which will take the predictions of KNN and Naive Bayes as input.

python
from sklearn.linear_model import LogisticRegression  base_learners = [     ('knn', KNeighborsClassifier()),     ('nb', GaussianNB()) ] meta_model = LogisticRegression()  stacking_model = StackingClassifier(estimators=base_learners, final_estimator=meta_model, use_probas=True) 

Step 9: Training Stacking Classifier  

python
model_stack = clf_stack.fit(X_train, y_train)    pred_stack = model_stack.predict(X_test)       

Evaluating Stacking Classifier:

python
acc_stack = accuracy_score(y_test, pred_stack)  # evaluating accuracy print('accuracy score of Stacked model:', acc_stack * 100)   

Output:

accuracy score of Stacked model: 83.90243902439025

Both of our individual models scores an accuracy of nearly 80% and our Stacked model got an accuracy of nearly 84% . By Combining two individual models we got a significant performance improvement.


Next Article
Tensorflow XLA: The Fusion Compiler for Tensorflow

K

Koushik222
Improve
Article Tags :
  • Machine Learning
  • AI-ML-DS
  • AI-ML-DS With Python
Practice Tags :
  • Machine Learning

Similar Reads

    Stacking in Machine Learning
    Stacking is a ensemble learning technique where the final model known as the “stacked model" combines the predictions from multiple base models. The goal is to create a stronger model by using different models and combining them.Architecture of StackingStacking architecture is like a team of models
    3 min read
    Stacking in Machine Learning
    Stacking is a ensemble learning technique used to improve performance of models by combining the predictions of multiple models. In this article, we will see how to implement a Stacking Classifier on a classification dataset using Python.For better understanding about stacking refer to: Stacking in
    3 min read
    Tensorflow XLA: The Fusion Compiler for Tensorflow
    TensorFlow, Google's renowned machine learning framework, has been at the forefront of empowering developers and researchers with tools to build sophisticated AI models. One such powerful addition to TensorFlow's arsenal is XLA (Accelerated Linear Algebra), a compiler that optimizes TensorFlow compu
    5 min read
    What's the Difference Between torch.stack() and torch.cat() Functions?
    Effective tensor manipulation in PyTorch is essential for creating and refining deep learning models. 'torch.stack()' and 'torch.cat()' are two frequently used functions for merging tensors. While they are both intended to combine tensors, their functions are different and have different application
    7 min read
    What is Stack Data Structure? A Complete Tutorial
    Stack is a linear data structure that follows LIFO (Last In First Out) Principle, the last element inserted is the first to be popped out. It means both insertion and deletion operations happen at one end only. LIFO(Last In First Out) PrincipleHere are some real world examples of LIFOConsider a stac
    4 min read
    Stack Notes for GATE Exam [2024]
    Stacks, a fundamental data structure in computer science, are crucial for understanding algorithmic paradigms and solving complex computational problems. As candidates gear up for the GATE Exam 2024, a solid grasp of stack concepts is indispensable. These notes are designed to provide a concise yet
    14 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences