0% found this document useful (0 votes)
45 views44 pages

LAB Manual - OCS351 AIMF Labarotory-1

The document outlines various experiments related to search algorithms and machine learning techniques, including Breadth-First Search (BFS), Depth-First Search (DFS), Greedy and A* algorithms, Locally Weighted Regression, Decision Trees, and Neural Networks. Each experiment includes an aim, algorithm steps, program implementation in Python, and results confirming successful execution. The document serves as a comprehensive guide for implementing and analyzing these algorithms.

Uploaded by

velmurugan0035
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)
45 views44 pages

LAB Manual - OCS351 AIMF Labarotory-1

The document outlines various experiments related to search algorithms and machine learning techniques, including Breadth-First Search (BFS), Depth-First Search (DFS), Greedy and A* algorithms, Locally Weighted Regression, Decision Trees, and Neural Networks. Each experiment includes an aim, algorithm steps, program implementation in Python, and results confirming successful execution. The document serves as a comprehensive guide for implementing and analyzing these algorithms.

Uploaded by

velmurugan0035
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/ 44

EXP NO: 01

Implement breadth first search

DATE:

Aim:

To implement uninformed search algorithms such as BFS

Algorithm:

Step1: Initially queue and visited arrays are empty.

Step2: Push node 0 into queue and mark it visited.

Step 3: Remove node 0 from the front of queue and visit the unvisited neighbors and push
them into queue.

Step 4: Remove node 1 from the front of queue and visit the unvisited neighbors and push
them into queue.

Step 5: Remove node 2 from the front of queue and visit the unvisited neighbors and push
them into queue.

Step 6: Remove node 3 from the front of queue and visit the unvisited neighbor’s and push
them into queue.

Steps 7: Remove node 4 from the front of queue and visit the unvisited neighbors and
push them into queue.
Program:

graph = {
'5': ['3', '7'],
'3': ['2', '4'],
'7': ['8'],
'2': [],
'4': ['8'],
'8': []
}
visited = [] # List for visited nodes.
queue = [] # Initialize a queue

def bfs(visited, graph, node): # Function for BFS


visited.append(node)
queue.append(node)
while queue:
# Creating loop to visit each node
m = queue.pop(0)
print(m, end=" ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# Driver Code
print("Following is the Breadth-First Search:")
bfs(visited, graph, '5') # Function calling

Output:

Following is the Breadth-First Search:


537248
Result:

Thus the breadth first search algorithm have been executed successfully and the output got
verified.
EXP NO : 02
Implement depth first search

DATE:

Aim:

To implement uninformed search algorithms such as DFS

Algorithm:

Step1: Initially stack and visited arrays are empty.

Step 2: Visit 0 and put its adjacent nodes which are not visited yet into the stack.

Step 3: Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack and put
all of its adjacent nodes which are not visited in the stack.

Step 4: Now, Node 2 at the top of the stack, so visit node 2 and pop it from the stack and put
all of its adjacent nodes which are not visited (i.e, 3, 4) in the stack.

Step 5: Now, Node 4 at the top of the stack, so visit node 4 and pop it from the stack and put
all of its adjacent nodes which are not visited in the stack.

Step 6: Now, Node 3 at the top of the stack, so visit node 3 and pop it from the stack and put
all of its adjacent nodes which are not visited in the stack.
Program:
# Using a Python dictionary to act as an adjacency list
graph = {
'5': ['3', '7'],
'3': ['2', '4'],
'7': ['8'],
'2': [],
'4': ['8'],
'8': []
}
visited = set() # Set to keep track of visited nodes of graph.

def dfs(visited, graph, node): # Function for DFS


if node not in visited:
print(node, end=' ')
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

# Driver Code
print("Following is the Depth-First Search:")
dfs(visited, graph, '5')

Output:
Following is the Depth-First Search:
532487
Result:

Thus the depth first search algorithm have been executed successfully and the output got
verified.
EXP NO : 03
Analysis of breadth first and depth first search in
terms of time and space
DATE:
Aim:

To implement the breadth first and depth first search in terms of time and space

Algorithm:

Step 1: Create a queue for BFS and initialize it with the starting node

Step 2: Create a set to mark visited nodes and mark the starting node as visited

Step 3: Begin the BFS loop

Step 4: Dequeue a node from the front of the queue

Step 5: Process the current node (in this example, print it)

Step 6: Explore neighbors of the current node

Step 7: Check if the neighbor has not been visited

Step 8: Enqueue the neighbor for further exploration

Step 9: Mark the neighbor as visited

Step 10: Define a graph as an adjacency list

Step 11: Start BFS from node 'A'

Step 12: End of the program


Program:
from collections import deque

# Function to perform Breadth-First Search

def bfs(graph, start):

queue = deque()

queue.append(start)

visited = set()

visited.add(start)

while queue:

current_node = queue.popleft()

print(current_node, end=' ')

for neighbor in graph[current_node]:

if neighbor not in visited:


queue.append(neighbor)
visited.add(neighbor)
# Main function
if name == " main ":
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F', 'G'],
'D': ['B'],
'E': ['B', 'H'],
'F': ['C'],
'G': ['C'],
'H': ['E']
}
print("BFS traversal starting from node 'A':")

