已知数据点坐标,以及数据点距离矩阵,请帮我生成一段近邻传播聚类代码,并用图片显示聚类结果
时间: 2023-05-28 08:01:02 浏览: 148
对于近邻传播聚类算法,我们需要先做以下准备工作:
1.导入必要的库和数据
```python
import numpy as np
from sklearn.cluster import AffinityPropagation
import matplotlib.pyplot as plt
# 数据点坐标
data_points = np.array([
[2.3, 3.2],
[1.8, 2.3],
[3.3, 4.4],
[0.7, 1.2],
[1.8, 1.7],
[2.9, 3.8],
[2.7, 2.7],
[3.8, 3.3],
[0.9, 1.8],
[1.1, 1.1]
])
# 数据点距离矩阵
dist_mat = np.array([
[0.0, 0.7, 0.8, 1.2, 1.1, 0.6, 0.5, 0.4, 1.4, 1.4],
[0.7, 0.0, 1.3, 1.4, 1.4, 1.1, 0.9, 1.1, 1.3, 1.3],
[0.8, 1.3, 0.0, 1.5, 1.4, 0.5, 1.1, 0.8, 1.7, 1.6],
[1.2, 1.4, 1.5, 0.0, 0.3, 1.9, 1.9, 1.8, 0.5, 0.8],
[1.1, 1.4, 1.4, 0.3, 0.0, 1.7, 1.7, 1.6, 0.7, 0.9],
[0.6, 1.1, 0.5, 1.9, 1.7, 0.0, 0.8, 0.7, 2.1, 2.0],
[0.5, 0.9, 1.1, 1.9, 1.7, 0.8, 0.0, 0.9, 1.7, 1.6],
[0.4, 1.1, 0.8, 1.8, 1.6, 0.7, 0.9, 0.0, 1.8, 1.7],
[1.4, 1.3, 1.7, 0.5, 0.7, 2.1, 1.7, 1.8, 0.0, 0.3],
[1.4, 1.3, 1.6, 0.8, 0.9, 2.0, 1.6, 1.7, 0.3, 0.0]
])
```
2.使用AffinityPropagation算法进行聚类
```python
af = AffinityPropagation(affinity='precomputed')
labels = af.fit_predict(dist_mat)
```
3.可视化聚类结果
```python
cluster_centers_indices = af.cluster_centers_indices_
n_clusters_ = len(cluster_centers_indices)
colors = plt.cm.Spectral(np.linspace(0, 1, n_clusters_))
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = data_points[cluster_centers_indices[k]]
plt.plot(data_points[class_members, 0], data_points[class_members, 1], 'w', markerfacecolor=col, marker='.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markersize=10)
plt.title('Affinity Propagation Clustering')
plt.show()
```
完整代码如下:
```python
import numpy as np
from sklearn.cluster import AffinityPropagation
import matplotlib.pyplot as plt
# 数据点坐标
data_points = np.array([
[2.3, 3.2],
[1.8, 2.3],
[3.3, 4.4],
[0.7, 1.2],
[1.8, 1.7],
[2.9, 3.8],
[2.7, 2.7],
[3.8, 3.3],
[0.9, 1.8],
[1.1, 1.1]
])
# 数据点距离矩阵
dist_mat = np.array([
[0.0, 0.7, 0.8, 1.2, 1.1, 0.6, 0.5, 0.4, 1.4, 1.4],
[0.7, 0.0, 1.3, 1.4, 1.4, 1.1, 0.9, 1.1, 1.3, 1.3],
[0.8, 1.3, 0.0, 1.5, 1.4, 0.5, 1.1, 0.8, 1.7, 1.6],
[1.2, 1.4, 1.5, 0.0, 0.3, 1.9, 1.9, 1.8, 0.5, 0.8],
[1.1, 1.4, 1.4, 0.3, 0.0, 1.7, 1.7, 1.6, 0.7, 0.9],
[0.6, 1.1, 0.5, 1.9, 1.7, 0.0, 0.8, 0.7, 2.1, 2.0],
[0.5, 0.9, 1.1, 1.9, 1.7, 0.8, 0.0, 0.9, 1.7, 1.6],
[0.4, 1.1, 0.8, 1.8, 1.6, 0.7, 0.9, 0.0, 1.8, 1.7],
[1.4, 1.3, 1.7, 0.5, 0.7, 2.1, 1.7, 1.8, 0.0, 0.3],
[1.4, 1.3, 1.6, 0.8, 0.9, 2.0, 1.6, 1.7, 0.3, 0.0]
])
# 使用AffinityPropagation算法进行聚类
af = AffinityPropagation(affinity='precomputed')
labels = af.fit_predict(dist_mat)
# 可视化聚类结果
cluster_centers_indices = af.cluster_centers_indices_
n_clusters_ = len(cluster_centers_indices)
colors = plt.cm.Spectral(np.linspace(0, 1, n_clusters_))
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = data_points[cluster_centers_indices[k]]
plt.plot(data_points[class_members, 0], data_points[class_members, 1], 'w', markerfacecolor=col, marker='.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markersize=10)
plt.title('Affinity Propagation Clustering')
plt.show()
```
运行以上代码后,会生成一个可视化的聚类结果,如下图所示:
![affinity_propagation_clustering_result](https://cdn.luogu.com.cn/upload/image_hosting/ed07jn3q.png)
阅读全文