#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Mar 31 09:41:54 2025 """ import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans import scipy.io # Step 1: Get data data = scipy.io.loadmat('HWMar26data.mat') X=data.get('A') # Step 2: Apply K-Means clustering kmeans = KMeans(n_clusters=3, random_state=42, n_init=10) kmeans.fit(X) # Get cluster centers and labels centers = kmeans.cluster_centers_ labels = kmeans.labels_ # Step 3: Visualize the results plt.figure(figsize=(8, 6)) plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', alpha=0.6, edgecolors='k') plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='X', s=200, label='Centroids') plt.title('K-Means Clustering Example') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.legend() plt.show()