import numpy as np
import matplotlib.pyplot as plt from sklearn import cluster
from sklearn.metrics import adjusted_rand_score
from sklearn.datasets.samples_generator import make_blobs def create_data(centers,num=100,std=0.7):
X, labels_true = make_blobs(n_samples=num, centers=centers, cluster_std=std)
return X,labels_true #密度聚类DBSCAN模型
def test_DBSCAN(*data):
X,labels_true=data
clst=cluster.DBSCAN()
predicted_labels=clst.fit_predict(X)
print("ARI:%s"% adjusted_rand_score(labels_true,predicted_labels))
print("Core sample num:%d"%len(clst.core_sample_indices_)) # 用于产生聚类的中心点
centers=[[1,1],[2,2],[1,2],[10,20]]
# 产生用于聚类的数据集
X,labels_true=create_data(centers,1000,0.5)
# 调用 test_DBSCAN 函数
test_DBSCAN(X,labels_true)

def test_DBSCAN_epsilon(*data):
'''
测试 DBSCAN 的聚类结果随 eps 参数的影响
'''
X,labels_true=data
epsilons=np.logspace(-1,1.5)
ARIs=[]
Core_nums=[]
for epsilon in epsilons:
clst=cluster.DBSCAN(eps=epsilon)
predicted_labels=clst.fit_predict(X)
ARIs.append( adjusted_rand_score(labels_true,predicted_labels))
Core_nums.append(len(clst.core_sample_indices_))
## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,2,1)
ax.plot(epsilons,ARIs,marker='+')
ax.set_xscale('log')
ax.set_xlabel(r"$\epsilon$")
ax.set_ylim(0,1)
ax.set_ylabel('ARI') ax=fig.add_subplot(1,2,2)
ax.plot(epsilons,Core_nums,marker='o')
ax.set_xscale('log')
ax.set_xlabel(r"$\epsilon$")
ax.set_ylabel('Core_Nums') fig.suptitle("DBSCAN")
plt.show() # 调用 test_DBSCAN_epsilon 函数
test_DBSCAN_epsilon(X,labels_true)

def test_DBSCAN_min_samples(*data):
'''
测试 DBSCAN 的聚类结果随 min_samples 参数的影响
'''
X,labels_true=data
min_samples=range(1,100)
ARIs=[]
Core_nums=[]
for num in min_samples:
clst=cluster.DBSCAN(min_samples=num)
predicted_labels=clst.fit_predict(X)
ARIs.append( adjusted_rand_score(labels_true,predicted_labels))
Core_nums.append(len(clst.core_sample_indices_)) ## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,2,1)
ax.plot(min_samples,ARIs,marker='+')
ax.set_xlabel( "min_samples")
ax.set_ylim(0,1)
ax.set_ylabel('ARI') ax=fig.add_subplot(1,2,2)
ax.plot(min_samples,Core_nums,marker='o')
ax.set_xlabel( "min_samples")
ax.set_ylabel('Core_Nums') fig.suptitle("DBSCAN")
plt.show() # 调用 test_DBSCAN_min_samples 函数
test_DBSCAN_min_samples(X,labels_true)

吴裕雄 python 机器学习——密度聚类DBSCAN模型的更多相关文章

  1. 吴裕雄 python 机器学习——层次聚类AgglomerativeClustering模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  2. 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  3. 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  4. 吴裕雄 python 机器学习——KNN分类KNeighborsClassifier模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  5. 吴裕雄 python 机器学习——半监督学习LabelSpreading模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...

  6. 吴裕雄 python 机器学习——支持向量机线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  7. 吴裕雄 python 机器学习——混合高斯聚类GMM模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import mixture from sklearn.metrics ...

  8. 吴裕雄 python 机器学习——K均值聚类KMeans模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  9. 吴裕雄 python 机器学习——分类决策树模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...

随机推荐

  1. 【原创】1. MYSQL++简介

    MYSQL++是对于MYSQL C API的C++完全包装. MYSQL++能够至少做如下几件事情 1. 连接数据库 通过TCP连接数据库 通过WINDOWS命名管道连接数据库 UNIX域SOCKET ...

  2. 输入一条url后,发生了什么??

    (1)浏览器解析 (2)查询缓存 (3)DNS查询 顺序如下,若其中一步成功直接进去建立连接部分: -- 浏览器自身DNS -- 操作系统DNS -- 本地hosts文件 -- 像域名服务器发送请求 ...

  3. 使用百度翻译的API接口

    http://api.fanyi.baidu.com/api/trans/product/desktop 这是申请的接口地址,会得到一个APPID和一个钥密 然后下载PHP的对应的代码 有一个PHP文 ...

  4. SpringBoot17 FastJson配置、Druid配置

    1 FastJson配置 1.1 FastJson基础知识 点击前往 1.2 SpringBoot整合FastJson 点击前往 1.2.1 导入FastJson依赖 <!--fastjson- ...

  5. c# 获取客户端ip、mac、机器名、操作系统、浏览器信息

    d using System; using System.Collections.Generic; using System.Linq; using System.Web; using System. ...

  6. 在CentOS6.x下安装Compiz——桌面立方体,特效种种

    很多人貌似认为compiz必须要emerland,但事实上,没这个必要. compiz+gnome,实用,而又华丽,是个不错的选择. compiz需要显卡驱动,一般情况下不成问题(别忘了这是很新的ce ...

  7. while 和do while循环的区别

    int a; scanf_s("%d",&a); while(a>0) { //do something; } while循环先要判断条件是否成立,如果不成立,那么就 ...

  8. hdu 2553 N皇后问题(一维数组详尽解释)

    //一维数组解法(注释详尽)//num皇后可以表示第num列,然后枚举num皇后所在的行//二维数组对角线转换为坐标的关系#include<stdio.h> #include<str ...

  9. [GO]变量内存和变量地址

    package main import "fmt" func main() { //每个变量都有两层含义,变量的内存和变量的地址 fmt.Printf("a = %d\n ...

  10. Requests接口测试(五)

    使用python+requests编写接口测试用例 好了,有了前几章的的基础,写下来我把前面的基础整合一下,来一个实际的接口测试练习吧. 接口测试流程 1.拿到接口的URL地址 2.查看接口是用什么方 ...