0% found this document useful (0 votes)
4 views40 pages

Practical 1

The document outlines practical implementations of various machine learning algorithms, including FIND-S, Candidate Elimination, ID3 decision tree, Linear Regression, Logistic Regression, and K-Nearest Neighbors. It also covers concepts like bias, variance, cross-validation, and categorical encoding techniques. Each practical includes code examples, outputs, and explanations of the algorithms and their results.

Uploaded by

shreya
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)
4 views40 pages

Practical 1

The document outlines practical implementations of various machine learning algorithms, including FIND-S, Candidate Elimination, ID3 decision tree, Linear Regression, Logistic Regression, and K-Nearest Neighbors. It also covers concepts like bias, variance, cross-validation, and categorical encoding techniques. Each practical includes code examples, outputs, and explanations of the algorithms and their results.

Uploaded by

shreya
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

Practical 1

Implement and demonstrate the FIND-S algorithm for finding the most specific
hypothesis based on a given set of training data samples. Read the training data
from a .CSV file

import csv

# Load CSV data


def load_data(filename):
with open(filename, 'r') as file:
data = list([Link](file))
return data

# FIND-S Algorithm
def find_s(data):
# Number of attributes (excluding target)
num_attributes = len(data[0]) - 1

# Initialize hypothesis with most specific values


hypothesis = ['Ø'] * num_attributes

print("\nInitial Hypothesis:", hypothesis)

for i, row in enumerate(data):


if row[-1].lower() == "yes": # Consider only positive examples
print(f"\nProcessing positive example {i+1}: {row[:-1]}")

for j in range(num_attributes):
if hypothesis[j] == 'Ø':
hypothesis[j] = row[j]
elif hypothesis[j] != row[j]:
hypothesis[j] = '?'

print("Updated Hypothesis:", hypothesis)

return hypothesis

# Main Execution
data = load_data("training_data.csv")

# Remove header
data = data[1:]

final_hypothesis = find_s(data)

print("\nFinal Hypothesis:", final_hypothesis)


Output

Initial Hypothesis: ['Ø', 'Ø', 'Ø', 'Ø', 'Ø', 'Ø']

Processing positive example 1: ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']


Updated Hypothesis: ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']

Processing positive example 2: ['Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same']


Updated Hypothesis: ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']

Processing positive example 4: ['Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change']


Updated Hypothesis: ['Sunny', 'Warm', '?', 'Strong', '?', '?']

Final Hypothesis: ['Sunny', 'Warm', '?', 'Strong', '?', '?']


Practical 2

For a given set of training data examples stored in a .CSV file, implement and
demonstrate the Candidate- Elimination algorithm to output a description of the
set of all hypotheses consistent with the training examples.

import csv

# Load data
def load_data(filename):
with open(filename, 'r') as file:
data = list([Link](file))
return data

# Check consistency
def is_consistent(h, x):
for i in range(len(h)):
if h[i] != '?' and h[i] != x[i]:
return False
return True

# Candidate Elimination Algorithm


def candidate_elimination(data):
num_attr = len(data[0]) - 1

# Initialize S and G
S = ['Ø'] * num_attr
G = [['?'] * num_attr]

print("\nInitial S:", S)
print("Initial G:", G)

for row in data:


x = row[:-1]
label = row[-1]

if [Link]() == "yes": # Positive example


# Remove inconsistent hypotheses from G
G = [g for g in G if is_consistent(g, x)]

# Update S
for i in range(num_attr):
if S[i] == 'Ø':
S[i] = x[i]
elif S[i] != x[i]:
S[i] = '?'

else: # Negative example


new_G = []
for g in G:
if is_consistent(g, x):
for i in range(num_attr):
if g[i] == '?':
if S[i] != x[i]:
new_h = [Link]()
new_h[i] = S[i]
new_G.append(new_h)
else:
new_G.append(g)

G = new_G

print("\nAfter example:", row)


print("S:", S)
print("G:", G)

return S, G

# Main
data = load_data("training_data.csv")
data = data[1:] # remove header

S, G = candidate_elimination(data)

print("\nFinal Specific Boundary S:", S)


print("Final General Boundary G:", G)
Output

