0% found this document useful (0 votes)
8 views10 pages

Candidate-Elimination and Classifiers Code

The document contains multiple machine learning programs, including implementations of the Candidate-Elimination algorithm, Naïve Bayesian classifier, Bayesian network for heart disease diagnosis, K-Means clustering, Bootstrap Aggregation, and a simple deep learning model using TensorFlow. Each program is designed to demonstrate specific machine learning techniques and includes sample data and accuracy evaluations. The document also includes installation instructions for necessary packages.

Uploaded by

krr87170
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)
8 views10 pages

Candidate-Elimination and Classifiers Code

The document contains multiple machine learning programs, including implementations of the Candidate-Elimination algorithm, Naïve Bayesian classifier, Bayesian network for heart disease diagnosis, K-Means clustering, Bootstrap Aggregation, and a simple deep learning model using TensorFlow. Each program is designed to demonstrate specific machine learning techniques and includes sample data and accuracy evaluations. The document also includes installation instructions for necessary packages.

Uploaded by

krr87170
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

1.

Implement and demonstrate the Candidate-Elimination algorithm to output a description


of the set of all hypotheses consistent with the training examples.

Program:

class CandidateElimination:
def __init__(self, num_attributes):
self.S = [['0'] * num_attributes] # Most specific
self.G = [['?'] * num_attributes] # Most general

def learn(self, example, label):


print(f"\nLearning: {example} -> {label}")

if label == 'Yes': # Positive example


# Update S - generalize to cover positive example
self.S = [[Link](s, example) for s in self.S]
# Update G - remove inconsistent hypotheses
self.G = [g for g in self.G if [Link](g, example)]
else: # Negative example
# Update G - specialize to exclude negative example
new_G = []
for g in self.G:
if not [Link](g, example):
new_G.append(g)
else:
# Specialize g
for i in range(len(g)):
if g[i] == '?':
for val in ['Sunny', 'Rainy']: # Adjust based on domain
if val != example[i]:
new_g = [Link]()
new_g[i] = val
if any([Link](new_g, s) for s in self.S):
new_G.append(new_g)
self.G = new_G
# Update S - remove inconsistent
self.S = [s for s in self.S if not [Link](s, example)]
print(f"S: {self.S}")
print(f"G: {self.G}")

def generalize(self, hypothesis, example):


new_h = []
for h_val, e_val in zip(hypothesis, example):
if h_val == '0': # Initial null value
new_h.append(e_val)
elif h_val != e_val:
new_h.append('?')
else:
new_h.append(h_val)
return new_h

def consistent(self, hypothesis, example):


for h_val, e_val in zip(hypothesis, example):
if h_val != '?' and h_val != e_val:
return False
return True

# Demo with the classic "Enjoy Sport" example


def demo():
print("=== Candidate-Elimination Algorithm Demo ===")
ce = CandidateElimination(6) # 6 attributes

# Training data: [Sky, AirTemp, Humidity, Wind, Water, Forecast]


examples = [
(['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same'], 'Yes'),
(['Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same'], 'Yes'),
(['Rainy', 'Cold', 'High', 'Strong', 'Warm', 'Change'], 'No'),
(['Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change'], 'Yes')
]

for example, label in examples:


[Link](example, label)

print("\n=== Final Result ===")


print(f"Most Specific (S): {ce.S}")
print(f"Most General (G): {ce.G}")
# Run demo
demo()

2. Write a program to implement the naïve Bayesian classifier for a sample training data set
stored as a .CSV file. Compute the accuracy of the classifier, considering few test data
sets.

Program:
import pandas as pd
import numpy as np
from collections import defaultdict

class SimpleNaiveBayes:
def fit(self, X, y):
self.class_probs = {}
self.feature_probs = defaultdict(dict)

# Calculate class probabilities


for class_label in set(y):
self.class_probs[class_label] = [Link](y == class_label) / len(y)

# Calculate feature probabilities


for class_label in set(y):
class_data = X[y == class_label]
for feature in [Link]:
feature_counts = class_data[feature].value_counts()
total = len(class_data)
self.feature_probs[class_label][feature] = {
value: (count + 1) / (total + len(feature_counts)) # Laplace smoothing
for value, count in feature_counts.items()
}

def predict(self, X):


predictions = []
for _, sample in [Link]():
class_scores = {}
for class_label in self.class_probs:
# Start with class probability
score = [Link](self.class_probs[class_label])

