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….