0% found this document useful (0 votes)
15 views18 pages

Program

The document outlines various machine learning programs using the Naïve Bayes classifier for tasks such as email spam detection, car purchase prediction, and heart disease prediction. Each program includes steps for data preparation, model training, evaluation, and making predictions on new data. Additionally, it provides results such as accuracy scores, confusion matrices, and classification reports for each application.

Uploaded by

mail2murutech
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)
15 views18 pages

Program

The document outlines various machine learning programs using the Naïve Bayes classifier for tasks such as email spam detection, car purchase prediction, and heart disease prediction. Each program includes steps for data preparation, model training, evaluation, and making predictions on new data. Additionally, it provides results such as accuracy scores, confusion matrices, and classification reports for each application.

Uploaded by

mail2murutech
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

Program: Naïve Bayes Classifier – Email Spam Detection

# Import required libraries

from sklearn.model_selection import train_test_split

from sklearn.feature_extraction.text import CountVectorizer

from sklearn.naive_bayes import MultinomialNB

from [Link] import accuracy_score, confusion_matrix, classification_report

# Step 1: Create a small dataset (for demonstration)

emails = [

"Congratulations! You have won a lottery worth $1 million",

"Hello friend, long time no see",

"Buy cheap medicines online now",

"Important meeting scheduled tomorrow",

"Earn money quickly from home",

"Let's catch up for lunch tomorrow",

"Exclusive offer! Get discount on electronics",

"Are you free this weekend?",

"You have been selected for a prize",

"Please find attached the project report"

# Labels: 1 = Spam, 0 = Not Spam

labels = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]

# Step 2: Convert text data into numerical feature vectors

vectorizer = CountVectorizer()

X = vectorizer.fit_transform(emails)

# Step 3: Split dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.3, random_state=42)


# Step 4: Create and train Naïve Bayes model

model = MultinomialNB()

[Link](X_train, y_train)

# Step 5: Predict on test data

y_pred = [Link](X_test)

# Step 6: Evaluate the model

print("✅ Naïve Bayes Email Spam Detection Results")

print("--------------------------------------------------")

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

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

print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=["Not Spam",


"Spam"]))

# Step 7: Test with new unseen messages

sample_emails = [

"Win exciting cash prizes now",

"Meeting postponed to next week"

sample_features = [Link](sample_emails)

predictions = [Link](sample_features)

print("--------------------------------------------------")

for email, label in zip(sample_emails, predictions):

print(f"📩 '{email}' --> {'Spam' if label == 1 else 'Not Spam'}")

output:

✅ Naïve Bayes Email Spam Detection Results


--------------------------------------------------

Accuracy: 1.0

Confusion Matrix:

[[2 0]

[0 1]]

Classification Report:

precision recall f1-score support

Not Spam 1.00 1.00 1.00 2

Spam 1.00 1.00 1.00 1

accuracy 1.00 3

macro avg 1.00 1.00 1.00 3

weighted avg 1.00 1.00 1.00 3

--------------------------------------------------

📩 'Win exciting cash prizes now' --> Spam

📩 'Meeting postponed to next week' --> Not Spam

Program: Naïve Bayes Classifier – Car Purchase Prediction

# Import libraries

import pandas as pd

from [Link] import LabelEncoder

from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import CategoricalNB

from [Link] import accuracy_score, confusion_matrix, classification_report

# Step 1: Create a simple dataset


data = {

'Age': ['Youth', 'Youth', 'Middle-aged', 'Senior', 'Senior', 'Senior',

'Middle-aged', 'Youth', 'Youth', 'Senior', 'Youth', 'Middle-aged',

'Middle-aged', 'Senior'],

'Income': ['High', 'High', 'High', 'Medium', 'Low', 'Low',

'Low', 'Medium', 'Low', 'Medium', 'Medium', 'Medium',

'High', 'Medium'],

'Student': ['No', 'No', 'No', 'No', 'Yes', 'Yes',

'Yes', 'No', 'Yes', 'Yes', 'Yes', 'No',

'Yes', 'No'],

'Credit_Rating': ['Fair', 'Excellent', 'Fair', 'Fair', 'Fair', 'Excellent',

'Excellent', 'Fair', 'Fair', 'Excellent', 'Excellent', 'Fair',

'Excellent', 'Fair'],

'Buys_Car': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No',

'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes',

'Yes', 'No']

# Step 2: Convert to DataFrame

df = [Link](data)

# Step 3: Encode categorical values into numeric values

le = LabelEncoder()

for column in [Link]:

df[column] = le.fit_transform(df[column])

# Step 4: Split dataset into features and target

X = df[['Age', 'Income', 'Student', 'Credit_Rating']]

y = df['Buys_Car']

# Step 5: Split into training and testing data


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

# Step 6: Create and train Naïve Bayes model (CategoricalNB)

model = CategoricalNB()

[Link](X_train, y_train)

# Step 7: Make predictions

y_pred = [Link](X_test)

# Step 8: Evaluate the model

print("🚗 Naïve Bayes Car Purchase Prediction Results")

print("--------------------------------------------------")

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

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

print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=["No", "Yes"]))