# Multiply feature probabilities


for feature, value in [Link]():
if value in self.feature_probs[class_label][feature]:
prob = self.feature_probs[class_label][feature][value]
score += [Link](prob)

class_scores[class_label] = score

# Predict class with highest probability


[Link](max(class_scores, key=class_scores.get))

return predictions

# Sample dataset
data = {
'Weather': ['Sunny', 'Sunny', 'Rainy', 'Rainy', 'Rainy', 'Sunny'],
'Temperature': ['Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Mild'],
'Play': ['No', 'No', 'Yes', 'Yes', 'No', 'Yes']
}

df = [Link](data)

# Prepare data
X = df[['Weather', 'Temperature']]
y = df['Play']

# Split into train and test


X_train = [Link][:4] # First 4 rows for training
y_train = [Link][:4]
X_test = [Link][4:] # Last 2 rows for testing
y_test = [Link][4:]

# Train classifier
nb = SimpleNaiveBayes()
[Link](X_train, y_train)

# Make predictions
predictions = [Link](X_test)

# Calculate accuracy
accuracy = [Link](predictions == y_test.values)

print("Training Data:")
print([Link]([X_train, y_train], axis=1))
print("\nTest Data:")
print([Link]([X_test, y_test], axis=1))
print(f"\nPredictions: {predictions}")
print(f"Actual: {list(y_test.values)}")
print(f"Accuracy: {accuracy:.2f}")

3. Write a program to construct a Bayesian network considering medical data. Use this
model to demonstrate the diagnosis of heart patients using standard Heart Disease Data
Set.

Install Required Packages


pip install pgmpy pandas numpy matplotlib seaborn scikit-learn

pip install matplotlib seaborn pandas numpy networkx

Program:

import numpy as np

# Simple Bayesian Network for Heart Disease


class HeartDiseaseBN:
def __init__(self):
# Prior probability
self.p_hd = 0.45 # P(Heart Disease)

# Conditional probabilities P(Symptom | Heart Disease)


self.p_cp_hd = 0.7 # P(Chest Pain | HD)
self.p_bp_hd = 0.6 # P(High BP | HD)
self.p_chol_hd = 0.65 # P(High Cholesterol | HD)

# Conditional probabilities P(Symptom | No Heart Disease)


self.p_cp_no = 0.2 # P(Chest Pain | No HD)
self.p_bp_no = 0.3 # P(High BP | No HD)
self.p_chol_no = 0.25 # P(High Cholesterol | No HD)

def diagnose(self, chest_pain, high_bp, high_chol):

# P(HD | Symptoms) ∝ P(HD) * P(Symptoms | HD)


"""Predict heart disease probability"""

p_symptoms_hd = 1.0
p_symptoms_no = 1.0

if chest_pain:
p_symptoms_hd *= self.p_cp_hd
p_symptoms_no *= self.p_cp_no
else:
p_symptoms_hd *= (1 - self.p_cp_hd)
p_symptoms_no *= (1 - self.p_cp_no)

if high_bp:
p_symptoms_hd *= self.p_bp_hd
p_symptoms_no *= self.p_bp_no
else:
p_symptoms_hd *= (1 - self.p_bp_hd)
p_symptoms_no *= (1 - self.p_bp_no)

if high_chol:
p_symptoms_hd *= self.p_chol_hd
p_symptoms_no *= self.p_chol_no
else:
p_symptoms_hd *= (1 - self.p_chol_hd)
p_symptoms_no *= (1 - self.p_chol_no)

# Bayes' Theorem
numerator = self.p_hd * p_symptoms_hd
denominator = numerator + (1 - self.p_hd) * p_symptoms_no

return numerator / denominator if denominator > 0 else 0.5

# Usage Example
if __name__ == "__main__":
model = HeartDiseaseBN()

print("Heart Disease Diagnosis using Bayesian Network")


print("=" * 50)

# Test cases
cases = [
(0, 0, 0, "No symptoms"),
(1, 0, 0, "Only chest pain"),
(0, 1, 1, "High BP and cholesterol"),
(1, 1, 1, "All symptoms")
]

for cp, bp, chol, desc in cases:


prob = [Link](cp, bp, chol)
risk = "HIGH" if prob > 0.7 else "MEDIUM" if prob > 0.3 else "LOW"

