Python Tutorial: Let's build a word cloud!

Build a word cloud in Python using the WordCloud package and Matplotlib. Import the libraries, pass your text to WordCloud().generate(), customize colors and fonts, then display it with matplotlib.pyplot.imshow(). The tutorial covers stop-words, scaling, and other parameters you can tweak.
Chapters
Welcome back. Chances are high that you have seen a Word cloud before. A word cloud is an image composed of words with different sizes and colors. They can be especially useful in sentiment analysis. Have you ever wondered how such an image is generated? In this video we will learn how to create a Word cloud in Python. Word Clouds, also called tag clouds, are are used across different contexts. In the most common type of Word clouds and the one we will be using in this course, the size of the text corresponds to the frequency of the word. The more frequent the word is, the bigger and bolder it will appear on the Word cloud. Remember how we found the longest movie review? This word cloud is generated using only the words in one of the longest reviews. Which movie do you guess the review is talking about? I think we can agree it is about the Titanic. Why are Word Clouds so popular? First of all, they can reveal the essential we saw in our Word Cloud the word Titanic really popped out. Second, unless told otherwise, they will plot all the words in a text and a quick scan of the image can provide an overall sense of the text. Last but not least, they are easy to understand and quite fun. However, they have their drawbacks. Sometimes they tend to work less well. All the words plotted on the cloud might seem unrelated and it could be difficult to draw conclusions based on a crowded word cloud. Secondly, if the text we work with is large, a word cloud might require quite a lot of pre processing steps before it appears sensible and uncluttered. Now let's create a Word cloud in Python. To do that we can use the Word Cloud function from the Word Cloud package. We will have to import Matplotlib Pyplot as well, which will allow Word Cloud to plot on its base. Let's define a string called two Cities which captures the first sentence of Dickens Tale of Two Cities Note how the text carries many emotionally charged words. After we have imported the package, we build the cloud by calling the Word Cloud function followed by the generate method which takes this argument the text, in our case the 2cts string. The word Cloud function has many arguments. We will not cover all of them here, but if you want to learn what they are, just type cloud? Word cloud in the shell. You can change things such as the background color, the size and font of the words, their scaling and others. One interesting argument you can specify is the stop words which will remove words such as the and to from and so on. We will cover what stop words are in detail in a later video. The result, cloudtocities is a Word Cloud object. If we want to display the generated word cloud object, we need to use some matplotlib functionality we call plot IMShow, specifying our Cloud2Cities as an argument. We also specify the interpolation to be bilinear. All this does is make the image appear more smoothly. The IMShow function thus creates the figure we specify. We don't want the image to display X and y axis, and finally call the show method. The IMShow function has created the figure, but we need to call show to display it. We see the word cloud we generated on this piece of text. Which words pop out the most? Let's practice building different word clouds in the.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







