Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the off-by-one bug in the TFIDF model. #2392

Merged
merged 3 commits into from
Apr 28, 2019
Merged

Fix the off-by-one bug in the TFIDF model. #2392

merged 3 commits into from
Apr 28, 2019

Conversation

AMR-KELEG
Copy link
Contributor

Fixes #2375.
Use len to compute the number of features.
Since the ids are zero-indexed, Using max causes an off-by-one bug.

Fixes #2375.
Use len to compute the number of features.
Since the ids are zero-indexed, Using max causes an off-by-one bug.
Copy link
Collaborator

@mpenkov mpenkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution. Looks good. Left you some comments.

As a side note, is it possible to unit test this thing somehow? I notice the only visible output is a logging statement, so tests may be tricky/useless, but just wanted to make sure.

gensim/models/tfidfmodel.py Outdated Show resolved Hide resolved
@@ -390,7 +390,7 @@ def initialize(self, corpus):
self.num_nnz = numnnz
self.dfs = dfs
# and finally compute the idf weights
n_features = max(dfs) if dfs else 0
n_features = len(dfs) if dfs else 0
logger.info(
"calculating IDF weights for %i documents and %i features (%i matrix non-zeros)",
self.num_docs, n_features, self.num_nnz
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.num_docs, n_features, self.num_nnz
self.num_docs, len(dfs), self.num_nnz

@mpenkov mpenkov self-assigned this Feb 23, 2019
gensim/models/tfidfmodel.py Outdated Show resolved Hide resolved
@piskvorky
Copy link
Owner

Looks good, thanks @AMR-KELEG ! Can you check whether the same issues is present somewhere else, too? (e.g. from that same refactoring)

logger.info(
"calculating IDF weights for %i documents and %i features (%i matrix non-zeros)",
self.num_docs, n_features, self.num_nnz
self.num_docs, 1 + max([-1] + list(dfs.keys())), self.num_nnz
Copy link
Owner

@piskvorky piskvorky Feb 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.num_docs, 1 + max([-1] + list(dfs.keys())), self.num_nnz
self.num_docs, max(dfs.keys()) + 1 if dfs else 0, self.num_nnz

@AMR-KELEG
Copy link
Contributor Author

Looks good, thanks @AMR-KELEG ! Can you check whether the same issues is present somewhere else, too? (e.g. from that same refactoring)

I have checked the commit and I don't think this edit was introduced in any other files.
The only edit that might look similar is this one (af01bc7#diff-327b58fcebe80ddff670af4d52bdde1cR232)
However, I believe this edit won't case errors.

I have also searched the whole project for this line n_features = max(dfs) if dfs else 0 and it was only present in the tfidf model script.

@mpenkov mpenkov merged commit 460dc1c into piskvorky:develop Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Off-by-one counting error in TFIDF
3 participants