{"record":{"id":"386d3e1bc67dca01","repo":"TheAlgorithms/Python","slug":"df-must-be-0","errorCode":null,"errorMessage":"df must be > 0","messagePattern":"df must be > 0","errorType":"exception","errorClass":"ZeroDivisionError","httpStatus":null,"severity":"error","filePath":"machine_learning/word_frequency_functions.py","lineNumber":117,"sourceCode":"    Traceback (most recent call last):\n     ...\n    ValueError: log10(0) is undefined.\n    >>> inverse_document_frequency(1, 3)\n    0.477\n    >>> inverse_document_frequency(0, 3)\n    Traceback (most recent call last):\n     ...\n    ZeroDivisionError: df must be > 0\n    >>> inverse_document_frequency(0, 3,True)\n    1.477\n    \"\"\"\n    if smoothing:\n        if n == 0:\n            raise ValueError(\"log10(0) is undefined.\")\n        return round(1 + log10(n / (1 + df)), 3)\n\n    if df == 0:\n        raise ZeroDivisionError(\"df must be > 0\")\n    elif n == 0:\n        raise ValueError(\"log10(0) is undefined.\")\n    return round(log10(n / df), 3)\n\n\ndef tf_idf(tf: int, idf: int) -> float:\n    \"\"\"\n    Combine the term frequency\n    and inverse document frequency functions to\n    calculate the originality of a term. This\n    'originality' is calculated by multiplying\n    the term frequency and the inverse document\n    frequency : tf-idf = TF * IDF\n    @params : tf, the term frequency, and idf, the inverse document\n    frequency\n    @examples :\n    >>> tf_idf(2, 0.477)\n    0.954","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/word_frequency_functions.py#L99-L135","documentation":"Raised as ZeroDivisionError by inverse_document_frequency when smoothing is False/omitted and the document frequency df is 0. The unsmoothed formula log10(n/df) divides by df, so a term appearing in no documents would divide by zero; the code raises deliberately with a descriptive message.","triggerScenarios":"Calling inverse_document_frequency(n, 0) without the third argument (smoothing defaults falsy). This happens for terms in the vocabulary that occur in zero documents in the current corpus slice.","commonSituations":"Vocabularies built from a larger corpus then applied to a smaller subset, stop-word filtering that zeroes out some df counts, or new/unseen terms looked up against stale df tables.","solutions":["Pass smoothing=True so the denominator becomes 1+df (sklearn-style Laplace smoothing).","Filter terms with df == 0 out of the vocabulary before computing IDF.","Rebuild the df table on the same corpus you compute IDF over."],"exampleFix":"# before\nidf = inverse_document_frequency(n_docs, df)  # df == 0, no smoothing\n\n# after\nidf = inverse_document_frequency(n_docs, df, True)  # smoothed: 1 + log10(n/(1+df))","handlingStrategy":"validation","validationCode":"if df > 0:\n    idf = inverse_document_frequency(n_docs, df)\nelse:\n    idf = inverse_document_frequency(n_docs, df, True)  # smoothed handles df=0","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer smoothing=True for robustness on sparse vocabularies.","Prune zero-df terms from the vocabulary before scoring."],"tags":["nlp","tf-idf","division-by-zero","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}