0% found this document useful (0 votes)
9 views2 pages

ML 7

Uploaded by

Mr Addy
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)
9 views2 pages

ML 7

Uploaded by

Mr Addy
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/ 2

# Import necessary libraries

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# Load the Iris dataset (you can replace this with any other dataset)
data = load_iris()
X = data.data # Features
y = data.target # True labels (for reference, not used in K-Means)

# Standardize the features (optional but helps K-Means perform better)


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

# Apply K-Means clustering


kmeans = KMeans(n_clusters=3, random_state=42) # 3 clusters (as Iris has 3 classes)
kmeans.fit(X_scaled)

# Get the cluster labels and centroids


cluster_labels = kmeans.labels_
centroids = kmeans.cluster_centers_

# Plot the clusters using two features for visualization (e.g., the first two features)
plt.figure(figsize=(8, 6))
scatter = plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=cluster_labels, cmap='viridis',
edgecolor='k', s=50)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=200, alpha=0.75, label='Centroids') # Plot
centroids
plt.title('K-Means Clustering on Iris Dataset')
plt.xlabel('Feature 1 (Standardized)')
plt.ylabel('Feature 2 (Standardized)')
plt.legend()
plt.colorbar(scatter, label='Cluster Label')
plt.show()

# Evaluate clustering (optional, using known true labels for comparison)


from sklearn.metrics import accuracy_score, confusion_matrix
print("Confusion Matrix:")
print(confusion_matrix(y, cluster_labels)) # Compare clusters with true labels

You might also like