Machine Learning 简明教程

Machine Learning - DBSCAN Clustering

DBSCAN 聚类算法的工作原理如下−

The DBSCAN Clustering algorithm works as follows −

  1. Randomly select a data point that has not been visited.

  2. If the data point has at least minPts neighbors within distance eps, create a new cluster and add the data point and its neighbors to the cluster.

  3. If the data point does not have at least minPts neighbors within distance eps, mark the data point as noise and continue to the next data point.

  4. Repeat steps 1-3 until all data points have been visited.

Implementation in Python

我们可以使用 scikit-learn 库在 Python 中实现 DBSCAN 算法。以下是执行此操作的步骤−

We can implement the DBSCAN algorithm in Python using the scikit-learn library. Here are the steps to do so −

Load the dataset

第一步是加载数据集。我们将使用 scikit-learn 库中的 make_moons 函数来生成一个有两个新月的玩具数据集。

The first step is to load the dataset. We will use the make_moons function from the scikitlearn library to generate a toy dataset with two moons.

from sklearn.datasets import make_moons
X, y = make_moons(n_samples=200, noise=0.05, random_state=0)

Perform DBSCAN clustering

下一步是在数据集上执行 DBSCAN 聚类。我们将使用来自 scikit-learn 库的 DBSCAN 类。我们将 minPts 参数设置为 5,将“eps”参数设置为 0.2。

The next step is to perform DBSCAN clustering on the dataset. We will use the DBSCAN class from the scikit-learn library. We will set the minPts parameter to 5 and the "eps" parameter to 0.2.

from sklearn.cluster import DBSCAN
clustering = DBSCAN(eps=0.2, min_samples=5)
clustering.fit(X)

Visualize the results

最后一步是对聚类结果进行可视化。我们将使用 Matplotlib 库根据聚类分配创建数据集的散点图。

The final step is to visualize the results of the clustering. We will use the Matplotlib library to create a scatter plot of the dataset colored by the cluster assignments.

import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1], c=clustering.labels_, cmap='rainbow')
plt.show()

Example

以下是 DBSCAN 在 Python 中的完整实现:

Here is the complete implementation of DBSCAN clustering in Python −

from sklearn.datasets import make_moons
X, y = make_moons(n_samples=200, noise=0.05, random_state=0)
from sklearn.cluster import DBSCAN

clustering = DBSCAN(eps=0.2, min_samples=5)
clustering.fit(X)

import matplotlib.pyplot as plt
plt.figure(figsize=(7.5, 3.5))
plt.scatter(X[:, 0], X[:, 1], c=clustering.labels_, cmap='rainbow')

plt.show()

生成的散点图应显示两个不同的簇,每个簇都对应于数据集中的一轮明月。噪声数据点应以黑色显示。

The resulting scatter plot should show two distinct clusters, each corresponding to one of the moons in the dataset. The noise data points should be colored black.

dbscan clustering

Advantages of DBSCAN

以下是使用 DBSCAN 聚类的优点:

Following are the advantages of using DBSCAN clustering −

  1. DBSCAN can handle clusters of arbitrary shape, unlike k-means, which assumes that clusters are spherical.

  2. It does not require prior knowledge of the number of clusters in the dataset, unlike k-means.

  3. It can detect outliers, which are points that do not belong to any cluster. This is because DBSCAN defines clusters as dense regions of points, and points that are far from any dense region are considered outliers.

  4. It is relatively insensitive to the initial choice of parameters, such as the epsilon and min_samples parameters, unlike k-means.

  5. It is scalable to large datasets, as it only needs to compute pairwise distances between neighboring points, rather than all pairs of points.

Disadvantages of DBSCAN

以下是使用 DBSCAN 聚类的缺点:

Following are the disadvantages of using DBSCAN clustering −

  1. It can be sensitive to the choice of the epsilon and min_samples parameters. If these parameters are not chosen carefully, DBSCAN may fail to identify clusters or merge them incorrectly.

  2. It may not work well on datasets with varying densities, as it assumes that all clusters have the same density.

  3. It may produce different results for different runs on the same dataset, due to the non-deterministic nature of the algorithm.

  4. It may be computationally expensive for high-dimensional datasets, as the distance computations become more expensive as the number of dimensions increases.

  5. It may not work well on datasets with noise or outliers if the density of the noise or outliers is too high. In such cases, the noise or outliers may be wrongly assigned to clusters.