単純なテストデータセットで階層的クラスタリングを行うことを目的とした単純なスクリプトを作成しました。
関数fclusterdataは、データを2つのクラスターにクラスター化する候補であることがわかりました。データセットとしきい値の2つの必須の呼び出しパラメーターが必要です。問題は、予想される2つのクラスターを生成するしきい値が見つからなかったことです。
誰かが私が間違っていることを教えてくれたら嬉しいです。また、クラスタリングに適した他のアプローチを誰かが指摘できれば幸いです(事前にクラスターの数を指定することは避けたいと思います)。
これが私のコードです:
import time
import scipy.cluster.hierarchy as hcluster
import numpy.random as random
import numpy
import pylab
pylab.ion()
data = random.randn(2,200)
data[:100,:100] += 10
for i in range(5,15):
thresh = i/10.
clusters = hcluster.fclusterdata(numpy.transpose(data), thresh)
pylab.scatter(*data[:,:], c=clusters)
pylab.axis("equal")
title = "threshold: %f, number of clusters: %d" % (thresh, len(set(clusters)))
print title
pylab.title(title)
pylab.draw()
time.sleep(0.5)
pylab.clf()
出力は次のとおりです。
threshold: 0.500000, number of clusters: 129
threshold: 0.600000, number of clusters: 129
threshold: 0.700000, number of clusters: 129
threshold: 0.800000, number of clusters: 75
threshold: 0.900000, number of clusters: 75
threshold: 1.000000, number of clusters: 73
threshold: 1.100000, number of clusters: 58
threshold: 1.200000, number of clusters: 1
threshold: 1.300000, number of clusters: 1
threshold: 1.400000, number of clusters: 1