Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import numpy as np # Sample data points data _ points = { ' P 1 ' : [ 5 . 1 , 3 .

import numpy as np
# Sample data points
data_points ={
'P1': [5.1,3.5],
'P2': [4.9,3.0],
'P3': [4.7,3.2],
'P4': [4.6,3.1],
'P5': [5.0,3.6],
'P6': [5.4,3.9]}
# Calculate Euclidean distance between two points
def euclidean_distance(point1, point2):
return np.sqrt(np.sum((np.array(point1)- np.array(point2))**2))
# Create initial distance matrix
labels = list(data_points.keys())
num_points = len(labels)
distance_matrix = np.zeros((num_points, num_points))
for i in range(num_points):
for j in range(i +1, num_points):
distance_matrix[i, j]= euclidean_distance(data_points[labels[i]], data_points[labels[j]])
distance_matrix[j, i]= distance_matrix[i, j] # Distance matrix is symmetric
# Display initial distance matrix
print("Initial Distance Matrix:")
print(distance_matrix)
# Single linkage clustering and updating distance matrix
clusters ={label: [label] for label in labels}
while len(clusters)>1:
# Find the minimum distance in the current matrix
min_distance = np.min(distance_matrix[np.nonzero(distance_matrix)])
# Find indices of the minimum distance
indices = np.where(distance_matrix == min_distance)
idx1, idx2= indices[0][0], indices[1][0]
# Merge the clusters by updating the distance matrix
new_cluster =(labels[idx1], labels[idx2])
clusters[new_cluster]= clusters.pop(labels[idx1])+ clusters.pop(labels[idx2])
# Update the distance matrix for merged clusters
for i in range(num_points):
if labels[i] in clusters[new_cluster]:
for j in range(num_points):
if labels[j] not in clusters[new_cluster]:
min_dist = min(distance_matrix[i, j], distance_matrix[idx1, j], distance_matrix[idx2, j])
distance_matrix[i, j]= min_dist
distance_matrix[j, i]= min_dist
# Display updated distance matrix
print(f"
Clusters Formed: {list(clusters.keys())}")
print("Updated Distance Matrix:")
print(distance_matrix)
# Print the final cluster
print(f"
Final Cluster: {list(clusters.keys())}")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

Respond to the questions in Consumer Insight 101.

Answered: 1 week ago

Question

=+ What is Pats minimax choice?

Answered: 1 week ago