0% found this document useful (0 votes)
3 views6 pages

Heart Disease Prediction with Stacking

The document outlines a machine learning workflow using Python to predict heart disease, utilizing libraries such as pandas, sklearn, and mlxtend. It includes steps for data loading, preprocessing, model training with KNeighbors and Naive Bayes classifiers, and implementing a Stacking Classifier to improve accuracy. The final results show that the stacked model achieved an accuracy of nearly 84%, outperforming the individual models which scored around 80% each.

Uploaded by

Harini Murugesan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Heart Disease Prediction with Stacking

The document outlines a machine learning workflow using Python to predict heart disease, utilizing libraries such as pandas, sklearn, and mlxtend. It includes steps for data loading, preprocessing, model training with KNeighbors and Naive Bayes classifiers, and implementing a Stacking Classifier to improve accuracy. The final results show that the stacked model achieved an accuracy of nearly 84%, outperforming the individual models which scored around 80% each.

Uploaded by

Harini Murugesan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Code: Import Required Libraries:

python3

import pandas as pd

import [Link] as plt

from [Link] import plot_confusion_matrix

from [Link] import StackingClassifier

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

from sklearn.linear_model import LogisticRegression

from [Link] import KNeighborsClassifier

from sklearn.naive_bayes import GaussianNB

from [Link] import confusion_matrix

from [Link] import accuracy_score

Code: Loading the dataset

python3

df = pd.read_csv('[Link]') # loading the dataset

[Link]() # viewing top 5 rows of dataset

Output:
Code:

python3

# Creating X and y for training

X = [Link]('target', axis = 1)

y = df['target']

Code: Splitting Data into Train and Test

python3

# 20 % training dataset is considered for testing

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 4

Code: Standardizing Data

python3

# initializing sc object

sc = StandardScaler()
# variables that needed to be transformed

var_transform = ['thalach', 'age', 'trestbps', 'oldpeak', 'chol']

X_train[var_transform] = sc.fit_transform(X_train[var_transform]) # standardizing traini

X_test[var_transform] = [Link](X_test[var_transform]) # standardizing tes

print(X_train.head())

Output:

Code: Building First Layer Estimators

python3

KNC = KNeighborsClassifier() # initialising KNeighbors Classifier

NB = GaussianNB() # initialising Naive Bayes

Let’s Train and evaluate with our first layer estimators to observe the difference
in the performance of the stacked model and general model
Code: Training KNeighborsClassifier

python3

model_kNeighborsClassifier = [Link](X_train, y_train) # fitting Training Set


pred_knc = model_kNeighborsClassifier.predict(X_test) # Predicting on test dataset

Code: Evaluation of KNeighborsClassifier

python3

acc_knc = accuracy_score(y_test, pred_knc) # evaluating accuracy score

print('accuracy score of KNeighbors Classifier is:', acc_knc * 100)

Output:

Code: Training Naive Bayes Classifier

python3

model_NaiveBayes = [Link](X_train, y_train)

pred_nb = model_NaiveBayes.predict(X_test)

Code: Evaluation of Naive Bayes Classifier

python3

acc_nb = accuracy_score(y_test, pred_nb)

print('Accuracy of Naive Bayes Classifier:', acc_nb * 100)

Output:
Code: Implementing Stacking Classifier

python3

lr = LogisticRegression() # defining meta-classifier

clf_stack = StackingClassifier(classifiers =[KNC, NB], meta_classifier = lr, use_probas = T


use_features_in_secondary = True)

 use_probas=True indicates the Stacking Classifier uses the prediction


probabilities as an input instead of using predictions classes.
 use_features_in_secondary=True indicates Stacking Classifier not only take
predictions as an input but also uses features in the dataset to predict on
new data.
Code: Training Stacking Classifier

python3

model_stack = clf_stack.fit(X_train, y_train) # training of stacked model

pred_stack = model_stack.predict(X_test) # predictions on test data using stacked mo

Code: Evaluating Stacking Classifier

python3

acc_stack = accuracy_score(y_test, pred_stack) # evaluating accuracy

print('accuracy score of Stacked model:', acc_stack * 100)

Output:
Our both 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.
Code:

You might also like