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

K-Means 10

The document outlines a Python script that performs K-Means clustering on synthetic data generated with make_blobs. It scales the data, reduces its dimensionality using PCA, and visualizes the clustering results in a 2D plot, highlighting the cluster centers. Additionally, it prints the inertia and the number of iterations run by the K-Means algorithm.
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)
6 views2 pages

K-Means 10

The document outlines a Python script that performs K-Means clustering on synthetic data generated with make_blobs. It scales the data, reduces its dimensionality using PCA, and visualizes the clustering results in a 2D plot, highlighting the cluster centers. Additionally, it prints the inertia and the number of iterations run by the K-Means algorithm.
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

In [4]: import numpy as np

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

n_samples = 1000
n_features = 10
n_clusters = 4
X, y_true = make_blobs(n_samples=n_samples, centers=n_clusters, n_features=n_features, cluster_std=1.5, random_state=42)

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

pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

kmeans = KMeans(n_clusters=n_clusters, random_state=42)


y_kmeans = kmeans.fit_predict(X_scaled)

plt.figure(figsize=(8, 6))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y_kmeans, cmap='viridis', s=50, alpha=0.7)
centers = kmeans.cluster_centers_
centers_pca = pca.transform(centers)
plt.scatter(centers_pca[:, 0], centers_pca[:, 1], c='red', marker='x', s=200, label='Cluster Centers')
plt.title('K-Means Clustering Visualization (Reduced to 2D via PCA)')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.legend()
plt.grid()
plt.show()

print(f"Inertia (Sum of squared distances to cluster centers): {kmeans.inertia_}")


print(f"Number of iterations run: {kmeans.n_iter_}")
Inertia (Sum of squared distances to cluster centers): 1253.2034940028748
Number of iterations run: 2

In [ ]:

You might also like