0% found this document useful (0 votes)
2 views4 pages

DM Practical05

The document contains multiple assignments implementing the K-means clustering algorithm using Python and NumPy. Each assignment generates synthetic data, initializes centroids, and iteratively assigns clusters and updates centroids for 10 iterations. The output for each assignment displays the cluster assignment for each data point.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

DM Practical05

The document contains multiple assignments implementing the K-means clustering algorithm using Python and NumPy. Each assignment generates synthetic data, initializes centroids, and iteratively assigns clusters and updates centroids for 10 iterations. The output for each assignment displays the cluster assignment for each data point.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment No:-05

Assignment Name:- Implement the Clustering using K-means


Roll No:-05
import numpy as np

# Generate synthetic data


np.random.seed(42)
X = np.random.rand(10, 2) # 10 samples, 2 features

# Number of clusters
k=3

# Randomly initialize centroids


centroids = X[np.random.choice(X.shape[0], k, replace=False)]

# K-means algorithm
for _ in range(10): # Number of iterations
# Assign clusters
distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)
labels = np.argmin(distances, axis=1)

# Update centroids
centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])

# Print the results


for i, label in enumerate(labels):
print(f"Point {X[i]} is in cluster {label}")

….OUTPUT….
Assignment No:-05
Assignment Name:- Implement the Clustering using K-means
Roll No:-06
import numpy as np

# Generate synthetic data


np.random.seed(42)
X = np.random.rand(10, 2) # 10 samples, 2 features

# Number of clusters
k=3

# Randomly initialize centroids


centroids = X[np.random.choice(X.shape[0], k, replace=False)]

# K-means algorithm
for _ in range(10): # Number of iterations
# Assign clusters
distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)
labels = np.argmin(distances, axis=1)

# Update centroids
centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])

# Print the results


for i, label in enumerate(labels):
print(f"Point {X[i]} is in cluster {label}")

….OUTPUT….
Assignment No:-05
Assignment Name:- Implement the Clustering using K-means
Roll No:-07
import numpy as np

# Generate synthetic data


np.random.seed(42)
X = np.random.rand(10, 2) # 10 samples, 2 features

# Number of clusters
k=3

# Randomly initialize centroids


centroids = X[np.random.choice(X.shape[0], k, replace=False)]

# K-means algorithm
for _ in range(10): # Number of iterations
# Assign clusters
distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)
labels = np.argmin(distances, axis=1)

# Update centroids
centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])

# Print the results


for i, label in enumerate(labels):
print(f"Point {X[i]} is in cluster {label}")

….OUTPUT….
Assignment No:-05
Assignment Name:- Implement the Clustering using K-means
Roll No:-08
import numpy as np

# Generate synthetic data


np.random.seed(42)
X = np.random.rand(10, 2) # 10 samples, 2 features

# Number of clusters
k=3

# Randomly initialize centroids


centroids = X[np.random.choice(X.shape[0], k, replace=False)]

# K-means algorithm
for _ in range(10): # Number of iterations
# Assign clusters
distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)
labels = np.argmin(distances, axis=1)

# Update centroids
centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])

# Print the results


for i, label in enumerate(labels):
print(f"Point {X[i]} is in cluster {label}")

….OUTPUT….

You might also like