3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

scikit-learnでDBSCAN

Last updated at Posted at 2014-05-18

scikit-learnに実装されてるDBSCANを使ってみた

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import numpy as np
from sklearn import cluster

""" パラメータを指定 """
dbscan = cluster.DBSCAN(eps=float(sys.argv[1]), min_samples=int(sys.argv[2]))

""" データを読み込む """
data_list = []
for line in open(sys.argv[3]):
    x = map(float, line.rstrip().split(' '))
    data_list.append(x)
data = np.array(data_list)

""" クラスタリング """
dbscan.fit(data)

""" 結果を表示 """
labels = dbscan.labels_
for i in range(len(labels)):
    if labels[i] != -1:
        print labels[i], data[i]

##使い方

データは行にサンプル、列に書く属性の値を記述した以下のようなファイルを準備

0 1
8.5 6
2 0
1.5 0
1 1.5
10 5
9 6
8 5.5
9.5 5.6
100 100
-100 -50
1 0

引数にeps, min_samples, data_fileの順で渡して実行

>> python dbscan.py 1.5 3 data
0.0 [ 0.  1.]
1.0 [ 8.5  6. ]
0.0 [ 2.  0.]
0.0 [ 1.5  0. ]
0.0 [ 1.   1.5]
1.0 [ 10.   5.]
1.0 [ 9.  6.]
1.0 [ 8.   5.5]
1.0 [ 9.5  5.6]
0.0 [ 1.  0.]

dbscan.labels_は、各サンプルがどのクラスタに割り当てられたかを表している。
-1の時はどのクラスタにも割り当てられないノイズになったという意味

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?