July 2017
Intermediate to advanced
382 pages
9h 13m
English
It was called the term--inverse document frequency (tf--idf), and we encountered it in Chapter 4, Representing Data and Engineering Features. If you recall, what tf--idf does is basically weigh the word count by a measure of how often they appear in the entire dataset. A useful side effect of this method is the idf part--the inverse frequency of words. This makes sure that frequent words, such as and, the, and but, carry only a small weight in the classification.
We apply tf--idf to the feature matrix by calling fit_transform on our existing feature matrix X:
In [24]: tfidf = feature_extraction.text.TfidfTransformer()In [25]: X_new = tfidf.fit_transform(X)
Don't forget to split the data:
In [26]: X_train, ...
Read now
Unlock full access