Initial S: ['Ø', 'Ø', 'Ø', 'Ø', 'Ø', 'Ø']


Initial G: [['?', '?', '?', '?', '?', '?']]

After example: ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same', 'Yes']


S: ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
G: [['?', '?', '?', '?', '?', '?']]

After example: ['Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same', 'Yes']


S: ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
G: [['?', '?', '?', '?', '?', '?']]

After example: ['Rainy', 'Cold', 'High', 'Strong', 'Warm', 'Change', 'No']


S: ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
G: [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', 'Same']]

After example: ['Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change', 'Yes']


S: ['Sunny', 'Warm', '?', 'Strong', '?', '?']
G: [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]

Final Specific Boundary S: ['Sunny', 'Warm', '?', 'Strong', '?', '?']


Final General Boundary G: [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?']]
Practical 3

Write a program to demonstrate the working of the decision tree based ID3
algorithm. Use an appropriate data set for building the decision tree and apply
this knowledge to classify a new sample.

import pandas as pd
import math
import [Link] as plt

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

def entropy(target_col):
values = target_col.value_counts()
total = len(target_col)
ent = 0

for v in values:
p = v / total
ent -= p * math.log2(p)

return ent

def information_gain(data, attr, target):


total_entropy = entropy(data[target])

values = data[attr].unique()
weighted_entropy = 0

for v in values:
subset = data[data[attr] == v]
weighted_entropy += (len(subset) / len(data)) * entropy(subset[target])

return total_entropy - weighted_entropy

def id3(data, attributes, target):


# If all examples are same
if len(data[target].unique()) == 1:
return data[target].iloc[0]

# If no attributes left
if len(attributes) == 0:
return data[target].mode()[0]
# Select best attribute
gains = [information_gain(data, attr, target) for attr in attributes]
best_attr = attributes[[Link](max(gains))]

tree = {best_attr: {}}

for value in data[best_attr].unique():


subset = data[data[best_attr] == value]

if [Link]:
tree[best_attr][value] = data[target].mode()[0]
else:
remaining_attrs = [attr for attr in attributes if attr != best_attr]
subtree = id3(subset, remaining_attrs, target)
tree[best_attr][value] = subtree

return tree

attributes = list([Link][:-1])
tree = id3(data, attributes, "PlayTennis")

def classify(tree, sample):


if not isinstance(tree, dict):
return tree

root = list([Link]())[0]
value = sample[root]

if value in tree[root]:
return classify(tree[root][value], sample)
else:
return "Unknown"

# Test sample
sample = {
"Outlook": "Sunny",
"Temperature": "Cool",
"Humidity": "High",
"Wind": "Strong"
}

result = classify(tree, sample)

print("\nNew Sample:", sample)


print("Prediction:", result)
def plot_tree(tree, x=0.5, y=1.0, dx=0.3):
if isinstance(tree, dict):
root = list([Link]())[0]

[Link](x, y, root, ha='center',


bbox=dict(boxstyle="round", fc="lightblue"))

branches = tree[root]
n = len(branches)
x_start = x - dx * (n - 1) / 2

for i, (value, subtree) in enumerate([Link]()):


child_x = x_start + i * dx
child_y = y - 0.2

# Draw line
[Link]([x, child_x], [y, child_y])

# Edge label
[Link]((x + child_x) / 2, (y + child_y) / 2, value)

if isinstance(subtree, dict):
plot_tree(subtree, child_x, child_y, dx / 1.5)
else:
[Link](child_x, child_y, subtree, ha='center',
bbox=dict(boxstyle="round", fc="lightgreen"))

# Draw Tree
[Link](figsize=(10, 6))
plot_tree(tree)
[Link]('off')
[Link]("Decision Tree (ID3)")
[Link]()
Output

New Sample: {'Outlook': 'Sunny', 'Temperature': 'Cool', 'Humidity': 'High', 'Wind': 'Strong'}
Prediction: No
Practical 4

Exercises to solve the real-world problems using the following machine


learning methods:

a) Linear Regression

import pandas as pd
from sklearn.linear_model import LinearRegression

data = [Link]({
'Area': [1000, 1500, 2000, 2500, 3000],
'Price': [2, 3, 4, 5, 6] # in lakhs
})

# Features and Target


X = data[['Area']]
y = data['Price']

model = LinearRegression()
[Link](X, y)

area = [Link]({'Area': [2200]})


prediction = [Link](area)

print("Predicted Price:", round(prediction[0], 2), "lakhs")

print("\nModel Equation:")
print(f"Price = {model.coef_[0]:.4f} * Area + {model.intercept_:.4f}")

b) Logistic Regression

