DBSCAN算法 python代码

以下是一个简单的Python实现DBSCAN算法的示例代码:

import numpy as np

# 计算两个点之间的欧氏距离
def euclidean_distance(x1, x2):
    return np.sqrt(np.sum((x1 - x2) ** 2))

# DBSCAN算法实现
def dbscan(X, eps, min_samples):
    labels = [0] * len(X)
    cluster_index = 0
    for i in range(len(X)):
        if not (labels[i] == 0):
            continue
        neighbor_points = region_query(X, i, eps)
        if len(neighbor_points) < min_samples:
            labels[i] = -1
        else:
            cluster_index += 1
            grow_cluster(X, labels, i, neighbor_points, cluster_index, eps, min_samples)
    return labels

# 寻找以该样本点为圆心, 半径为eps内的所有点
def region_query(X, index, eps):
    neighbors = []
    for i in range(len(X)):
        if euclidean_distance(X[i], X[index]) < eps:
            neighbors.append(i)
    return neighbors

# 对聚类进行递归生长
def grow_cluster(X, labels, index, neighbor_points, cluster_index, eps, min_samples):
    labels[index] = cluster_index
    i = 0
    while i < len(neighbor_points):
        point = neighbor_points[i]
        if labels[point] == -1:
            labels[point] = cluster_index
        elif labels[point] == 0:
            labels[point] = cluster_index
            new_neighbor_points = region_query(X, point, eps)
            if len(new_neighbor_points) >= min_samples:
                neighbor_points = neighbor_points + new_neighbor_points
        i += 1

# 调用示例
X = np.array([[1, 1], [2, 1], [1, 2], [2, 2], [3, 3], [8, 8], [9, 8], [8, 9], [9, 9]])
labels = dbscan(X, 1, 3)
print(labels)

该代码段使用numpy库创建一个简单的数据集,并打印出DBSCAN算法的聚类结果。您可以根据需要进行修改以适应自己的数据集。

你可能感兴趣的:(python语言,算法,python,开发语言)