用鸢尾花数据做基于网格CLIQUE算法聚类算法代码
时间: 2023-07-04 13:18:48 浏览: 182
Clique-master_网格聚类_clique_
3星 · 编辑精心推荐
以下是基于网格CLIQUE算法的鸢尾花数据聚类算法代码:
```python
import numpy as np
from sklearn.datasets import load_iris
# 加载鸢尾花数据集
iris = load_iris()
data = iris.data
# 定义距离函数
def distance(x, y):
return np.sqrt(np.sum((x - y) ** 2))
# 定义网格大小
grid_size = 0.2
# 计算网格数量
n_grid = int(np.ceil((np.max(data, axis=0) - np.min(data, axis=0)) / grid_size))
# 初始化网格
grid = [[] for i in range(n_grid[0] * n_grid[1])]
# 将数据点划分到网格中
for i, x in enumerate(data):
grid_index = int((x[0] - np.min(data, axis=0)[0]) // grid_size) * n_grid[1] + int((x[1] - np.min(data, axis=0)[1]) // grid_size)
grid[grid_index].append(i)
# 定义CLIQUE算法
def clique(cluster, min_pts):
# 定义核心对象集合
core = set()
for i in cluster:
count = 0
for j in cluster:
if i != j and distance(data[i], data[j]) <= eps:
count += 1
if count >= min_pts:
core.add(i)
# 定义聚类结果
clusters = []
visited = set()
# 遍历核心对象
for i in core:
if i not in visited:
visited.add(i)
cluster = set()
cluster.add(i)
neighbors = set(j for j in core if distance(data[i], data[j]) <= eps)
while neighbors:
j = neighbors.pop()
if j not in visited:
visited.add(j)
cluster.add(j)
new_neighbors = set(j for j in core if distance(data[i], data[j]) <= eps)
neighbors.update(new_neighbors - visited)
clusters.append(cluster)
return clusters
# 定义聚类结果
clusters = []
eps = 0.6
min_pts = 5
# 对每个网格中的数据点进行聚类
for i in range(n_grid[0] * n_grid[1]):
if grid[i]:
cluster = clique(grid[i], min_pts)
for c in cluster:
clusters.append([grid[i][j] for j in c])
# 输出聚类结果
for i, c in enumerate(clusters):
print("Cluster %d:" % (i + 1))
print([iris.target_names[iris.target[j]] for j in c])
```
该代码实现了基于网格CLIQUE算法的鸢尾花数据聚类,将数据点划分到网格中,并对每个网格中的数据点进行聚类。输出了聚类结果,每个聚类中包含的鸢尾花类型。
阅读全文