# Step 9: Predict for a new person

sample = [Link]({

'Age': ['Youth'],

'Income': ['Low'],

'Student': ['Yes'],

'Credit_Rating': ['Fair']

})

# Encode sample using same label encoders

for col in [Link]:

sample[col] = le.fit_transform(sample[col])

prediction = [Link](sample)

print("--------------------------------------------------")

print(f"🧍 Will the person buy a car? ➜ {'Yes' if prediction[0] == 1 else 'No'}")
🚗 Naïve Bayes Car Purchase Prediction Results

--------------------------------------------------

Accuracy: 0.8

Confusion Matrix:

[[1 1]

[0 3]]

Classification Report:

precision recall f1-score support

No 1.00 0.50 0.67 2

Yes 0.75 1.00 0.86 3

accuracy 0.80 5

macro avg 0.88 0.75 0.76 5

weighted avg 0.85 0.80 0.78 5

--------------------------------------------------

🧍 Will the person buy a car? ➜ Yes

Program: Heart Disease Prediction using Naïve Bayes Classifier

# Import required libraries

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import GaussianNB

from [Link] import accuracy_score, confusion_matrix, classification_report

from [Link] import StandardScaler


# Step 1: Load Dataset

# (You can replace this with your own CSV file, e.g. '[Link]')

# For demonstration, we’ll create a small sample dataset

data = {

'age': [52, 53, 70, 61, 62, 58, 44, 60, 63, 40],

'sex': [1, 1, 1, 1, 0, 0, 1, 0, 1, 0],

'cp': [0, 1, 0, 1, 2, 2, 1, 0, 1, 2], # chest pain type

'trestbps': [125, 140, 145, 130, 120, 130, 120, 110, 150, 120], # resting BP

'chol': [212, 203, 174, 243, 281, 197, 263, 221, 247, 240], # cholesterol

'thalach': [168, 155, 125, 150, 160, 174, 147, 160, 135, 175], # max heart rate

'oldpeak': [1.0, 1.5, 2.6, 1.8, 1.4, 0.8, 0.4, 1.1, 2.0, 0.2], # ST depression

'target': [1, 1, 0, 0, 1, 1, 1, 0, 0, 1] # 1 = heart disease, 0 = no heart disease

# Convert dictionary to DataFrame

df = [Link](data)

# Step 2: Separate features and target

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

y = df['target']

# Step 3: Split data into training and testing sets

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

# Step 4: Feature Scaling (optional but improves numerical stability)

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)

X_test = [Link](X_test)

# Step 5: Create and train the Naïve Bayes model

model = GaussianNB()
[Link](X_train, y_train)

# Step 6: Make predictions

y_pred = [Link](X_test)

# Step 7: Evaluate the model

print("❤️Naïve Bayes - Heart Disease Prediction Results")

print("--------------------------------------------------")

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

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

print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=["No Disease",


"Disease"]))

# Step 8: Predict for a new patient

new_patient = [[57, 1, 1, 130, 240, 160, 1.2]] # sample input: age, sex, cp, trestbps, chol, thalach,
oldpeak

new_patient_scaled = [Link](new_patient)

prediction = [Link](new_patient_scaled)

print("--------------------------------------------------")

print(f"🧍 Prediction for new patient: {'Heart Disease' if prediction[0]==1 else 'No Heart Disease'}")

❤️Naïve Bayes - Heart Disease Prediction Results

--------------------------------------------------

