Skip to content

Commit

Permalink
Bugfix: Full2sparse clipped to use abs value (#811)
Browse files Browse the repository at this point in the history
* Fix full2sparse_clipped to return he `topn` elements of the greatest magnitude (abs). Previously it was returing the greatest elements(without abs).

* ab
  • Loading branch information
tmylk committed Aug 4, 2016
1 parent 272c8f5 commit 35bad8b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gensim/matutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def full2sparse_clipped(vec, topn, eps=1e-9):
return []
vec = numpy.asarray(vec, dtype=float)
nnz = numpy.nonzero(abs(vec) > eps)[0]
biggest = nnz.take(argsort(vec.take(nnz), topn, reverse=True))
biggest = nnz.take(argsort(abs(vec).take(nnz), topn, reverse=True))
return list(zip(biggest, vec.take(biggest)))


Expand Down
7 changes: 7 additions & 0 deletions gensim/test/test_similarities.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ def testNumBest(self):
for num_best in [None, 0, 1, 9, 1000]:
self.testFull(num_best=num_best)

def test_full2sparse_clipped(self):

vec = [0.8, 0.2, 0.0, 0.0, -0.1, -0.15]
expected = [(0, 0.80000000000000004), (1, 0.20000000000000001), (5, -0.14999999999999999)]
self.assertTrue(matutils.full2sparse_clipped(vec, topn=3), expected)



def testChunking(self):
if self.cls == similarities.Similarity:
Expand Down

0 comments on commit 35bad8b

Please sign in to comment.