import pandas as pd
from sklearn.linear_model import LogisticRegression

data = [Link]({
'Hours': [1, 2, 3, 4, 5, 6],
'Pass': [0, 0, 0, 1, 1, 1]
})

X = data[['Hours']]
y = data['Pass']

model = LogisticRegression()
[Link](X, y)

hours = [Link]({'Hours': [3.5]})


prediction = [Link](hours)

print("Pass (1) / Fail (0):", prediction[0])

c) Binary Classifier

import pandas as pd
from [Link] import KNeighborsClassifier

# Dataset
data = [Link]({
'Size': [1, 2, 3, 4, 5, 6],
'Label': [0, 0, 0, 1, 1, 1] # 0 = benign, 1 = malignant
})

X = data[['Size']]
y = data['Label']

# Model
model = KNeighborsClassifier(n_neighbors=3)
[Link](X, y)

# Prediction
tumor = [Link]({'Size': [3.5]})
prediction = [Link](tumor)

label_map = {0: "Benign", 1: "Malignant"}


print("Tumor Type:", label_map[prediction[0]])
Output

a) Linear Regression

Predicted Price: 4.4 lakhs

Model Equation:
Price = 0.0020 * Area + 0.0000

b) Logistic Regression

Pass (1) / Fail (0): 1

c) Binary Classifier

Tumor Type: Benign


Practical 5

Develop a program for Bias, Variance, Remove duplicates, Cross Validation


import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
from [Link] import DummyRegressor
from [Link] import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score

model = DummyRegressor(strategy="mean")

#Bias

[Link](42)
X = [Link](0, 10, 50).reshape(-1, 1)
y = [Link](X).flatten() + [Link](0, 0.2, 50)

model = LinearRegression()
[Link](X, y)

pred = [Link](X)

bias = mean_squared_error(y, pred)

print("BIAS RESULT:")
print("Bias (High):", bias)
print("\n")

#variance

[Link](42)
A = [Link](1, 10, 50).reshape(-1, 1)

B = 2 * [Link]() + [Link](0, 2, 50)

errors = []

for i in range(10):
# Random train-test split each time
A_train, A_test, B_train, B_test = train_test_split(
A, B, test_size=0.3
)

model = DecisionTreeRegressor(max_depth=None)
[Link](A_train, B_train)

pred = [Link](A_test)

mse = mean_squared_error(B_test, pred)


[Link](mse)

print("VARIANCE RESULT:")
print("Errors:", errors)
print("Variance in errors:", [Link](errors))
print("\n")

#cross validation

P = [Link]([[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]])
Q = [Link]([1,2,3,4,5,6,7,8,9,10])

model = LinearRegression()

scores = cross_val_score(model, P, Q, cv=5)

print("CROSS VALIDATION RESULTS:")


print("Scores:", scores)
print("Average Score:", [Link]())
print("\n")

#remove duplicates

data = [Link]({
'Name': ['A', 'B', 'A', 'C', 'B'],
'Marks': [90, 80, 90, 70, 80]
})

print("REMOVE DUPLICATE RESULT:")

print("Original Data:\n", data)

clean_data = data.drop_duplicates()

print("\nAfter Removing Duplicates:\n", clean_data)


Output

BIAS RESULT:
Bias (High): 0.49258786763231205

VARIANCE RESULT:
Errors: [6.4987114841914515, 8.046849439391528, 7.677328351045775,
5.4556405150999145, 5.9767210775647435, 7.125785491961824, 9.150038280513225,
4.787860465565026, 8.12243023625064, 6.376672422826398]
Variance in errors: 1.63573919190639

CROSS VALIDATION RESULTS:


Scores: [1. 1. 1. 1. 1.]
Average Score: 1.0

REMOVE DUPLICATE RESULT:


Original Data:
Name Marks
0 A 90
1 B 80
2 A 90
3 C 70
4 B 80

