Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite0b091e

Browse files
author
Martijn van Beers
committed
Do the dot-product on the sparse matrix
When you have a large corpus, first making the matrix dense takes a lot ofmemory. Doing the dot product first and then expanding the result ismore memory-efficient and still gives the same result
1 parent3a95063 commite0b091e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

‎Chapter-6/document_similarity.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
defcompute_cosine_similarity(doc_features,corpus_features,
4040
top_n=3):
4141
# get document vectors
42-
doc_features=doc_features.toarray()[0]
43-
corpus_features=corpus_features.toarray()
42+
doc_features=doc_features[0]
4443
# compute similarities
4544
similarity=np.dot(doc_features,
4645
corpus_features.T)
46+
similarity=similarity.toarray()[0]
4747
# get docs with highest similarity scores
4848
top_docs=similarity.argsort()[::-1][:top_n]
4949
top_docs_with_score= [(index,round(similarity[index],3))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp