用python写kmeans++
时间: 2023-08-31 22:10:12 浏览: 145
Sure, here's a basic example of how to implement k-means clustering in Python:
```python
from sklearn.cluster import KMeans
import numpy as np
# Set up your data
X = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]])
# Create k-means object and fit to data
kmeans = KMeans(n_clusters=2)
kmeans.fit(X)
# Get cluster assignments and centroids
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
# Print out cluster assignments and centroids
print("Cluster Assignments:", labels)
print("Cluster Centroids:", centroids)
```
This code uses the `KMeans` algorithm from scikit-learn, a popular machine learning library in Python, to cluster some sample data into two clusters. Hope this helps!
阅读全文