0% found this document useful (0 votes)
12 views4 pages

Ex 12

Uploaded by

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

Ex 12

Uploaded by

2317061
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

IMPLEMENT A BAYESIAN INFERENCE IN CARDIOVASCULAR

DISEASE ANALYSIS.

EX.NO:
DATE:

AIM:
To write a python program to implement a Bayesian inference in cardiovascular disease analysis.

ALGORITHM:

1. Import the necessary libraries including pandas, sklearn, matplotlib, and seaborn.

2. Load the cardiovascular disease dataset (cardio_train.csv) using pandas with sep=';'.

3. Check if the dataset contains an id column and drop it if present.

4. Define the feature matrix X by selecting all columns except the target column cardio.

5. Define the target variable y as the cardio column which indicates disease presence (1)

or absence (0).

6. Check the dataset for any missing or null values and handle them if necessary.

7. Use the train_test_split function to split the data into 80% training and 20% testing

sets.

8. Set random_state=42 in the split to ensure reproducibility of the results.

9. Initialize the Gaussian Naive Bayes classifier using GaussianNB().

10. Train the model using the training dataset with the fit() method.

11. Make predictions on the testing dataset using the predict() method.

12. Calculate the accuracy of the model using the accuracy_score() function.

13. Generate a classification report using classification_report() to view performance

metrics.

14. Create and display a confusion matrix to understand prediction errors.


SOURCE CODE:

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import GaussianNB

from sklearn.metrics import classification_report, accuracy_score, confusion_matrix

import matplotlib.pyplot as plt

import seaborn as sns

data = pd.read_csv("cardio_train.csv", sep=';')

if 'id' in data.columns: data.drop('id', axis=1, inplace=True)

X = data.drop('cardio', axis=1)

y = data['cardio']

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

gnb = GaussianNB()

gnb.fit(X_train, y_train)

y_pred = gnb.predict(X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))

print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))

cm = confusion_matrix(y_test, y_pred)

plt.figure(figsize=(6, 5))

sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',

plt.xlabel('Predicted')

plt.ylabel('Actual')

plt.title('Confusion Matrix')

plt.show()
OUTPUT:

RESULT:

Thus the python program to implement Bayesian interface in cardiovascular disease


analysis was executed successfully.

You might also like