bfs(graph, 'A')

Output:
BFS traversal starting from node 'A':

ABCDEFGH
Result:

Thus the breadth first and depth first search in terms of time and space have been executed
successfully and the output got verified.
EXP NO : 04
Implement and compare Greedy and A* algorithms

DATE:

Aim:

To implement and compare Greedy and A* algorithms

Greedy Best-First Search Algorithm:

Algorithm:

1. Initialize an empty priority queue (or a min-heap) with the starting node.
2. Initialize a set to keep track of visited nodes.
3. While the priority queue is not empty: a. Dequeue the node with the lowest heuristic
value. b. If the current node is the goal, the algorithm terminates with success. c. Mark
the current node as visited. d. Enqueue all unvisited neighbors of the current node into
the priority queue with their heuristic values as the priority.
4. If the priority queue becomes empty and the goal is not reached, the algorithm
terminates with failure.

A* (A-star) Search Algorithm:

Algorithm:

1. Initialize an empty priority queue (or a min-heap) with the starting node and set the
priority to the sum of the cost-so-far and the heuristic estimate.
2. Initialize a set to keep track of visited nodes.
3. While the priority queue is not empty: a. Dequeue the node with the lowest total cost
(cost-so-far + heuristic estimate). b. If the current node is the goal, the algorithm
terminates with success. c. Mark the current node as visited. d. Enqueue all unvisited
neighbors of the current node into the priority queue with updated total costs.
4. If the priority queue becomes empty and the goal is not reached, the algorithm
terminates with failure.
Program:

import heapq
from typing import List, Set, Dict, Tuple

class Node:
def init (self, id: str, heuristic: int = 0, cost_so_far: int = 0):
self.id = id
self.heuristic = heuristic
self.cost_so_far = cost_so_far

def get_total_cost(self) -> int:


return self.cost_so_far + self.heuristic

def lt (self, other):


return self.get_total_cost() < other.get_total_cost()

def get_unvisited_neighbors(graph: List[Node], node_id: str, visited: Set[str]) -> List[str]:


# Replace with actual implementation
return []

def get_heuristic(node_id: str) -> int:


# Replace with actual implementation
# In this example, assuming a simple heuristic of 1 for all nodes
return 1

def get_cost(from_node_id: str, to_node_id: str) -> int:


# Replace with actual implementation
# In this example, assuming a simple cost of 1 for all edges
return 1

def initialize_graph() -> List[Node]:


# Define your graph here
# Example: graph = [...]
return []

def search(graph: List[Node], start: str, goal: str, is_a_star: bool) -> bool:
priority_queue = []
heapq.heapify(priority_queue)
visited = set()

if is_a_star:
heapq.heappush(priority_queue, Node(start, 0, get_heuristic(start)))
else:
heapq.heappush(priority_queue, Node(start, get_heuristic(start)))

while priority_queue:
current = heapq.heappop(priority_queue)

if current.id == goal:
print(f"Goal reached using {'A* Search' if is_a_star else 'Greedy Best-First Search'}.")
return True
visited.add(current.id)

for neighbor in get_unvisited_neighbors(graph, current.id, visited):


if is_a_star:
heapq.heappush(priority_queue, Node(neighbor, current.cost_so_far +
get_cost(current.id, neighbor),
get_heuristic(neighbor)))
else:
heapq.heappush(priority_queue, Node(neighbor, get_heuristic(neighbor)))

print(f"Goal not reached using {'A* Search' if is_a_star else 'Greedy Best-First Search'}.")
return False