Accuracy: 0.67

Confusion Matrix:

[[1 1]

[0 1]]

Classification Report:

precision recall f1-score support


No Disease 1.00 0.50 0.67 2

Disease 0.50 1.00 0.67 1

--------------------------------------------------

🧍 Prediction for new patient: Heart Disease

Program: Heart Disease Prediction using Naïve Bayes Classifier (UCI Dataset)
# Import necessary libraries

import pandas as pd

import [Link] as plt

import seaborn as sns

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

from sklearn.naive_bayes import GaussianNB

from [Link] import accuracy_score, confusion_matrix, classification_report

# Step 1: Load the dataset

# Ensure you have [Link] in the same folder or provide full path

# Dataset source: [Link]

df = pd.read_csv("[Link]")

# Step 2: Display first few rows and info

print("🏥 First 5 records of the dataset:")

print([Link]())

print("\nDataset Information:")

print([Link]())

# Step 3: Check for missing values


print("\nMissing Values in Each Column:")

print([Link]().sum())

# Step 4: Separate features (X) and target (y)

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

y = df["target"]

# Step 5: Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.2, random_state=42

# Step 6: Normalize numerical features

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)

X_test = [Link](X_test)

# Step 7: Train the Naïve Bayes model

model = GaussianNB()

[Link](X_train, y_train)

# Step 8: Make predictions

y_pred = [Link](X_test)

# Step 9: Evaluate the model

print("\n✅ Naïve Bayes - Heart Disease Prediction Results")

print("--------------------------------------------------")

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

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

print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=["No Disease",


"Disease"]))
# Step 10: Visualize the confusion matrix

cm = confusion_matrix(y_test, y_pred)

[Link](figsize=(5,4))

[Link](cm, annot=True, fmt='d', cmap='Blues', xticklabels=["No Disease", "Disease"],


yticklabels=["No Disease", "Disease"])

[Link]("Confusion Matrix - Heart Disease Prediction")

[Link]("Predicted Label")

[Link]("True Label")

[Link]()

# Step 11: Predict for a new patient

new_patient = [[57, 1, 2, 130, 236, 0, 0, 174, 0, 0.0, 1, 1, 2]]

new_patient_scaled = [Link](new_patient)

prediction = [Link](new_patient_scaled)

print("--------------------------------------------------")

print(f"🧍 Prediction for New Patient: {'Heart Disease' if prediction[0]==1 else 'No Heart Disease'}")

Dataset Description ([Link])

The UCI Heart Disease dataset contains 14 features, such as:

Feature Description

age Age of the person

sex 1 = male, 0 = female

cp Chest pain type (0–3)

trestbps Resting blood pressure

chol Serum cholesterol (mg/dl)

fbs Fasting blood sugar > 120 mg/dl (1 = true, 0 = false)

restecg Resting electrocardiographic results

thalach Maximum heart rate achieved

exang Exercise-induced angina (1 = yes, 0 = no)


Feature Description

oldpeak ST depression induced by exercise

slope Slope of peak exercise ST segment

ca Number of major vessels (0–3)

thal 3 = normal, 6 = fixed defect, 7 = reversible defect

target 1 = Heart Disease, 0 = No Disease

✅ Naïve Bayes - Heart Disease Prediction Results

--------------------------------------------------

Accuracy: 0.85

Confusion Matrix:

[[25 5]

[ 4 27]]

Classification Report:

precision recall f1-score support

No Disease 0.86 0.83 0.84 30

Disease 0.84 0.87 0.85 31

🧍 Prediction for New Patient: Heart Disease

Program: Decision Tree – Student Pass/Fail Prediction


# Import necessary libraries

import pandas as pd

from sklearn.model_selection import train_test_split

from [Link] import DecisionTreeClassifier, export_text, plot_tree

from [Link] import accuracy_score, confusion_matrix, classification_report

import [Link] as plt

# Step 1: Create a sample dataset

