def tf_idf(vectors=[], base=2.71828):
 df = {}
 for v in vectors:
  for f in v:
   if (v[f] != 0):
    df[f] = ((df[f] + 1) if (f in df) else 1.0)
 n = len(vectors)
 s = dict.__setitem__
 for v in vectors:
  for f in v:
   s(v, f, (v[f] * log((n / df[f]), base)))
 return vectors

def tf_idf ( vectors = [ ] , base = 2.71828 ) : df = { } for v in vectors : for f in v : if ( v [ f ] != 0 ) : df [ f ] = ( ( df [ f ] + 1 ) if ( f in df ) else 1.0 ) n = len ( vectors ) s = dict . __setitem__ for v in vectors : for f in v : s ( v , f , ( v [ f ] * log ( ( n / df [ f ] ) , base ) ) ) return vectors
calculates tf * idf on the vector feature weights .