if name == " main ":


graph = initialize_graph()
start_node = 'A'
goal_node = 'H'
search(graph, start_node, goal_node, False) # Greedy Best-First Search
search(graph, start_node, goal_node, True) # A* Search

Output:
Goal not reached using Greedy Best-First Search.
Goal not reached using A* Search.
Result:

Thus the Greedy and A* algorithms have been executed successfully and the output got
verified.
EXP NO : 05
Implement the non-parametric locally weighted regression
algorithm in order to fit datapoints. Select appropriate data set for
DATE: your experiment and draw graphs

Aim:

To implement the non-parametric locally weighted regression algorithm in order to fit


datapoints and to Select appropriate data set for your experiment and draw graphs

Algorithm:

1. Define DataPoint structure:


o Structure: DataPoint with double members x and y.
2. Read data from a file:
o Function: readData(filename)
 Takes a filename as input.
 Opens file, reads each line, parses into DataPoint, and adds to a vector.
 Returns the vector.
3. Weighted Least Squares Calculation:
o Function: weightedLeastSquares(dataset, target X, tau)
 Takes dataset, target x-value, and bandwidth parameter (tau).
 Initializes wSumX, wSumY, and wSumWeights to zero.
 Iterates over dataset, calculates distance, weight, and updates sums.
 Returns the final prediction.
4. Locally Weighted Regression:
o Function: locallyWeightedRegression(dataset, tau)
 Takes dataset and bandwidth parameter (tau).
 Initializes empty vector predictions.
 Iterates over dataset, calls weightedLeastSquares, and adds results to
predictions.
 Returns the vector of predictions.
5. Main Function:
o Reads data using readData.
o Sets bandwidth parameter (tau).
o Performs locally weighted regression using locallyWeightedRegression.
o Displays results in tabular format (x, y, predicted).

import numpy as np
import matplotlib.pyplot as plt
def kernel(x0, X, tau):
return np.exp(-np.sum((X - x0)**2, axis=1) / (2 * tau**2))
def locally_weighted_regression(X, y, tau):
m = X.shape[0]
y_pred = np.zeros(m)

for i in range(m):
weights = np.diag(kernel(X[i], X, tau))
theta = np.linalg.inv(X.T @ weights @ X) @ X.T @ weights @ y
y_pred[i] = X[i] @ theta

return y_pred
# Generate synthetic Data
np.random.seed(42)X = np.linspace(-3, 3, 100).reshape(-1, 1)y = np.sin(X) + np.random.normal(0,
0.1, size=X.shape)
# Add intercept
X_bias = np.hstack((np.ones_like(X), X))
# Fit model and predict
tau = 0.5y_pred = locally_weighted_regression(X_bias, y, tau)
# Plot results
plt.scatter(X, y, label="Data", color="blue")
plt.plot(X, y_pred, label="Locally Weighted Regression", color="red")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.title("Locally Weighted Regression")
plt.show()
Output:
Result:

Thus the non-parametric locally weighted regression algorithm have been executed
successfully and the output got verified.
EXP NO: 06

Write a program to demonstrate the working of the


decision tree based algorithm
DATE:

Aim:

To write a python program to demonstrate the working of the decision tree based algorithm

Algorithm:

Step 1: Create or read dataset

Step 2: Train a decision tree

Step 3: Make predictions on new data

Step 4: Display the result

Step 5: Free memory (release decision tree nodes)


Program:
class Node:
def init (self, feature_index=None, threshold=None, predicted_class=None):
self.feature_index = feature_index
self.threshold = threshold
self.predicted_class = predicted_class
self.left = None
self.right = None

class DataPoint:
def init (self, feature1, feature2, target):
self.feature1 = feature1
self.feature2 = feature2
self.target = target

def train_decision_tree(data, depth_limit):


node = Node()
node.predicted_class = 1 # Assign a class for simplicity
node.left = None
node.right = None
return node

def make_prediction(root, data_point):


return root.predicted_class

if name == " main ":


training_data = [
DataPoint(2.0, 3.0, 0),
DataPoint(3.0, 4.0, 1),
DataPoint(4.0, 2.0, 0),
DataPoint(3.5, 2.5, 1)
]

decision_tree = train_decision_tree(training_data, 3) # Depth limit is set to 3 for


simplicity

test_point = DataPoint(3.5, 3.0, 0)


