用鸢尾花数据做基于网格CLIQUE算法聚类算法的代码
时间: 2023-07-04 14:18:40 浏览: 115
好的,下面是用鸢尾花数据做基于网格CLIQUE算法聚类算法的 Python 代码:
```python
import numpy as np
from sklearn import datasets
# 鸢尾花数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 网格CLIQUE算法
class GridCLIQUE():
def __init__(self, grid_size=0.5, density_threshold=5):
self.grid_size = grid_size
self.density_threshold = density_threshold
def fit(self, X):
n_samples, n_features = X.shape
# 网格划分
grid_x = int(np.ceil((np.max(X[:, 0]) - np.min(X[:, 0])) / self.grid_size))
grid_y = int(np.ceil((np.max(X[:, 1]) - np.min(X[:, 1])) / self.grid_size))
grids = np.zeros((grid_x, grid_y), dtype=np.int)
for i in range(n_samples):
x, y = X[i, :2]
x_idx = int(np.floor((x - np.min(X[:, 0])) / self.grid_size))
y_idx = int(np.floor((y - np.min(X[:, 1])) / self.grid_size))
grids[x_idx, y_idx] += 1
# 密度阈值
density = np.zeros((grid_x, grid_y), dtype=np.int)
for i in range(grid_x):
for j in range(grid_y):
x_min = max(0, i - 1)
x_max = min(grid_x - 1, i + 1)
y_min = max(0, j - 1)
y_max = min(grid_y - 1, j + 1)
neighbor_grids = grids[x_min:x_max+1, y_min:y_max+1]
density[i, j] = np.sum(neighbor_grids) - grids[i, j]
threshold = np.percentile(density, 100 - self.density_threshold)
# 核心网格单元
core_grids = np.argwhere(density >= threshold)
n_core_grids = core_grids.shape[0]
core_labels = np.zeros(n_core_grids, dtype=np.int)
for i in range(n_core_grids):
x, y = core_grids[i]
if core_labels[i] == 0:
core_labels[i] = i + 1
x_min = max(0, x - 1)
x_max = min(grid_x - 1, x + 1)
y_min = max(0, y - 1)
y_max = min(grid_y - 1, y + 1)
neighbor_grids = grids[x_min:x_max+1, y_min:y_max+1]
neighbor_density = density[x_min:x_max+1, y_min:y_max+1]
for j in range(n_core_grids):
if i != j and core_labels[j] == 0:
x2, y2 = core_grids[j]
if x_min <= x2 <= x_max and y_min <= y2 <= y_max:
if neighbor_density[x2-x_min, y2-y_min] >= threshold:
core_labels[j] = core_labels[i]
# 簇合并
cluster_labels = np.zeros((grid_x, grid_y), dtype=np.int)
for i in range(n_core_grids):
x, y = core_grids[i]
label = core_labels[i]
if cluster_labels[x, y] == 0:
cluster_labels[x, y] = label
x_min = max(0, x - 1)
x_max = min(grid_x - 1, x + 1)
y_min = max(0, y - 1)
y_max = min(grid_y - 1, y + 1)
neighbor_labels = cluster_labels[x_min:x_max+1, y_min:y_max+1]
for j in range(n_core_grids):
if i != j and core_labels[j] == label:
x2, y2 = core_grids[j]
if x_min <= x2 <= x_max and y_min <= y2 <= y_max:
neighbor_labels[core_grids[j, 0]-x_min, core_grids[j, 1]-y_min] = label
# 分配剩余网格单元
for i in range(grid_x):
for j in range(grid_y):
if cluster_labels[i, j] == 0:
x_min = max(0, i - 1)
x_max = min(grid_x - 1, i + 1)
y_min = max(0, j - 1)
y_max = min(grid_y - 1, j + 1)
neighbor_labels = cluster_labels[x_min:x_max+1, y_min:y_max+1]
unique_labels = np.unique(neighbor_labels)
if unique_labels.shape[0] == 1:
cluster_labels[i, j] = unique_labels[0]
else:
cluster_labels[i, j] = unique_labels[np.argmax(np.bincount(neighbor_labels.flatten()))]
self.labels_ = cluster_labels.reshape(-1)
return self
# 聚类
grid_clique = GridCLIQUE(grid_size=0.5, density_threshold=5)
labels = grid_clique.fit(X[:, :2]).labels_
# 可视化
import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.show()
```
其中,`GridCLIQUE` 类实现了网格CLIQUE算法,`grid_size` 和 `density_threshold` 分别为网格大小和密度阈值,`fit` 方法用于聚类。
代码中只用了鸢尾花数据的前两个特征(即花萼长度和花萼宽度),并将聚类结果可视化。
阅读全文