2014-12-06 248 views
1

林試圖解決一個聚類problem..I具有由CountVectorizer產生()功能。本TFIDF加權向量的列表的數據類型:numpy的矩陣尺寸-TFIDF矢量

<1000x5369 sparse matrix of type '<type 'numpy.float64'>' 
with 42110 stored elements in Compressed Sparse Row format> 

我具有下列尺寸的「質心」載體:

<1x5369 sparse matrix of type '<type 'numpy.float64'>' 
with 57 stored elements in Compressed Sparse Row format> 

當嘗試測量所述質心的餘弦相似性,並通過下面的行的代碼在我tfidf_vec_list其他載體:

for centroid in centroids: 
sim_scores=[cosine_similarity(vector,centroid) for vector in tfidf_vec_list] 

其中所述相似性函數是:

def cosine_similarity(vector1,vector2): 
    score=1-scipy.spatial.distance.cosine(vector1,vector2) 
    return score 

我得到錯誤:

Traceback (most recent call last): 
    File "<pyshell#25>", line 1, in <module> 
    sim_scores=[cosine_similarity(vector,centroid) for vector in tfidf_vec_list] 
    File "/home/ashwin/Desktop/Python-2.7.9/programs/test_2.py", line 28, in    cosine_similarity 
    score=1-scipy.spatial.distance.cosine(vector1,vector2) 
    File "/usr/lib/python2.7/dist-packages/scipy/spatial/distance.py", line 287, in cosine 
    dist = 1.0 - np.dot(u, v)/(norm(u) * norm(v)) 
    File "/usr/lib/python2.7/dist-packages/scipy/sparse/base.py", line 302, in __mul__ 
    raise ValueError(**'dimension mismatch'**) 

我已經試過各種包括矩陣中的每個向量轉換成一個陣列和一個list.But我得到同樣的錯誤!

+0

看起來像矢量和質心有不同的尺寸,所以檢查這兩個向量長度 – 2014-12-06 22:45:25

+0

@ Michael Plakhov不 - 他們有相同的尺寸:1 * 5369這是我無法理解的 – 2014-12-06 23:49:49

+0

這種向量中的什麼樣的元素?我是指典型的大小? – 2014-12-07 09:38:20

回答

3

scipy.spatial.distance.cosine似乎不支持稀疏矩陣輸入。具體而言,np.linalg.norm(sparse_vector)失敗(請參閱Get norm of numpy sparse matrix rows)。

如果你路過它們之前都轉換輸入向量(實際上他們在這裏以矩陣形式行向量),以密集的版本,它工作得很好:

>>> xs 
<1x4 sparse matrix of type '<class 'numpy.int64'>' 
     with 3 stored elements in Compressed Sparse Row format> 
>>> ys 
<1x4 sparse matrix of type '<class 'numpy.int64'>' 
     with 3 stored elements in Compressed Sparse Row format> 
>>> cosine(xs, ys) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python3.4/site-packages/scipy/spatial/distance.py", line 296, in cosine 
    dist = 1.0 - np.dot(u, v)/(norm(u) * norm(v)) 
    File "/usr/lib/python3.4/site-packages/scipy/sparse/base.py", line 308, in __mul__ 
    raise ValueError('dimension mismatch') 
ValueError: dimension mismatch 
>>> cosine(xs.todense(), ys.todense()) 
-2.2204460492503131e-16 

這應該是罰款,只有個別5369元矢量(而不是整個矩陣)。

+0

@ HapeMask ..我錯過了這個問題。我做了同樣的事情。當矩陣轉換爲稠密矩陣時,它工作得很好。在處理距離度量和稀疏矩陣時,需要注意一點。 – 2014-12-09 19:42:16