In [2]:
import numpy as np
import scipy
import pandas
import pyclust

import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

In [4]:
df = pandas.read_table('data_k5.txt', sep=' ')

df.head(3)


Out[4]:
x1 x2 label
0 -4.4791 3.3937 2
1 7.8984 -5.8965 3
2 -9.8639 -7.7490 0

In [5]:
bkm = pyclust.BisectKMeans(n_clusters=5)

bkm.fit(df.iloc[:,0:2].values)

print(bkm.labels_)


[5 4 8 4 5 5 4 6 4 4 6 4 5 7 5 5 4 4 4 7 4 5 8 6 6 8 5 4 8 8 7 5 5 7 8 7 6
 7 5 6 5 6 7 5 7 5 6 4 7 8 4 6 7 4 7 8 7 5 8 6 4 5 6 8 5 7 8 6 4 7 4 8 6 4
 6 7 6 4 4 6 8 6 7 7 6 8 8 5 5 7 7 7 6 6 8 6 4 4 4 8 4 5 8 8 8 5 8 5 6 5 5
 8 8 6 7 8 6 7 6 8 7 8 5 8 7 6 5 8 4 4 8 6 4 6 7 6 6 4 7 5 7 6 7 5 4 5 5 8
 7 8 7 5 5 8 7 7 7 4 8 6 8 6 8 7 6 5 5 8 4 5 4 5 5 4 5 8 6 5 8 8 6 8 5 7 6
 6 7 7 4 5 5 5 5 5 5 7 6 5 6 5]

In [6]:
bkm.tree_.show(line_type='ascii')


0
|-- 1
|   |-- 7
|   +-- 8
+-- 2
    |-- 3
    |   |-- 5
    |   +-- 6
    +-- 4


In [13]:
bkm.centers_


Out[13]:
array([[  6.93363823e-310,   2.11466974e-316],
       [  0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000]])

In [9]:
bkm.tree_.to_json()


Out[9]:
'{"0": {"children": [{"1": {"children": [7, 8]}}, {"2": {"children": [{"3": {"children": [5, 6]}}, 4]}}]}}'

In [12]:
node = bkm.tree_.get_node("1")

node.data()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-b32ddb70d1be> in <module>()
      1 node = bkm.tree_.get_node("1")
      2 
----> 3 node.data()

AttributeError: 'NoneType' object has no attribute 'data'

In [ ]: