2017-06-01 31 views
0

私はsiameseネットワークから得たfeat_left, feat_rightという2つの点を持っており、これらの点をx,yの座標にプロットしました。ここでenter image description here重心と精度を計算する

Pythonスクリプト

import json 
import matplotlib.pyplot as plt 
import numpy as np 



data = json.load(open('predictions-mnist.txt')) 

n=len(data['outputs'].items()) 
label_list = np.array(range(n)) 
feat_left = np.random.random((n,2)) 


count=1 

for key,val in data['outputs'].items(): 
    feat = data['outputs'][key]['feat_left'] 
    feat_left[count-1] = feat 
    key = key.split("/") 
    key = int(key[6]) 
    label_list[count - 1] = key 
    count = count + 1 


f = plt.figure(figsize=(16,9)) 

c = ['#ff0000', '#ffff00', '#00ff00', '#00ffff', '#0000ff', 
    '#ff00ff', '#990000', '#999900', '#009900', '#009999'] 

for i in range(10): 
    plt.plot(feat_left[label_list==i,0].flatten(), feat_left[label_list==i,1].flatten(), '.', c=c[i]) 
plt.legend(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) 
plt.grid() 
plt.show() 

は今、私はcentriodし、各クラスタの純度を計算したいです。

+1

クラスタの精度をどのように定義しますか? – Shai

+0

モジュール「sklearn.cluster」によって提供されるk-means(k = 10)を使用したり、別の[クラスタリング方法](http://scikit-learn.org/stable/modules/clustering.html)で一見できます' – Nuageux

+0

私はこの記事[クラスタリングの評価](https://nlp.stanford.edu/IR-book/html/htmledition/evaluation-of-clustering-1.html)に従っています@Shai – cpwah

答えて

2

は、重心は、単にmeanです:

sqerr = np.zeros((10,), dtype='f4') 
for i in xrange(10): 
    sqerr[i] = np.sum((feat_left[label_list==i, :2]-centroids[i,:])**2) 

コンピューティングpurity

def compute_cluster_purity(gt_labels, pred_labels): 
    """ 
    Compute purity of predicted labels (pred_labels), given 
    the ground-truth labels (gt_labels). 

    Assuming gt_labels and pred_labels are both lists of int of length n 
    """ 
    n = len(gt_labels) # number of elements 
    assert len(pred_labels) == n 
    purity = 0 
    for l in set(pred_labels): 
    # for predicted label l, what are the gt_labels of this cluster? 
    gt = [gt_labels[i] for i, il in enumerate(pred_labels) if il==l] 
    # most frequent gt label in this cluster: 
    mfgt = max(set(gt), key=gt.count) 
    purity += gt.count(mfgt) # count intersection between most frequent ground truth and this cluster 
    return float(purity)/n 

精度については
centorids = np.zeros((10,2), dtype='f4') 
for i in xrange(10): 
    centroids[i,:] = np.mean(feat_left[label_list==i, :2], axis=0) 

は、あなたが重心からの平均二乗誤差(距離)を計算することができます

seleの詳細については、this answerを参照してください。クラスタ内で最も頻繁にラベルを付ける。

+0

を評価するkmeansクラスタリングを使用してセントリドを計算しました。純度メトリックの計算にもっと関心があります。 @Shai – cpwah

+0

@cpwahこれは**別の質問です。純粋さのために、真実のラベルと 'kmeans 'の割り当て(あなたの例では' label_list'のようなもの) – Shai

+0

ええ、私は自分の質問を修正しました。グラウンドトゥルーラベリングは、あなたが指定した 'label_list'にあります。 @Shai – cpwah

関連する問題