KMeans多分类python代码
时间: 2023-11-12 20:09:31 浏览: 81
Kmeans与Kmeans++算法Python代码实现
5星 · 资源好评率100%
以下是KMeans多分类Python代码的示例:
```python
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)
kmeans = KMeans(n_clusters=4)
y_kmeans = kmeans.fit_predict(X)
import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5);
```
请注意,这是一个通用的示例,您需要根据您的数据和需要进行相应的调整。
阅读全文