def counts_to_idfs(count_matrix, cutoff=0.):
  """Compute IDFs for the vocab given a V x N count matrix.

  Args:
    count_matrix: Sparse matrix from vocab to documents.
    cutoff: Fraction of lowest IDF words to remove (i.e. set IDF=0).

  Returns:
    idfs: Sparse diagonal matrix of IDF values.
  """
  freqs = np.array((count_matrix > 0).astype(int).sum(1)).squeeze()
  idfs = np.log((count_matrix.shape[1] - freqs + 0.5) / (freqs + 0.5))
  idfs[idfs < 0] = 0
  sorted_idx = np.argsort(idfs)
  num_to_remove = int(sorted_idx.shape[0] * cutoff)
  tf.logging.info("Removing %d lowest IDF words.", num_to_remove)
  idx_to_keep = sorted_idx[num_to_remove:]
  idfs = sp.csr_matrix((idfs[idx_to_keep], (idx_to_keep, idx_to_keep)),
                       shape=[idfs.shape[0], idfs.shape[0]])
  return idfs
Compute IDFs for the vocab given a V x N count matrix.

  Args:
    count_matrix: Sparse matrix from vocab to documents.
    cutoff: Fraction of lowest IDF words to remove (i.e. set IDF=0).

  Returns:
    idfs: Sparse diagonal matrix of IDF values.
  
language/labs/drkit/search_utils.py