After Removing Duplicates:


Name Marks
0 A 90
1 B 80
3 C 70
Practical 6

Write a program to implement Categorical Encoding, One-hot Encoding


import pandas as pd
from [Link] import LabelEncoder

# Categorial Encoding
# Dataset
data = [Link]({
'Color': ['Red', 'Blue', 'Green', 'Blue', 'Red']
})

print("Original Data:\n", data)

le = LabelEncoder()
data['Color_Encoded'] = le.fit_transform(data['Color'])

print("\nAfter Label Encoding:\n", data)

# One-Hot Encoding
one_hot = pd.get_dummies(data['Color'])

print("\nOne-Hot Encoded Data:\n", one_hot)


Output

Original Data:
Color
0 Red
1 Blue
2 Green
3 Blue
4 Red

After Label Encoding:


Color Color_Encoded
0 Red 2
1 Blue 0
2 Green 1
3 Blue 0
4 Red 2

One-Hot Encoded Data:


Blue Green Red
0 False False True
1 True False False
2 False True False
3 True False False
4 False False True
Practical 7

Build an Artificial Neural Network by implementing the Back propagation


algorithm and test the same using appropriate data sets.

import numpy as np

# Input dataset (XOR)


X = [Link]([[0,0],
[0,1],
[1,0],
[1,1]])

# Output
y = [Link]([[0],[1],[1],[0]])

# Activation function
def sigmoid(x):
return 1 / (1 + [Link](-x))

# Derivative
def sigmoid_derivative(x):
return x * (1 - x)

# Initialize weights
[Link](1)

input_layer_neurons = 2
hidden_layer_neurons = 2
output_neurons = 1

# Weights
W1 = [Link](size=(input_layer_neurons, hidden_layer_neurons))
W2 = [Link](size=(hidden_layer_neurons, output_neurons))

# Bias
b1 = [Link](size=(1, hidden_layer_neurons))
b2 = [Link](size=(1, output_neurons))

# Training
epochs = 10000
learning_rate = 0.1

for i in range(epochs):
# Forward pass
hidden_input = [Link](X, W1) + b1
hidden_output = sigmoid(hidden_input)

final_input = [Link](hidden_output, W2) + b2


final_output = sigmoid(final_input)

# Error
error = y - final_output

# Backpropagation
d_output = error * sigmoid_derivative(final_output)

error_hidden = d_output.dot(W2.T)
d_hidden = error_hidden * sigmoid_derivative(hidden_output)

# Update weights
W2 += hidden_output.[Link](d_output) * learning_rate
W1 += [Link](d_hidden) * learning_rate

b2 += [Link](d_output, axis=0, keepdims=True) * learning_rate


b1 += [Link](d_hidden, axis=0, keepdims=True) * learning_rate

print("Original output:\n",y)
# Output
print("Final Output after training:\n", final_output)
Output

Original output:
[[0]
[1]
[1]
[0]]

Final Output after training:


[[0.07304485]
[0.93084242]
[0.93122512]
[0.07564609]]
Practical 8

Write a program to implement k-Nearest Neighbors algorithm to classify the


iris data set. Print both correct and wrong predictions
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score

iris = load_iris()
X = [Link]
y = [Link]

X_train, X_test, y_train, y_test = train_test_split(


X, y, test_size=0.4, random_state=42
)

model = KNeighborsClassifier(n_neighbors=5)
[Link](X_train, y_train)

y_pred = [Link](X_test)

print("\nCorrect Predictions:\n")

correct = []

# collect correct predictions first


for i in range(len(y_test)):
if y_test[i] == y_pred[i]:
[Link]((y_test[i], y_pred[i]))

for i in range(0, len(correct), 2):


left = correct[i]

if i + 1 < len(correct):
right = correct[i + 1]
print(f"Actual: {left[0]} Pred: {left[1]} | Actual: {right[0]} Pred: {right[1]}")
else:
print(f"Actual: {left[0]} Pred: {left[1]}")

print("\nWrong Predictions:\n")
for i in range(len(y_test)):
if y_test[i] != y_pred[i]:
print(f"Actual: {y_test[i]} Pred: {y_pred[i]}")

