from sklearn.cluster import KMeans
import numpy as np
# Create sample data
# Let's assume we have two-dimensional data points
X = np.array(
[
[150],
[48],
[702],
[850],
[560],
]
)
# Define initial centroids (for 3 clusters here)
initial_centroids = np.array([[360], [850]])
# Create KMeans instance with specified initial centroids
kmeans = KMeans(n_clusters=2, init=initial_centroids, n_init=1)
kmeans.fit(X)
# After fitting, you can check the labels and final centroids
print("Cluster Labels:", kmeans.labels_)
print("Centroids:", kmeans.cluster_centers_)
Click Run or press shift + ENTER to run code