def apply_idf_weighting(doc_term_matrix, smooth_idf=True):
    """
    Apply inverse document frequency (idf) weighting to a term-frequency (tf)
    weighted document-term matrix, optionally smoothing idf values.

    Args:
        doc_term_matrix (:class:`scipy.sparse.csr_matrix <scipy.sparse.csr_matrix`):
            M X N matrix, where M is the # of docs and N is the # of unique terms
        smooth_idf (bool, optional): if True, add 1 to all document frequencies,
            equivalent to adding a single document to the corpus containing every
            unique term

    Returns:
        :class:`scipy.sparse.csr_matrix <scipy.sparse.csr_matrix>`: sparse matrix
            of shape (# docs, # unique terms), where value (i, j) is the tfidf
            weight of term j in doc i
    """
    dfs = get_doc_freqs(doc_term_matrix, normalized=False)
    n_docs = doc_term_matrix.shape[0]
    if smooth_idf is True:
        n_docs += 1
        dfs += 1
    idfs = np.log(dfs / n_docs) + 1.0
    # return doc_term_matrix.multiply(idfs)
    return doc_term_matrix.dot(sp.diags(idfs, 0))  # same as above, but 100x faster?!
dataset/ETHPy150Open chartbeat-labs/textacy/textacy/representations/vsm.py/apply_idf_weighting/swapped operands of `/`