Vertopal.com_ML LAB 9
Vertopal.com_ML LAB 9
import numpy as np
import matplotlib.pyplot as plt
while True:
# Assign labels based on the closest center
distances_to_x1 = np.linalg.norm(data - x1, axis=1)
distances_to_x2 = np.linalg.norm(data - x2, axis=1)
labels = np.where(distances_to_x1 < distances_to_x2, 1, 2)
# Plot results
plt.scatter(data[:, 0], data[:, 1], c=labels, cmap='bwr', alpha=0.6)
plt.scatter([x1[0], x2[0]], [x1[1], x2[1]], c='black', marker='x',
s=200, label='Centers')
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Clustering using Distance-based Labeling')
plt.show()