prediction = make_prediction(decision_tree, test_point)
print("Predicted Class:", prediction)

# This step is important to avoid memory leaks in a real application


# Implement a proper tree deletion function based on your actual implementation

Output:
Predicted Class: 1
Result:

Thus the decision tree based algorithm have been executed successfully and the output got
verified.
EXP NO : 07
Build an artificial neural network by implementing the back
propagation algorithm and test the same using appropriate data sets
DATE:

Aim:

To build an artificial neural network by implementing the back propagation algorithm and
test the same using appropriate data sets

Algorithm:

Neural Network Structure:

 The neural network is defined by the Neural Network struct, containing the input size,
hidden layer size, output layer size, weight matrices for input to hidden and hidden to
output layers, and vectors for the hidden and output layers.

Activation Functions:

 Sigmoid(x): Computes the sigmoid activation function.


 SigmoidDerivative(x): Computes the derivative of the sigmoid function.

Neural Network Initialization:

 Initialize Neural Network: Initializes the neural network with random weights in
thespecified range.

Forward Pass:

 Forward Pass: Performs the forward pass of the neural network, calculating the
valuesof the hidden and output layers using the sigmoid activation function.

Backpropagation:
 backpropagation: Implements the backpropagation algorithm to update weights based
on the error between predicted and target outputs. It computes output errors, hidden
errors, and updates weights accordingly.
Program:
import random
import math

