python-scikit-learn-DBSCAN

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

mac2id={}
onlinetimes=[]
f=open('c:/pythonpractice/python-data/netDuration.txt')
lines=f.readlines()
#skip the first row
#storage the data in dict ,key is mac and the  valus is duration
for line in range(1,len(lines)):
    mac=lines[line].split('\t')[2]
    onlinetime=int(lines[line].split('\t')[6])
    starttime=int(lines[line].split('\t')[4].split(' ')[1].split(':')[0])
    if mac not in mac2id:
        mac2id[mac]=len(onlinetimes) #use the index to refer to the onlintimes
        onlinetimes.append((starttime,onlinetime))
    else:
        onlinetimes[mac2id[mac]]=[(starttime,onlinetime)]
real_X=np.array(onlinetimes).reshape((-1,2))  # change the shape of onlietimes  to 2 colums
X=real_X[:,0:1] # chose the  hour of  starttime

db=ske.DBSCAN(eps=0.01,min_samples=20).fit(X)
labels=db.labels_
print('Labels:')
print(labels) #print the labels


raito=len(labels[labels[:]==-1])/len(labels)  # caculate the ratio
print('noise raito:',format(raito,'.2%'))

n_clusters_=len(set(labels))-(1 if -1 in labels else 0)

print('estimated sumber of clusters: %d' % n_clusters_)
print('silhousette coofficient: %0.3f' % metrics.silhouette_score(X,labels))

#print the cluster data and the label
for i in range(n_clusters_):
    print('Cluster',i,':')
    print(list(X[labels==i].flatten()))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值