Skip to content

added idf-smooth #2174

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

Merged
merged 3 commits into from
Sep 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions machine_learning/word_frequency_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ def document_frequency(term: str, corpus: str) -> int:
return (len([doc for doc in docs if term in doc]), len(docs))


def inverse_document_frequency(df: int, N: int) -> float:
def inverse_document_frequency(df: int, N: int, smoothing=False) -> float:
"""
Return an integer denoting the importance
of a word. This measure of importance is
calculated by log10(N/df), where N is the
number of documents and df is
the Document Frequency.
@params : df, the Document Frequency, and N,
the number of documents in the corpus.
@returns : log10(N/df)
@params : df, the Document Frequency, N,
the number of documents in the corpus and
smoothing, if True return the idf-smooth
@returns : log10(N/df) or 1+log10(N/1+df)
@examples :
>>> inverse_document_frequency(3, 0)
Traceback (most recent call last):
Expand All @@ -104,7 +105,14 @@ def inverse_document_frequency(df: int, N: int) -> float:
Traceback (most recent call last):
...
ZeroDivisionError: df must be > 0
>>> inverse_document_frequency(0, 3,True)
1.477
"""
if smoothing:
if N == 0:
raise ValueError("log10(0) is undefined.")
return round(1 + log10(N / (1 + df)), 3)

if df == 0:
raise ZeroDivisionError("df must be > 0")
elif N == 0:
Expand Down