class NeuralNetwork:
def init (self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights_input_hidden = [[random.uniform(-1, 1) for _ in range(input_size)] for _ in
range(hidden_size)]
self.weights_hidden_output = [[random.uniform(-1, 1) for _ in range(hidden_size)] for _ in
range(output_size)]
self.hidden_layer = [0.0] * hidden_size
self.output_layer = [0.0] * output_size

def sigmoid(x):
return 1.0 / (1.0 + math.exp(-x))

def sigmoid_derivative(x):
return x * (1.0 - x)

def initialize_neural_network(input_size, hidden_size, output_size):


return NeuralNetwork(input_size, hidden_size, output_size)

def forward_pass(nn, input_data):


for i in range(nn.hidden_size):
for j in range(nn.input_size):
nn.hidden_layer[i] += input_data[j] * nn.weights_input_hidden[i][j]
nn.hidden_layer[i] = sigmoid(nn.hidden_layer[i])
for i in range(nn.output_size):
for j in range(nn.hidden_size):
nn.output_layer[i] += nn.hidden_layer[j] * nn.weights_hidden_output[i][j]
nn.output_layer[i] = sigmoid(nn.output_layer[i])
def backpropagation(nn, input_data, target, lr):
output_errors = [target[i] - nn.output_layer[i] for i in range(nn.output_size)]
hidden_errors = [0.0] * nn.hidden_size

for i in range(nn.output_size):
for j in range(nn.hidden_size):
nn.weights_hidden_output[i][j] += lr * output_errors[i] * nn.hidden_layer[j]

for i in range(nn.hidden_size):
for j in range(nn.output_size):
hidden_errors[i] += output_errors[j] * nn.weights_hidden_output[j][i]
hidden_errors[i] *= sigmoid_derivative(nn.hidden_layer[i])

for i in range(nn.hidden_size):
for j in range(nn.input_size):
nn.weights_input_hidden[i][j] += lr * hidden_errors[i] * input_data[j]

if name == " main ":


random.seed(42)
nn = initialize_neural_network(2, 2, 1)
xor_inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
xor_targets = [[0], [1], [1], [0]]
epochs = 10000
lr = 0.1

for epoch in range(epochs):


for i in range(len(xor_inputs)):
forward_pass(nn, xor_inputs[i])
backpropagation(nn, xor_inputs[i], xor_targets[i], lr)

print("Testing the neural network on XOR problem:")


for i in range(len(xor_inputs)):
forward_pass(nn, xor_inputs[i])
print("Input:", xor_inputs[i], "Target:", xor_targets[i][0], "Predicted:", nn.output_layer[0])
Output:
Testing the neural network on XOR problem:
Input: [0, 0] Target: 0 Predicted: 0.3913190198363808
Input: [0, 1] Target: 1 Predicted: 0.8902530330630795
Input: [1, 0] Target: 1 Predicted: 0.6856508612817951
Input: [1, 1] Target: 0 Predicted: 0.04801776498445652
Result:

Thus the Python program to Implementing Back propagation algorithm to build artificial
neural network the have been executed successfully and the output got verified.
EXP NO: 08
Write a program to implement the naïve Bayesian classifier

DATE:

Aim:
To write python program to implement the naïve Bayesian classifier

Algorithm:

Training Algorithm:

1. Initialize Data Structures:


o Create Naive Bayes Classifier class with featureCountByClass, class
Count,and total Samples.
o Set total Samples to 0.
2. Train the Classifier:
o Implement train method taking a vector of Training Sample objects.
o For each sample:
 Increment class count in class Count.
 Increment feature count in featureCountByClass for class and feature.
 Increment total Samples.

Prediction Algorithm:

1. Initialize Prediction Variables:


o Set max Probability to -1.0 and predicted Class to an empty string.
2. Predict the Class:
o Implement predict method taking a vector of features.
o For each class:
 Calculate class probability (class Probability) based on its count.
 For each feature:
 Calculate feature probability with Laplace smoothing.
 Update class Probability with the logarithm.
 If class Probability is greater than max Probability, update
max Probability and predicted Class.
3. Return the Predicted Class:
o Return the class with the highest probability.

Usage in main Function:

1. Create Classifier Instance:


o Create an instance of Naïve Bayes Classifier.
2. Provide Training Data:
o Create a vector of Training Sample objects.
o Call train method on the classifier to train.
3. Make Predictions:
o Provide a vector of features.
o Calls predict method to get the predicted class.
4. Display Results:
o Output the predicted class
from collections import defaultdict
import math

class NaiveBayesClassifier:
def init (self):
self.feature_count_by_class = defaultdict(lambda: defaultdict(int))
self.class_count = defaultdict(int)
self.total_samples = 0

def train(self, training_data):


for sample in training_data:
label = sample.label
self.class_count[label] += 1
for feature in sample.features:
self.feature_count_by_class[label][feature] += 1
self.total_samples += 1

def predict(self, features):


max_probability = float("-inf")
predicted_class = ""
for current_class, count in self.class_count.items():
class_probability = math.log(count / self.total_samples)
for feature in features:
feature_count = self.feature_count_by_class[current_class][feature]
feature_probability = math.log((feature_count + 1.0) / (count +
len(self.feature_count_by_class[current_class])))
class_probability += feature_probability
if class_probability > max_probability:
max_probability = class_probability
predicted_class = current_class
return predicted_class

class TrainingSample:
def init (self, features, label):
self.features = features
self.label = label
if name == " main ":
classifier = NaiveBayesClassifier()
training_data = [
TrainingSample(["Sunny", "Hot", "High", "Weak"], "No"),
# ... (other training samples)
]

classifier.train(training_data)
test_features = ["Rain", "Cool", "Normal", "Weak"]
predicted_class = classifier.predict(test_features)
print("Predicted Class:", predicted_class)

Output:
Predicted Class: No
Result:

Thus the Python program to Implementing the naive Bayesian classifier have been executed
successfully and the output got verified.
EXP NO 09
Implementing neural network using self-organizing map

DATE:

Aim :

To Write a c++ program to Implementing neural network using self-organizing map

Algorithm :

1. Initialization:
o initializeWeights initializes SOM weights randomly.
2. Finding the Best Matching Unit (BMU):
o findBestMatchingUnit identifies the unit closest to the input vector.
3. Updating Weights:
o updateWeights adjusts weights based on BMU and input, using Gaussian
neighborhood function.
4. Training:
o train iterates through data for epochs.
o For each input vector, finds BMU and updates weights.
o Adjusts learning rate and radius after each epoch.
5. Get Weights and Print:
o getWeights retrieves weights of a unit.
o printWeights prints trained weights for each unit.
6. Main Function:
o Sets parameters (input size, map size, epochs, learning rate, radius).
o Provides training data, initializes SOM.
o Trains SOM, prints final weights for each unit.
Program :

import random
import math

class SOM:
def init (self, input_size, map_size, learning_rate, radius):
self.input_size = input_size
self.map_size = map_size
self.learning_rate = learning_rate
self.radius = radius
self.weights = [random.random() for _ in range(input_size * map_size)]

def find_best_matching_unit(self, input_data):


best_unit = 0
min_distance = float('inf')
for i in range(self.map_size):
distance = sum((input_data[j] - self.weights[i * self.input_size + j]) ** 2 for j in
range(self.input_size))
if distance < min_distance:
min_distance = distance
best_unit = i
return best_unit

def update_weights(self, input_data, best_unit):


for i in range(self.map_size):
distance_to_best = abs(i - best_unit)
influence = math.exp(-(distance_to_best ** 2) / (2 * self.radius ** 2))
for j in range(self.input_size):
self.weights[i * self.input_size + j] += self.learning_rate * influence * (input_data[j] -
self.weights[i * self.input_size + j])

def train(self, training_data, epochs):


for epoch in range(epochs):
for input_data in training_data:
best_unit = self.find_best_matching_unit(input_data)
self.update_weights(input_data, best_unit)
# Adjust learning rate and radius
self.learning_rate *= 0.9
self.radius *= 0.9

def get_weights(self, unit):


return self.weights[unit * self.input_size : (unit + 1) * self.input_size]

def print_weights(self):
for i in range(self.map_size):
unit_weights = self.get_weights(i)
print(f"Unit {i} weights:", *unit_weights)

if name == " main ":


input_size = 3 # Number of input features
map_size = 5 # Number of units in the map
epochs = 1000 # Number of training epochs
learning_rate = 0.1
radius = 2.0
training_data = [
[0.2, 0.8, 0.5],
[0.6, 0.3, 0.8],
[0.5, 0.2, 0.7],
[0.9, 0.1, 0.3],
[0.3, 0.6, 0.9]
]

som = SOM(input_size, map_size, learning_rate, radius)


som.train(training_data, epochs)
print("Trained SOM weights:")
som.print_weights()
Output:
Trained SOM weights:
Unit 0 weights: 0.7085237329898884 0.27419205530276514 0.41478591017885097
Unit 1 weights: 0.3441633431770295 0.5583727913831075 0.5520427656137076
Unit 2 weights: 0.33066745131896597 0.5170507022537023 0.7524046685357049
Unit 3 weights: 0.4664523010267135 0.37236128462622275 0.7361582110948847
Unit 4 weights: 0.5596434645738546 0.2496051794844077 0.757362009214744
Result:

Thus the Python program to Implementing neural network using self-organizing map have
been executed successfully and the output got verified.
EXP NO 10
Implementing k-Means algorithm to cluster a set of data

DATE:

Aim :
To Implementing k-Means algorithm to cluster a set of data

Algorithm :

1. Initialization:
o initializeCentroids: Randomly selects k data points from the input data to
initialize cluster centroids.
2. Distance Calculation:
o distance: Calculates the Euclidean distance between two points.
3. Finding the Nearest Centroid:
o findNearestCentroid: Determines the index of the nearest centroid for a given
data point based on Euclidean distance.
4. Assigning Points to Clusters:
o assignToClusters: Assigns each data point to its nearest cluster based on the
nearest centroid.
5. Updating Centroids:
o updateCentroids: Updates centroids by calculating the mean of points assigned
to each cluster.
6. Convergence Check:
o hasConverged: Checks for convergence by comparing the distance between
old and new centroids.
7. Training (k-Means Algorithm):
o kMeans: Executes the main k-Means algorithm, including centroid
initialization, point assignment, centroid update, and convergence check.
Repeated for a specified number of iterations or until convergence.
8. Printing Clusters:
o printClusters: Outputs the final clusters along with their data points.
9. Main Function:
o Initializes a set of 2D points as input data.
o Creates an instance of the KMeans class with the desired number of clusters
(k).
o Calls the kMeans method to perform the clustering.
o Prints the final clusters using the printClusters method.
Program :

import random
import math

class Point:
def init (self, x, y):
self.x = x
self.y = y

class KMeans:
def init (self, k):
self.k = k
self.centroids = []
self.clusters = []

def initialize_centroids(self, data):


self.centroids = random.sample(data, self.k)

def distance(self, a, b):


return math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2)

