机器学习-python实现DBSCAN

本文主要介绍的是DBSCAN.是一种著名的密度聚类算法。其原理这里就不详细介绍了,详细请看周志华西瓜书P211-P214。这里主要介绍它的python代码。

import xlrd
import numpy as np
import numpy.random as random
import math 
import copy
import Queue
from random import choice
import matplotlib.pyplot as plt
from collections import Counter 
class DataSet():
    def __init__(self):
        self.DataMat=[]
        self.DataNum=0
        self.FinalSub=[]
        
        print "Creat a Class"
def ReadData(ob):
    data=xlrd.open_workbook("kmeans.xlsx")
    table=data.sheets()[0]
    for i in range(table.nrows-1):
        ob.DataMat.append(table.row_values(i+1))
    ob.DataMat=np.array(ob.DataMat)
    ob.DataNum=np.shape(ob.DataMat)[0]
    print "Your Data Mat is \n",ob.DataMat
    print "Your Data Set Num is",ob.DataNum
def DrawPicture(ob):
    plt.figure('test')
    Signal=['xb','xr','xg','xy','xm','xk','xw','xc']
    for x in range(len(ob.FinalSub)):
        for y in list(ob.FinalSub[x]):
            plt.plot(ob.DataMat[y][0],ob.DataMat[y][1],Signal[x]) 
    plt.savefig("My.jpg")
def CalculataSeed(ob,epsen,MinPts):
    dist=0
    epsenNum=0
    epsenSet=[]
    SeedSet=set()
     
    for i in range(ob.DataNum):
        epsenSet.append(set([-1]))
        for j in range(ob.DataNum):
            if i!=j:
                dist=round(np.linalg.norm(ob.DataMat[i]-ob.DataMat[j]),3)
                if dist<=epsen :
                    epsenNum+=1
                    epsenSet[i].add(j)
            else :
                continue
        if len(epsenSet[i])>=MinPts:
            SeedSet.add(i)
    for i in range(len(epsenSet)):
        print epsenSet[i]
    print "The core seed set is ",SeedSet
    k=0
    NoViSet=set(range(ob.DataNum))
    print "NoviSet is ",NoViSet
    Olde_NoviSet=set()
    Q=Queue.Queue()
    randomStep=0
    deta=set()
    Qnum=0
    while SeedSet != set() :
        Olde_NoviSet=copy.deepcopy(NoViSet) 
        randomStep=choice(list(SeedSet))
        Q.put(randomStep)
        print "random seed is ",randomStep
        NoViSet=NoViSet-set([randomStep])
        while Q.empty() != True:
            Qnum=Q.get()
            if len(epsenSet[Qnum]) >= MinPts :
                deta=epsenSet[Qnum]&NoViSet
                NoViSet=NoViSet-deta
                while deta != set():
                    Q.put(deta.pop())
        k+=1
        ob.FinalSub.append(Olde_NoviSet-NoViSet)
        print "Class is", ob.FinalSub[k-1]
        SeedSet=SeedSet-ob.FinalSub[k-1]   
        print "SeedSet is ",SeedSet		
        print "NoviSet is ",NoViSet
    ob.FinalSub.append(NoViSet)    
def main():
    A=DataSet()
    ReadData(A)
    CalculataSeed(A,0.11,5)
    DrawPicture(A)
main()
数据如下:把它存放在excel表格中。
densitysweet
0.6970.46
0.7740.376
0.6340.264
0.6080.318
0.5560.215
0.4030.237
0.4810.149
0.4370.211
0.6660.091
0.2430.267
0.2450.057
0.3430.099
0.6390.161
0.6570.198
0.360.37
0.5930.042
0.7190.103
0.3590.188
0.3390.241
0.2820.257
0.7480.232
0.7140.346
0.4830.312
0.4780.437
0.5250.369
0.7510.489
0.5320.472
0.4730.376
0.7250.445
0.4460.459

得到结果如图:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值