【数据挖掘】任务6:DBSCAN聚类

举报
zstar 发表于 2022/08/06 01:43:03 2022/08/06
【摘要】 要求 编程实现DBSCAN对下列数据的聚类 数据获取:https://download.csdn.net/download/qq1198768105/85865302 导库与全局设置 from s...

要求

编程实现DBSCAN对下列数据的聚类

数据获取:https://download.csdn.net/download/qq1198768105/85865302

导库与全局设置

from scipy.io import loadmat
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn import datasets
import pandas as pd

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
plt.rcParams['font.sans-serif'] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False

  
 
  • 1
  • 2

DBSCAN 聚类参数说明

eps:ϵ-邻域的距离阈值,和样本距离超过ϵ的样本点不在ϵ-邻域内,默认值是0.5。

min_samples:形成高密度区域的最小点数。作为核心点的话邻域(即以其为圆心,eps为半径的圆,含圆上的点)中的最小样本数(包括点本身)。

若y=-1,则为异常点

由于DBSCAN生成的类别不确定,因此定义一个函数用来筛选出符合指定类别的最合适的参数。

合适的标准是异常点个数最少

def search_best_parameter(N_clusters, X):
    min_outliners = 999
    best_eps = 0
    best_min_samples = 0
    # 迭代不同的eps值
    for eps in np.arange(0.001, 1, 0.05):
        # 迭代不同的min_samples值
        for min_samples in range(2, 10):
            dbscan = DBSCAN(eps=eps, min_samples=min_samples)
            # 模型拟合
            y = dbscan.fit_predict(X)
            # 统计各参数组合下的聚类个数(-1表示异常点)
            if len(np.argwhere(y == -1)) == 0:
                n_clusters = len(np.unique(y))
            else:
                n_clusters = len(np.unique(y)) - 1
            # 异常点的个数
            outliners = len([i for i in y if i == -1])
            if outliners < min_outliners and n_clusters == N_clusters:
                min_outliners = outliners
                best_eps = eps
                best_min_samples = min_samples
    return best_eps, best_min_samples

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
# 导入数据
colors = ['green', 'red', 'blue']
smile = loadmat('data-密度聚类/smile.mat')

  
 
  • 1
  • 2
  • 3

smile数据

X = smile['smile']
eps, min_samples = search_best_parameter(3, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(smile['smile'])):
    plt.scatter(smile['smile'][i][0], smile['smile'][i][1],
                color=colors[int(smile['smile'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    plt.scatter(smile['smile'][i][0], smile['smile'][i][1], color=colors[y[i]])
    plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述

sizes5数据

# 导入数据
colors = ['blue', 'green', 'red', 'black', 'yellow']
sizes5 = loadmat('data-密度聚类/sizes5.mat')

  
 
  • 1
  • 2
  • 3
X = sizes5['sizes5']
eps, min_samples = search_best_parameter(4, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(sizes5['sizes5'])):
    plt.scatter(sizes5['sizes5'][i][0], sizes5['sizes5']
                [i][1], color=colors[int(sizes5['sizes5'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    if y[i] != -1:
        plt.scatter(sizes5['sizes5'][i][0], sizes5['sizes5']
                    [i][1], color=colors[y[i]])
        plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

square1数据

# 导入数据
colors = ['green', 'red', 'blue', 'black']
square1 = loadmat('data-密度聚类/square1.mat')

  
 
  • 1
  • 2
  • 3
X = square1['square1']
eps, min_samples = search_best_parameter(4, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(square1['square1'])):
    plt.scatter(square1['square1'][i][0], square1['square1']
                [i][1], color=colors[int(square1['square1'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    plt.scatter(square1['square1'][i][0], square1['square1']
                [i][1], color=colors[y[i]])
    plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

square4数据

# 导入数据
colors = ['blue', 'green', 'red', 'black',
          'yellow', 'brown', 'orange', 'purple']
square4 = loadmat('data-密度聚类/square4.mat')

  
 
  • 1
  • 2
  • 3
  • 4
X = square4['b']
eps, min_samples = search_best_parameter(5, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(square4['b'])):
    plt.scatter(square4['b'][i][0], square4['b']
                [i][1], color=colors[int(square4['b'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    plt.scatter(square4['b'][i][0], square4['b']
                [i][1], color=colors[y[i]])
    plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

spiral数据

# 导入数据
colors = ['green', 'red']
spiral = loadmat('data-密度聚类/spiral.mat')

  
 
  • 1
  • 2
  • 3
X = spiral['spiral']
eps, min_samples = search_best_parameter(2, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(spiral['spiral'])):
    plt.scatter(spiral['spiral'][i][0], spiral['spiral']
                [i][1], color=colors[int(spiral['spiral'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    plt.scatter(spiral['spiral'][i][0], spiral['spiral']
                [i][1], color=colors[y[i]])
    plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

moon数据

# 导入数据
colors = ['green', 'red']
moon = loadmat('data-密度聚类/moon.mat')

  
 
  • 1
  • 2
  • 3
X = moon['a']
eps, min_samples = search_best_parameter(2, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(moon['a'])):
    plt.scatter(moon['a'][i][0], moon['a']
                [i][1], color=colors[int(moon['a'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    plt.scatter(moon['a'][i][0], moon['a']
                [i][1], color=colors[y[i]])
    plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

long数据

# 导入数据
colors = ['green', 'red']
long = loadmat('data-密度聚类/long.mat')

  
 
  • 1
  • 2
  • 3
X = long['long1']
eps, min_samples = search_best_parameter(2, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(long['long1'])):
    plt.scatter(long['long1'][i][0], long['long1']
                [i][1], color=colors[int(long['long1'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    plt.scatter(long['long1'][i][0], long['long1']
                [i][1], color=colors[y[i]])
    plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

2d4c数据

# 导入数据
colors = ['green', 'red', 'blue', 'black']
d4c = loadmat('data-密度聚类/2d4c.mat')

  
 
  • 1
  • 2
  • 3
X = d4c['a']
eps, min_samples = search_best_parameter(4, X)
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
y = dbscan.fit_predict(X)

  
 
  • 1
  • 2
  • 3
  • 4
# 聚类结果可视化
plt.figure(figsize=(20, 15))
plt.subplot(2, 2, 1)
for i in range(len(d4c['a'])):
    plt.scatter(d4c['a'][i][0], d4c['a']
                [i][1], color=colors[int(d4c['a'][i][2])])
    plt.title("原始数据")
plt.subplot(2, 2, 2)
for i in range(len(y)):
    plt.scatter(d4c['a'][i][0], d4c['a']
                [i][1], color=colors[y[i]])
    plt.title("聚类后数据")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

总结

上述实验证明了DBSCAN聚类方法比较依赖数据点位置上的关联度,对于smile、spiral等分布的数据聚类效果较好。

文章来源: zstar.blog.csdn.net,作者:zstar-_,版权归原作者所有,如需转载,请联系作者。

原文链接:zstar.blog.csdn.net/article/details/125575620

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。