2016-07-25 27 views
2

行列の行間のコサイン距離を計算しようとしています。行列matrを作成してリストからデータを取り込み、それを再構成しています分析目的:行列の行間のコサイン距離の計算

s = [] 

for i in range(len(a)): 
    for j in range(len(b_list)): 
     s.append(a[i].count(b_list[j])) 

matr = np.array(s) 
d = matr.reshape((22, 254)) 

Dの出力は、私はのようになめらか与える:

array([[0, 0, 0, ..., 0, 0, 0], 
     [2, 0, 0, ..., 1, 0, 0], 
     [2, 0, 0, ..., 0, 0, 0], 
     ..., 
     [0, 0, 0, ..., 0, 0, 0], 
     [0, 0, 0, ..., 0, 0, 0], 
     [1, 0, 0, ..., 0, 0, 0]]) 

は、それから私は、内の他のすべての他に、最初の行からのコサインを計算するscipy.spatial.distance.cosineパッケージを使用したいですd行列。 どうすればいいですか?それはループのためにいくつかあるべきですか?行列操作や配列操作の経験はあまりありません。

それでは、どのように私はそれを毎回起動しないという建設中(D [1]、D [2]、など)第二引数のためのforループを使用することができます。

from scipy.spatial.distance import cosine 
x=cosine (d[0], d[6]) 
+0

このパッケージには「ペアワイズ」機能や2つの機能はありませんか? – hpaulj

+0

私は恐らくそうではありません - それは入力として2つの1次元配列を使用する余弦を使用します:cosine(u、v)、ここでu、v-1-D配列。 – HalfPintBoy

+0

あなたはnumpyを使用していますか? –

答えて

3

を参照してください。

dists = [] 
for row in matr: 
    dists.append(scipy.spatial.distance.cosine(matr[0,:], row)) 
7

あなたが求める」と述べました最初の行からd行目までの余弦までのコサイン[sic]。それはあなたがすべてのを計算したいことが判明した場合

In [31]: from scipy.spatial.distance import cdist 

In [32]: matr = np.random.randint(0, 3, size=(6, 8)) 

In [33]: matr 
Out[33]: 
array([[1, 2, 0, 1, 0, 0, 0, 1], 
     [0, 0, 2, 2, 1, 0, 1, 1], 
     [2, 0, 2, 1, 1, 2, 0, 2], 
     [2, 2, 2, 2, 0, 0, 1, 2], 
     [0, 2, 0, 2, 1, 0, 0, 0], 
     [0, 0, 0, 1, 2, 2, 2, 2]]) 

In [34]: cdist(matr[0:1], matr[1:], metric='cosine') 
Out[34]: array([[ 0.65811827, 0.5545646 , 0.1752139 , 0.24407105, 0.72499045]]) 

:私が正しく理解していれば、あなたは第二引数として最初の引数と、残りの行として最初の行を渡し、scipy.spatial.distance.cdistであることを行うことができます対の距離はmatrで、scipy.spatial.distance.pdistを使用できます。 pdistによって返される最初の5つの値はcdistを使用して、上記戻さ同じ値であることたとえば

In [35]: from scipy.spatial.distance import pdist 

In [36]: pdist(matr, metric='cosine') 
Out[36]: 
array([ 0.65811827, 0.5545646 , 0.1752139 , 0.24407105, 0.72499045, 
     0.36039785, 0.27625314, 0.49748109, 0.41498206, 0.2799177 , 
     0.76429774, 0.37117185, 0.41808563, 0.5765951 , 0.67661917]) 

注意。 pdistの戻り値のさらなる説明については

、あなただけscipy.spatial.distance.cosineでループするための単純なを使用することができますHow does condensed distance matrix work? (pdist)

+0

答えは間違っているようですね。私はすべての '自己'比較のために距離が '0'であると期待しています。 –

+0

@TasosPapastylianou 'pdist'によって計算された結果には、 '自己'比較は含まれていません。私の答えの終わりに私が提供したリンクの説明を参照してください。 –

1

ここでは、手で簡単に計算するかもしれない方法は次のとおりです。

from numpy import array as a 
from numpy.random import random_integers as randi 
from numpy.linalg.linalg import norm 
from numpy import set_printoptions 

M = randi(10, size=a([5,5])); # create demo matrix 

# dot products of rows against themselves 
DotProducts = M.dot(M.T);  

# kronecker product of row norms 
NormKronecker = a([norm(M, axis=1)]) * a([norm(M, axis=1)]).T; 

CosineSimilarity = DotProducts/NormKronecker 
CosineDistance = 1 - CosineSimilarity 

set_printoptions(precision=2, suppress=True) 
print CosineDistance 

出力:

[[-0. 0.15 0.1 0.11 0.22] 
[ 0.15 0. 0.15 0.13 0.06] 
[ 0.1 0.15 0. 0.15 0.14] 
[ 0.11 0.13 0.15 0. 0.18] 
[ 0.22 0.06 0.14 0.18 -0. ]] 

この行列は、 「3行目と2行目(または同様に2行目と3行目)のコサイン距離は0.15」と解釈されます。

関連する問題