print("\nAccuracy:", accuracy_score(y_test, y_pred))


Output

Correct Predictions:

Actual: 1 Pred: 1 | Actual: 0 Pred: 0


Actual: 2 Pred: 2 | Actual: 1 Pred: 1
Actual: 1 Pred: 1 | Actual: 0 Pred: 0
Actual: 1 Pred: 1 | Actual: 2 Pred: 2
Actual: 1 Pred: 1 | Actual: 1 Pred: 1
Actual: 2 Pred: 2 | Actual: 0 Pred: 0
Actual: 0 Pred: 0 | Actual: 0 Pred: 0
Actual: 0 Pred: 0 | Actual: 1 Pred: 1
Actual: 2 Pred: 2 | Actual: 1 Pred: 1
Actual: 1 Pred: 1 | Actual: 2 Pred: 2
Actual: 0 Pred: 0 | Actual: 2 Pred: 2
Actual: 0 Pred: 0 | Actual: 2 Pred: 2
Actual: 2 Pred: 2 | Actual: 2 Pred: 2
Actual: 2 Pred: 2 | Actual: 2 Pred: 2
Actual: 0 Pred: 0 | Actual: 0 Pred: 0
Actual: 0 Pred: 0 | Actual: 0 Pred: 0
Actual: 1 Pred: 1 | Actual: 0 Pred: 0
Actual: 0 Pred: 0 | Actual: 2 Pred: 2
Actual: 1 Pred: 1 | Actual: 0 Pred: 0
Actual: 0 Pred: 0 | Actual: 0 Pred: 0
Actual: 2 Pred: 2 | Actual: 1 Pred: 1
Actual: 1 Pred: 1 | Actual: 0 Pred: 0
Actual: 0 Pred: 0 | Actual: 1 Pred: 1
Actual: 2 Pred: 2 | Actual: 1 Pred: 1
Actual: 2 Pred: 2 | Actual: 1 Pred: 1
Actual: 2 Pred: 2 | Actual: 1 Pred: 1
Actual: 0 Pred: 0 | Actual: 2 Pred: 2
Actual: 1 Pred: 1 | Actual: 0 Pred: 0
Actual: 0 Pred: 0 | Actual: 0 Pred: 0
Actual: 1 Pred: 1

Wrong Predictions:

Actual: 2 Pred: 1

Accuracy: 0.9833333333333333
Practical 9

Implement the non-parametric Locally Weighted Regression algorithm in order


to fit data points Select appropriate data set for your experiment and draw
graphs.
import numpy as np
import [Link] as plt

# Generate data
[Link](0)
X = [Link](-3, 3, 50)
y = [Link](X) + [Link](0, 0.1, 50)

# Add bias term


X_mat = np.c_[[Link](len(X)), X]

# Locally Weighted Regression


def lwr(query_point, X, y, tau):
m = len(X)
W = [Link](m)

for i in range(m):
diff = query_point - X[i]
W[i, i] = [Link](-[Link](diff, diff) / (2 * tau**2))

# Theta = (X^T W X)^-1 X^T W y


theta = [Link](X.T @ W @ X) @ (X.T @ W @ y)

return query_point @ theta

# Predictions
tau = 0.5 # bandwidth (controls smoothness)
y_pred = []

for i in range(len(X_mat)):
pred = lwr(X_mat[i], X_mat, y, tau)
y_pred.append(pred)

# Plot
[Link](X, y, label="Data")
[Link](X, y_pred, color='red', label="LWR Fit")
[Link]("Locally Weighted Regression")
[Link]()
[Link]()
Output
Practical 10

Assuming a set of documents that need to be classified, use the naïve Bayesian
Classifier model to perform this task. Built-in Java classes/API can be used to
write the program. Calculate the accuracy, precision, and recall for your data
set.

import math
from collections import defaultdict
sports_docs = [
"team win game",
"player score goal",
"match win team"
]

not_sports_docs = [
"market stock profit",
"business growth economy",
"finance market trade"
]
sports = defaultdict(int)
not_sports = defaultdict(int)
sports_count = 0
not_sports_count = 0

def train():
global sports_count, not_sports_count

for doc in sports_docs:


for word in [Link]():
sports[word] += 1
sports_count += 1