def find_nearest_centroid(self, point):


nearest_centroid = min(range(self.k), key=lambda i: self.distance(point, self.centroids[i]))
return nearest_centroid

def assign_to_clusters(self, data):


self.clusters = [[] for _ in range(self.k)]
for point in data:
nearest_centroid = self.find_nearest_centroid(point)
self.clusters[nearest_centroid].append(point)

def update_centroids(self):
for i in range(self.k):
if self.clusters[i]:
sum_x = sum(point.x for point in self.clusters[i])
sum_y = sum(point.y for point in self.clusters[i])
centroid_x = sum_x / len(self.clusters[i])
centroid_y = sum_y / len(self.clusters[i])
self.centroids[i] = Point(centroid_x, centroid_y)

def has_converged(self, old_centroids, tolerance):


return all(self.distance(old_centroids[i], self.centroids[i]) <= tolerance for i in range(self.k))

def print_clusters(self):
for i, cluster in enumerate(self.clusters):
print(f"Cluster {i + 1}: ", end="")
print(*[(point.x, point.y) for point in cluster])

def k_means(self, data, max_iterations=100, tolerance=1e-4):


self.initialize_centroids(data)
for iteration in range(max_iterations):
old_centroids = [Point(centroid.x, centroid.y) for centroid in self.centroids]
self.assign_to_clusters(data)
self.update_centroids()
if self.has_converged(old_centroids, tolerance):
print(f"Converged in {iteration + 1} iterations.")
break

