July 2017
Intermediate to advanced
382 pages
9h 13m
English
Scikit-learn offers a number of options when it comes to encoding text features, which we discussed in Chapter 4, Representing Data and Engineering Features. One of the simplest methods of encoding text data, we recall, is by word count: For each phrase, you count the number of occurrences of each word within it. In scikit-learn, this is easily done using CountVectorizer:
In [10]: from sklearn import feature_extraction... counts = feature_extraction.text.CountVectorizer()... X = counts.fit_transform(data['text'].values)... X.shapeOut[10]: (52076, 643270)
The result is a giant matrix, which tells us that we harvested a total of 52,076 emails that collectively contain 643,270 different words. However, scikit-learn ...
Read now
Unlock full access