首页 技术 正文
技术 2022年11月14日
0 收藏 375 点赞 2,279 浏览 2056 个字
 # -*- coding: utf-8 -*-
"""
Created on Tue Sep 25 10:48:34 2018 @author: zhen
""" import numpy as np
import matplotlib.pyplot as plt
import sklearn.datasets as ds
import matplotlib.colors
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler def expand(a, b):
d = (b - a) * 0.1
return a-d, b+d if __name__ == "__main__":
N = 1000
centers = [[1, 2], [-1, -1], [1, -1], [-1, 1]]
data, y = ds.make_blobs(N, n_features=2, centers=centers, cluster_std=[0.5, 0.25, 0.7, 0.5], random_state=0)
# 归一化数据
data = StandardScaler().fit_transform(data)
# 数据的参数
params = ((0.2, 5), (0.2, 10), (0.2, 15), (0.3, 5), (0.3, 10), (0.3, 15)) # 设置中文样式
matplotlib.rcParams['font.sans-serif'] = [u'SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
# 设置颜色
cm = matplotlib.colors.ListedColormap(list('rgbm'))
plt.figure(figsize=(12, 8), facecolor='w')
plt.suptitle(u'DBSCAN聚类', fontsize=20) for i in range(6):
eps, min_samples = params[i]
# 创建密度聚类模型
model = DBSCAN(eps=eps, min_samples=min_samples)
# 训练模型
model.fit(data)
y_hat = model.labels_ core_indices = np.zeros_like(y_hat, dtype=bool)
core_indices[model.core_sample_indices_] = True y_unique = np.unique(y_hat)
n_clusters = y_unique.size - (1 if -1 in y_hat else 0)
# print(y_unique, '聚类簇的个数:', n_clusters) plt.subplot(2, 3, i+1)
clrs = plt.cm.Spectral(np.linspace(0, 0.8, y_unique.size))
# print(clrs) x1_min, x2_min = np.min(data, axis=0)
x1_max, x2_max = np.max(data, axis=0)
x1_min, x1_max = expand(x1_min, x1_max)
x2_min, x2_max = expand(x2_min, x2_max) for k, clr in zip(y_unique, clrs):
cur = (y_hat == k)
if k == -1:
plt.scatter(data[cur, 0], data[cur, 1], s=20, c='k')
# 设置散点图数据
plt.scatter(data[cur, 0], data[cur, 1], s=20, cmap=cm, edgecolors='k')
plt.scatter(data[cur & core_indices][:, 0], data[cur & core_indices][:, 1],
s=20, cmap=cm, marker='o', edgecolors='k')
# 设置x,y轴
plt.xlim((x1_min, x1_max))
plt.ylim((x2_min, x2_max))
plt.grid(True)
plt.title(u'epsilon = %.1f m = %d, 聚类数目:%d' % (eps, min_samples, n_clusters), fontsize=16)
plt.tight_layout()
plt.subplots_adjust(top=0.9)
plt.show()

结果:

Python之密度聚类

Python之密度聚类

总结:

  1.在epsilon(半径)相同的情况下,m(数量)越大,划分的聚类数目就可能越多,异常的数据就会划分的越多。在m(数量)相同的情况下,epsilon(半径)越大,划分的聚类数目就可能越少,异常的数据就会划分的越少。因此,epsilon和m是相互牵制的,合适的epsilon和m有利于更好的聚类,减少欠拟合或过拟合的情况。

  2.和KMeans聚类相比,DBSCAN密度聚类更擅长聚不规则形状的数据,因此在数据不是接近圆形的方式分布的情况下,建议使用密度聚类!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298