print(f"\n{desc}:")
print(f" Chest Pain: {cp}, High BP: {bp}, High Cholesterol: {chol}")
print(f" Heart Disease Probability: {prob:.3f}")
print(f" Risk Level: {risk}")

4. Write a program to implement K-Means Clustering.

Install Required Packages


pip install numpy
pip install matplotlib
pip install scikit-learn

Program:
import numpy as np
import [Link] as plt
from [Link] import make_blobs

# Generate sample data


X, _ = make_blobs(n_samples=300, centers=4, n_features=2, random_state=42)

# K-Means implementation
class SimpleKMeans:
def __init__(self, k=3, max_iters=100):
self.k = k
self.max_iters = max_iters

def fit(self, X):


# Initialize centroids randomly
[Link] = X[[Link](len(X), self.k, replace=False)]

for _ in range(self.max_iters):
# Assign points to nearest centroid
distances = [Link](((X - [Link][:, [Link]])**2).sum(axis=2))
[Link] = [Link](distances, axis=0)
# Update centroids
new_centroids = [Link]([X[[Link] == i].mean(axis=0) for i in range(self.k)])

# Check convergence
if [Link](new_centroids, [Link]):
break
[Link] = new_centroids

return self

# Run K-Means
kmeans = SimpleKMeans(k=4)
[Link](X)

# Plot results
[Link](X[:, 0], X[:, 1], c=[Link], cmap='viridis')
[Link]([Link][:, 0], [Link][:, 1], marker='X', s=200, c='red')
[Link]('K-Means Clustering')
[Link]()

5. Write a program to implement Bootstrap Aggregation in machine learning.

pip install numpy matplotlib scikit-learn seaborn

Program:

import numpy as np
from [Link] import DecisionTreeClassifier
from [Link] import make_classification
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score

class SimpleBagging:
def __init__(self, n_estimators=10):
self.n_estimators = n_estimators
[Link] = []

def fit(self, X, y):


n_samples = [Link][0]
[Link] = []

for i in range(self.n_estimators):
# Create bootstrap sample
indices = [Link](n_samples, n_samples, replace=True)
X_bootstrap = X[indices]
y_bootstrap = y[indices]

# Train a decision tree


tree = DecisionTreeClassifier()
[Link](X_bootstrap, y_bootstrap)
[Link](tree)

return self

def predict(self, X):


# Collect predictions from all models
all_predictions = [Link]([[Link](X) for model in [Link]])

# Majority voting
final_predictions = []
for sample_predictions in all_predictions.T:
unique, counts = [Link](sample_predictions, return_counts=True)
winner = unique[[Link](counts)]
final_predictions.append(winner)

return [Link](final_predictions)

# Generate sample data


X, y = make_classification(n_samples=1000, n_features=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Single Decision Tree


single_tree = DecisionTreeClassifier()
single_tree.fit(X_train, y_train)
single_pred = single_tree.predict(X_test)
single_accuracy = accuracy_score(y_test, single_pred)

# Bagging Ensemble
bagging = SimpleBagging(n_estimators=20)
[Link](X_train, y_train)
bagging_pred = [Link](X_test)
bagging_accuracy = accuracy_score(y_test, bagging_pred)

print("=== BAGGING RESULTS ===")


print(f"Single Tree Accuracy: {single_accuracy:.4f}")
print(f"Bagging Accuracy: {bagging_accuracy:.4f}")
print(f"Improvement: {((bagging_accuracy - single_accuracy) / single_accuracy * 100):.2f}%")

6. Write a simple machine learning program to implement deep learning.


pip install tensorflow matplotlib scikit-learn seaborn

Program:

import tensorflow as tf
from tensorflow import keras
import numpy as np

# Load data
(x_train, y_train), (x_test, y_test) = [Link].load_data()

# Preprocess
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
x_test = x_test.reshape(-1, 784).astype('float32') / 255.0

# Build model
model = [Link]([
[Link](128, activation='relu', input_shape=(784,)),
[Link](10, activation='softmax')
])

# Compile
[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# Train
[Link](x_train, y_train, epochs=5, batch_size=32, validation_split=0.2)

# Evaluate
test_loss, test_acc = [Link](x_test, y_test)
print(f'\nTest accuracy: {test_acc:.4f}')

# Predict
predictions = [Link](x_test)
print(f'First prediction: {[Link](predictions[0])}, Actual: {y_test[0]}')

You might also like