python-scikit-learn-DBSCAN
2017-12-05 23:23
204 查看
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()))
相关文章推荐
- Python安装scikit-learn包
- scikit-learn: machine learning in Python
- Python的scikit-learn包的学习笔记(1)
- Python scikit-learn 学习笔记—手写数字识别
- scikit-learn: machine learning in Python系列(一)
- scikit-learn使用joblib.dump()持久化模型过程中的问题详解--python
- 机器学习 Python scikit-learn 中文文档与指南
- windows下python3.4的scikit-learn搭建
- 在Windows下将python中scikit learn的模型转化为PMML文件
- python3.6下安装(numpy,scipy,pandas,matplotlib,scikit-learn)
- Python第三方库(模块)"scikit learn"以及其他库的安装
- Python/scikit-learn机器学习库(决策树)
- Python机器学习工具:Scikit-Learn介绍与实践
- Python学习-安装pip和scikit-learn
- 【Python】scikit-learn机器学习(二)——BP神经网络
- Python库scikit-learn的PCA类使用
- python成功配置scikit-learn,含试错历程
- 基于Python和Scikit-Learn的机器学习探索
- Python Scikit-learn ---- SVM算法
- 【Python】scikit-learn教程