for doc in not_sports_docs:


for word in [Link]():
not_sports[word] += 1
not_sports_count += 1

def classify(doc):
p_sports = [Link](0.5)
p_not_sports = [Link](0.5)

for word in [Link]():

count_s = [Link](word, 1)
count_ns = not_sports.get(word, 1)

p_sports += [Link](count_s / sports_count)


p_not_sports += [Link](count_ns / not_sports_count)

return "Sports" if p_sports > p_not_sports else "Not Sports"

train()

test_docs = [
"team score win",
"market profit trade",
"player goal match",
"business economy growth"
]

actual = [
"Sports",
"Not Sports",
"Sports",
"Not Sports"
]

tp = tn = fp = fn = 0

print("\nPredictions:\n")

for i in range(len(test_docs)):
predicted = classify(test_docs[i])

print("Doc:", test_docs[i])
print("Predicted:", predicted, " | Actual:", actual[i])
print()

if predicted == "Sports" and actual[i] == "Sports":


tp += 1
elif predicted == "Not Sports" and actual[i] == "Not Sports":
tn += 1
elif predicted == "Sports":
fp += 1
else:
fn += 1

total = tp + tn + fp + fn

accuracy = (tp + tn) / total


precision = tp / (tp + fp) if (tp + fp) != 0 else 0
recall = tp / (tp + fn) if (tp + fn) != 0 else 0

print("FINAL RESULTS ")


print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
Output

Predictions:

Doc: team score win


Predicted: Sports | Actual: Sports

Doc: market profit trade


Predicted: Not Sports | Actual: Not Sports

Doc: player goal match


Predicted: Not Sports | Actual: Sports

Doc: business economy growth


Predicted: Not Sports | Actual: Not Sports

FINAL RESULTS
Accuracy: 0.75
Precision: 1.0
Recall: 0.5
Practical 11

Apply EM algorithm to cluster a Heart Disease Data Set. Use the same data set
for clustering using k-Means algorithm. Compare the results of these two
algorithms and comment on the quality of clustering. You can add Java/Python
ML library classes/API in the program.

import pandas as pd
from [Link] import StandardScaler
from [Link] import KMeans
from [Link] import GaussianMixture
from [Link] import accuracy_score

data = pd.read_csv("[Link]") # Dataset

# Features and Target


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

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

kmeans = KMeans(n_clusters=2, random_state=42)


kmeans_labels = kmeans.fit_predict(X_scaled)

em = GaussianMixture(n_components=2, random_state=42)
em_labels = em.fit_predict(X_scaled)

def adjust_labels(true, pred):


acc1 = accuracy_score(true, pred)
acc2 = accuracy_score(true, 1 - pred)
return max(acc1, acc2)

kmeans_acc = adjust_labels(y, kmeans_labels)


em_acc = adjust_labels(y, em_labels)

print("K-Means Accuracy:", kmeans_acc)


print("EM (Gaussian Mixture) Accuracy:", em_acc)

if em_acc > kmeans_acc:


print("\nEM performs better than K-Means")
elif em_acc < kmeans_acc:
print("\nK-Means performs better than EM")
else:
print("\nBoth perform equally")
Output

K-Means Accuracy: 0.8118811881188119


EM (Gaussian Mixture) Accuracy: 0.7194719471947195

K-Means performs better than EM


Practical 12

Exploratory Data Analysis for Classification using Pandas or Matplotlib

import [Link] as plt


from [Link] import load_iris

iris = load_iris()
X = [Link]
y = [Link]
feature_names = iris.feature_names
[Link]()
[Link](y, bins=3, edgecolor='black')
[Link]("Class Distribution (Iris)")
[Link]("Class")
[Link]("Count")
[Link]([0, 1, 2])
[Link]()

[Link]()
[Link](X[:, 0], alpha=0.5, label=feature_names[0])
[Link](X[:, 1], alpha=0.5, label=feature_names[1])
[Link](X[:, 2], alpha=0.5, label=feature_names[2])
[Link](X[:, 3], alpha=0.5, label=feature_names[3])
[Link]("Feature Distributions")
[Link]("Value")
[Link]("Frequency")
[Link]()
[Link]()

[Link]()