if name == " main ":


data = [
Point(1, 2), Point(2, 3), Point(1, 3), Point(6, 5),
Point(7, 7), Point(8, 6), Point(10, 8), Point(12, 9),
Point(11, 8), Point(15, 13), Point(13, 12), Point(14, 12)
]
k = 3 # Number of clusters
k_means = KMeans(k)
k_means.k_means(data)
print("Final Clusters:")
k_means.print_clusters()
Output:
Converged in 4 iterations.
Final Clusters:
Cluster 1: (15, 13) (13, 12) (14, 12)
Cluster 2: (7, 7) (8, 6) (10, 8) (12, 9) (11, 8)
Cluster 3: (1, 2) (2, 3) (1, 3) (6, 5)
Result:

Thus the Python program to Implementing k-Means algorithm to cluster a set of data have
been executed successfully and the output got verified.
EXP NO 11
Implementing hierarchical clustering algorithm

DATE:

Aim :
To Implementing hierarchical clustering algorithm

Algorithm :

1. Define Point Structure:

 A structure Point is defined to represent a 2D point with x and y coordinates.

2. Define HierarchicalClustering Class:

 A class HierarchicalClustering is defined to encapsulate the clustering process.

3. Main Function:

 The main function demonstrates the usage of the HierarchicalClustering class with a
vector of points.

4. Algorithm Execution in main Function:

 Create a vector of points data.


 Initialize an instance of HierarchicalClustering with the provided points.
 Call the cluster method to perform hierarchical clustering.
 Print the final clusters using the printClusters method.
Program :

import math

class Point:
def init (self, x, y):
self.x = x
self.y = y

def euclidean_distance(p1, p2):


return math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2)

class HierarchicalClustering:
def init (self, points):
self.points = points
self.distances = [[0.0] * len(points) for _ in range(len(points))]
self.initialize_distances()

def initialize_distances(self):
for i in range(len(self.points)):
for j in range(i):
distance = euclidean_distance(self.points[i], self.points[j])
self.distances[i][j] = self.distances[j][i] = distance

def cluster(self):
while len(self.points) > 1:
min_i, min_j = 0, 0
min_distance = float('inf')
for i in range(len(self.points)):
for j in range(i):
if self.distances[i][j] < min_distance:
min_distance = self.distances[i][j]
min_i, min_j = i, j

self.merge_clusters(min_i, min_j)

def merge_clusters(self, i, j):


self.points[i].x = (self.points[i].x + self.points[j].x) / 2
self.points[i].y = (self.points[i].y + self.points[j].y) / 2
del self.points[j]

for k in range(len(self.points)):
if k != i:
self.distances[i][k] = self.distances[k][i] = min(self.distances[i][k],
self.distances[j][k])

del self.distances[j]
for row in self.distances:
del row[j]

def print_clusters(self):
print("Final Clusters:")
for point in self.points:
print(f"({point.x}, {point.y})")
if name == " main ":
data = [Point(1, 2), Point(5, 8), Point(1.5, 1.8), Point(8, 8), Point(1, 0.6), Point(9, 11)]
clustering = HierarchicalClustering(data)
clustering.cluster()
clustering.print_clusters()

Output:

Final Clusters:
(4.4375, 5.375)
Result:

Thus the hierarchical clustering algorithm have been executed successfully and the output
got verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy