python进行kmeans聚类分析
时间: 2023-11-23 11:55:31 浏览: 111
以下是使用Python进行K-means聚类分析的步骤和示例代码:
1.导入必要的库和数据集
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
# 生成随机数据
X, y = make_blobs(n_samples=1000, centers=4, random_state=42)
```
2.确定最佳的聚类数量
```python
# 通过肘部法则确定最佳的聚类数量
wcss = []
for i in range(1, 11):
kmeans = KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10, random_state=0)
kmeans.fit(X)
wcss.append(kmeans.inertia_)
plt.plot(range(1, 11), wcss)
plt.title('Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
```
3.训练K-means模型并进行预测
```python
# 训练K-means模型并进行预测
kmeans = KMeans(n_clusters=4, init='k-means++', max_iter=300, n_init=10, random_state=0)
pred_y = kmeans.fit_predict(X)
```
4.可视化聚类结果
```python
# 可视化聚类结果
plt.scatter(X[:,0], X[:,1])
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red')
plt.title('Clustered Data')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
```
阅读全文