[Link](X[y == 0, 0], X[y == 0, 2], label="Setosa (0)")


[Link](X[y == 1, 0], X[y == 1, 2], label="Versicolor (1)")
[Link](X[y == 2, 0], X[y == 2, 2], label="Virginica (2)")

[Link]("Sepal Length vs Petal Length")


[Link]("Sepal Length (cm)")
[Link]("Petal Length (cm)")
[Link]()
[Link]()

[Link]()

[Link](X[y == 0, 1], X[y == 0, 3], label="Setosa (0)")


[Link](X[y == 1, 1], X[y == 1, 3], label="Versicolor (1)")
[Link](X[y == 2, 1], X[y == 2, 3], label="Virginica (2)")

[Link]("Sepal Width vs Petal Width")


[Link]("Sepal Width (cm)")
[Link]("Petal Width (cm)")
[Link]()
[Link]()
Output
Practical 13

Write a Python 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

import pandas as pd
import numpy as np

from [Link] import DiscreteBayesianNetwork


from [Link] import MaximumLikelihoodEstimator
from [Link] import VariableElimination

# Load dataset
df = pd.read_csv("[Link]")
[Link] = [[Link]().lower() for c in [Link]]
df = [Link]("?", [Link])

# Rename target column if needed


if "num" in [Link]:
df = [Link](columns={"num": "heartdisease"})
elif "target" in [Link]:
df = [Link](columns={"target": "heartdisease"})
elif "heartdisease" not in [Link]:
raise ValueError("Target column not found. Expected one of: num, target, heartdisease")

# Required columns
required_cols = [
"age", "sex", "cp", "trestbps", "chol", "fbs",
"restecg", "thalach", "exang", "oldpeak",
"slope", "ca", "thal", "heartdisease"
]

missing = [c for c in required_cols if c not in [Link]]


if missing:
raise ValueError(f"Missing required columns: {missing}")

df = df[required_cols].copy()

# Convert to numeric
for col in [Link]:
df[col] = pd.to_numeric(df[col], errors="coerce")

# Binary target
df["heartdisease"] = (df["heartdisease"] > 0).astype(int)

# Discretize continuous features


df["age"] = [Link](df["age"], bins=3, labels=["young", "middle", "old"])
df["trestbps"] = [Link](df["trestbps"], bins=3, labels=["low", "medium", "high"])
df["chol"] = [Link](df["chol"], bins=3, labels=["low", "medium", "high"])
df["thalach"] = [Link](df["thalach"], bins=3, labels=["low", "medium", "high"])
df["oldpeak"] = [Link](df["oldpeak"], bins=3, labels=["low", "medium", "high"])

# Drop missing values


df = [Link]().reset_index(drop=True)

print("Dataset loaded successfully.")


print("Shape after cleaning:", [Link])

# Bayesian network structure


model = DiscreteBayesianNetwork([
("age", "heartdisease"),
("sex", "heartdisease"),
("cp", "heartdisease"),
("fbs", "heartdisease"),
("exang", "heartdisease"),
("heartdisease", "restecg"),
("heartdisease", "chol"),
("heartdisease", "thalach"),
("heartdisease", "oldpeak"),
("heartdisease", "slope"),
("heartdisease", "ca"),
("heartdisease", "thal")
])

# Learn parameters
mle = MaximumLikelihoodEstimator(model, df)
cpds = mle.get_parameters()
model.add_cpds(*cpds)

print("Model valid:", model.check_model())

# Inference
infer = VariableElimination(model)

def summarize_result(result, title="Result"):


no_disease = float([Link][0])
disease = float([Link][1])

print(f"\n{title}")
print(f"No heart disease probability: {no_disease:.4f}")
print(f"Heart disease probability: {disease:.4f}")

if disease >= 0.5:


print("Prediction: Heart disease likely present")
else:
print("Prediction: Heart disease likely absent")

# Print valid states in clean format


print("\nUnique states in the dataset:")
for col in ["cp", "exang", "restecg", "thal", "ca", "slope", "sex", "fbs"]:
states = sorted(df[col].dropna().unique())
clean_states = [[Link]() if hasattr(x, "item") else x for x in states]
print(f"{col}: {clean_states}")

# Example inference queries