data = {

'Study_Hours': [2, 5, 1, 4, 6, 2, 7, 3, 8, 5],

'Attendance': [50, 80, 40, 70, 90, 60, 95, 55, 100, 85],

'Sleep_Hours': [8, 6, 9, 7, 5, 8, 6, 7, 5, 6],

'Pass': ['No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes']

# Step 2: Convert to DataFrame

df = [Link](data)

# Step 3: Split dataset into features and target

X = df[['Study_Hours', 'Attendance', 'Sleep_Hours']]

y = df['Pass']

# Step 4: Split into training and testing sets

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

# Step 5: Create and train the Decision Tree model

dt_model = DecisionTreeClassifier(criterion='entropy', random_state=42) # using entropy for info


gain

dt_model.fit(X_train, y_train)

# Step 6: Make predictions


y_pred = dt_model.predict(X_test)

# Step 7: Evaluate the model

print("🎓 Decision Tree - Student Pass/Fail Prediction Results")

print("--------------------------------------------------")

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

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

print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=["Fail",


"Pass"]))

# Step 8: Visualize the Decision Tree

[Link](figsize=(12,8))

plot_tree(dt_model, feature_names=[Link], class_names=['Fail', 'Pass'], filled=True,


rounded=True)

[Link]("Decision Tree - Student Pass/Fail")

[Link]()

# Step 9: Display textual representation of the tree

tree_rules = export_text(dt_model, feature_names=list([Link]))

print("\nDecision Tree Rules:\n")

print(tree_rules)

# Step 10: Predict for a new student

new_student = [[4, 75, 7]] # Study hours, Attendance, Sleep hours

prediction = dt_model.predict(new_student)

print("--------------------------------------------------")

print(f"🧑 Prediction for New Student: {'Pass' if prediction[0]=='Yes' else 'Fail'}")

Python Program: Diabetes Prediction using SVM


# Import required libraries
import pandas as pd

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

from [Link] import SVC

from [Link] import accuracy_score, confusion_matrix, classification_report

# Load dataset (Pima Indians Diabetes dataset)

url = "[Link]
[Link]"

# Define column names

columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness',

'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome']

# Read CSV

data = pd.read_csv(url, names=columns)

# Features and target

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

y = data['Outcome']

# Split into training and test sets

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

# Feature scaling (important for SVM)

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)

X_test = [Link](X_test)

# Create SVM classifier

svm_model = SVC(kernel='rbf') # You can also try 'linear' or 'poly'


# Train the model

svm_model.fit(X_train, y_train)

# Predict

y_pred = svm_model.predict(X_test)

# Evaluate the model

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

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

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

# Predict for a new patient (example)

new_patient = [[6, 148, 72, 35, 0, 33.6, 0.627, 50]] # Example data

new_patient_scaled = [Link](new_patient)

prediction = svm_model.predict(new_patient_scaled)

if prediction[0] == 1:

print("\nPrediction: Patient is likely to have diabetes.")

else:

print("\nPrediction: Patient is unlikely to have diabetes.")

Python Program: Email/Message Spam Detection using SVM


# Import libraries

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.feature_extraction.text import TfidfVectorizer

from [Link] import SVC

from [Link] import accuracy_score, confusion_matrix, classification_report


# Load dataset

url = "[Link]

data = pd.read_csv(url, sep='\t', names=['label', 'message'])

# Map labels to 0 (ham) and 1 (spam)

data['label_num'] = [Link]({'ham':0, 'spam':1})

# Features and target

X = data['message']

y = data['label_num']

# Convert text to numerical data using TF-IDF

vectorizer = TfidfVectorizer()

X_vectorized = vectorizer.fit_transform(X)

# Split into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X_vectorized, y, test_size=0.3, random_state=42)

# Create SVM classifier

svm_model = SVC(kernel='linear') # linear kernel works well for text classification

svm_model.fit(X_train, y_train)

# Predict

y_pred = svm_model.predict(X_test)

# Evaluate the model

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

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

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

# Predict a new message


new_message = ["Congratulations! You have won a free ticket. Call now!"]

new_message_vectorized = [Link](new_message)

prediction = svm_model.predict(new_message_vectorized)

if prediction[0] == 1:

print("\nPrediction: Spam")

else:

print("\nPrediction: Not Spam")

# Predict a new message (Non-spam example)

new_message = ["Hey, are we meeting for lunch today?"] # Normal message

new_message_vectorized = [Link](new_message)

prediction = svm_model.predict(new_message_vectorized)

if prediction[0] == 1:

print("\nPrediction: Spam")

else:

print("\nPrediction: Not Spam")

You might also like