Token Frequency Distribution¶
A method for visualizing the frequency of tokens within and across corpora is frequency distribution. A frequency distribution tells us the frequency of each vocabulary item in the text. In general, it could count any kind of observable event. It is a distribution because it tells us how the total number of word tokens in the text are distributed across the vocabulary items.
FreqDistVisualizer does not perform any normalization or vectorization, and it expects text that has already been count vectorized.
We first instantiate a
FreqDistVisualizer object, and then call
fit() on that object with the count vectorized documents and the features (i.e. the words from the corpus), which computes the frequency distribution. The visualizer then plots a bar chart of the top 50 most frequent terms in the corpus, with the terms listed along the x-axis and frequency counts depicted at y-axis values. As with other Yellowbrick visualizers, when the user invokes
poof(), the finalized visualization is shown. Note that in this plot and in the subsequent one, we can orient our plot vertically by passing in
orient='v' on instantiation (the plot will orient horizontally by default):
from sklearn.feature_extraction.text import CountVectorizer from yellowbrick.text import FreqDistVisualizer from yellowbrick.datasets import load_hobbies # Load the text data corpus = load_hobbies() vectorizer = CountVectorizer() docs = vectorizer.fit_transform(corpus.data) features = vectorizer.get_feature_names() visualizer = FreqDistVisualizer(features=features, orient='v') visualizer.fit(docs) visualizer.poof()
It is interesting to compare the results of the
FreqDistVisualizer before and after stopwords have been removed from the corpus:
It is also interesting to explore the differences in tokens across a corpus. The hobbies corpus that comes with Yellowbrick has already been categorized (try
corpus.target), so let’s visually compare the differences in the frequency distributions for two of the categories: “cooking” and “gaming”.
Here is the plot for the cooking corpus (oriented horizontally this time):
And for the gaming corpus (again oriented horizontally):
Implementations of frequency distributions for text visualization
FrequencyVisualizer(features, ax=None, n=50, orient='h', color=None, **kwargs)¶
A frequency distribution tells us the frequency of each vocabulary item in the text. In general, it could count any kind of observable event. It is a distribution because it tells us how the total number of word tokens in the text are distributed across the vocabulary items.
- features : list, default: None
The list of feature names from the vectorizer, ordered by index. E.g. a lexicon that specifies the unique vocabulary of the corpus. This can be typically fetched using the
get_feature_names()method of the transformer in Scikit-Learn.
- ax : matplotlib axes, default: None
The axes to plot the figure on.
- n: integer, default: 50
Top N tokens to be plotted.
- orient : ‘h’ or ‘v’, default: ‘h’
Specifies a horizontal or vertical bar chart.
- color : list or tuple of colors
Specify color for bars
- kwargs : dict
Pass any additional keyword arguments to the super class.
- These parameters can be influenced later on in the visualization
- process, but can and should be set as early as possible.
Called from the fit method, this method gets all the words from the corpus and their corresponding frequency counts.
- X : ndarray or masked ndarray
Pass in the matrix of vectorized documents, can be masked in order to sum the word frequencies for only a subset of documents.
- counts : array
A vector containing the counts of all words in X (columns)
Called from the fit method, this method creates the canvas and draws the distribution plot on it.
- kwargs: generic keyword arguments.
The finalize method executes any subclass-specific axes finalization steps. The user calls poof & poof calls finalize.
- kwargs: generic keyword arguments.
The fit method is the primary drawing input for the frequency distribution visualization. It requires vectorized lists of documents and a list of features, which are the actual words from the original corpus (needed to label the x-axis ticks).
- X : ndarray or DataFrame of shape n x m
A matrix of n instances with m features representing the corpus of frequency vectorized documents.
- y : ndarray or DataFrame of shape n
Labels for the documents for conditional frequency distribution.
- .. note:: Text documents must be vectorized before ``fit()``.