q1 = [Link](variables=["heartdisease"], evidence={"cp": 3, "exang": 1})
summarize_result(q1, "Query 1: cp=3, exang=1")

q2 = [Link](variables=["heartdisease"], evidence={"restecg": 1})


summarize_result(q2, "Query 2: restecg=1")

q3 = [Link](variables=["heartdisease"], evidence={"thal": 2, "ca": 0, "oldpeak": "low"})


summarize_result(q3, "Query 3: thal=2, ca=0, oldpeak=low")

sample_evidence = {
"age": "old",
"sex": 1,
"cp": 3,
"fbs": 0,
"exang": 1,
"restecg": 0
}

q4 = [Link](variables=["heartdisease"], evidence=sample_evidence)
summarize_result(q4, "Sample Patient Diagnosis")
Output

Dataset loaded successfully.


Shape after cleaning: (303, 14)
Model valid: True

Unique states in the dataset:


cp: [0, 1, 2, 3]
exang: [0, 1]
restecg: [0, 1, 2]
thal: [0, 1, 2, 3]
ca: [0, 1, 2, 3, 4]
slope: [0, 1, 2]
sex: [0, 1]
fbs: [0, 1]

Query 1: cp=3, exang=1


No heart disease probability: 0.2706
Heart disease probability: 0.7294
Prediction: Heart disease likely present

Query 2: restecg=1
No heart disease probability: 0.3545
Heart disease probability: 0.6455
Prediction: Heart disease likely present

Query 3: thal=2, ca=0, oldpeak=low


No heart disease probability: 0.0720
Heart disease probability: 0.9280
Prediction: Heart disease likely present

Sample Patient Diagnosis


No heart disease probability: 0.0000
Heart disease probability: 1.0000
Prediction: Heart disease likely present
Practical 14

Write a program to Implement Support Vector Machines and Principal


Component Analysis

import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import PCA
from [Link] import SVC
from [Link] import accuracy_score
import numpy as np
import [Link] as plt

# Load dataset
iris = load_iris()
X = [Link]
y = [Link]

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

# Scale data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)

# PCA
pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train)
X_test_pca = [Link](X_test)

print("Explained Variance:", pca.explained_variance_ratio_)

# SVM
model = SVC(kernel='linear')
[Link](X_train_pca, y_train)

# Prediction
y_pred = [Link](X_test_pca)

# Accuracy
accuracy = accuracy_score(y_test, y_pred)

print("\nAccuracy:", accuracy)
x_min, x_max = X_train_pca[:, 0].min() - 1, X_train_pca[:, 0].max() + 1
y_min, y_max = X_train_pca[:, 1].min() - 1, X_train_pca[:, 1].max() + 1

xx, yy = [Link](
[Link](x_min, x_max, 200),
[Link](y_min, y_max, 200)
)

Z = [Link](np.c_[[Link](), [Link]()])
Z = [Link]([Link])

[Link]()
[Link](xx, yy, Z, alpha=0.3)

[Link](X_train_pca[:, 0], X_train_pca[:, 1], c=y_train, label="Train")

[Link](X_test_pca[:, 0], X_test_pca[:, 1], c=y_test, marker="x", label="Test")

[Link]("SVM Decision Boundary (PCA Reduced)")


[Link]("PCA Component 1")
[Link]("PCA Component 2")
[Link]()
[Link]()
Output

Explained Variance: [0.7070102 0.24507687]

Accuracy: 0.9333333333333333
Practical 15

Write a program to Implement Principal Component Analysis

import pandas as pd
from [Link] import load_iris
from [Link] import StandardScaler
from [Link] import PCA
import [Link] as plt

# Load dataset
iris = load_iris()
X = [Link]
y = [Link]

# Step 1: Standardize data


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Step 2: Apply PCA (reduce to 2 components)


pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# Step 3: Print variance


print("Explained Variance Ratio:", pca.explained_variance_ratio_)

# Step 4: Plot
[Link](X_pca[:, 0], X_pca[:, 1], c=y)
[Link]("Principal Component 1")
[Link]("Principal Component 2")
[Link]("PCA of Iris Dataset")
[Link]()
Output

Explained Variance Ratio: [0.72962445 0.22850762]

You might also like