Calculates tf * idf on the vector feature weights (in-place).
(vectors=[], base=2.71828)
| 701 | cos = cosine_similarity |
| 702 | |
| 703 | def tf_idf(vectors=[], base=2.71828): # Euler's number |
| 704 | """ Calculates tf * idf on the vector feature weights (in-place). |
| 705 | """ |
| 706 | df = {} |
| 707 | for v in vectors: |
| 708 | for f in v: |
| 709 | if v[f] != 0: |
| 710 | df[f] = df[f] + 1 if f in df else 1.0 |
| 711 | n = len(vectors) |
| 712 | s = dict.__setitem__ |
| 713 | for v in vectors: |
| 714 | for f in v: # Modified in-place. |
| 715 | s(v, f, v[f] * (log(n / df[f], base))) |
| 716 | return vectors |
| 717 | |
| 718 | tfidf = tf_idf |
| 719 |