How to Build Word Cloud in Python?
This article was published as a part of the Data Science Blogathon
Word Clouds came out to be a game-changer visualization technique for understanding and determining patterns and evolving trends. Whether to discover the political agendas of aspiring election candidates of a country or to analyze the customer reviews on the recently launched product, one can get a visual representation by plotting the Word Cloud.
In the next few lines, we will learn about the Word Clouds, their Applications, and how to build them in Python.
Table of Contents
- – Introduction
- – History
- – Applications
- – How To Create Word Cloud in Python
- – Conclusion
Word Cloud or Tag Clouds is a visualization technique for texts that are natively used for visualizing the tags or keywords from the websites. These keywords typically are single words that depict the context of the webpage the word cloud is being made from. These words are clustered together to form a Word Cloud.
Each word in this cloud has a variable font size and color tone. Thus, this representation helps to determine words of prominence. A bigger font size of a word portrays its prominence more relative to other words in the cluster. Word Cloud can be built in varying shapes and sizes based on the creators’ vision. The number of words plays an important role while creating a Word Cloud. More number of words does not always mean a better Word Cloud as it becomes cluttery and difficult to read. A Word Cloud must always be semantically meaningful and must represent what it is meant for.
Although, there are different ways by which Word Clouds can be created but the most widely used type is by using the Frequency of Words in our corpus. And thus, we will be creating our Word Cloud by using the Frequency type.
Image Source – Here
The history of Word Clouds dated back to 1976 when an American Social Psychologist Stanley Milgram conducted a Psychological Study and asked people about the places from Paris. The main idea was to build a mental map of Paris when the people were asked about the city. He analyzed and drew a map based on the responses received from people and kept a bigger font size for the frequently received responses
The Tag Clouds came into the limelight when Flickr, a photo-sharing website, in 2006 started using Tag Clouds for site exploration. By the end of the first decade of the 21st century, Word Cloud become a very popular tool among the text miners.
But, the trend of Tag Cloud keeps vacillating and eventually started declining over the period of time. And thus, Word Clouds are being popularly used in today’s world.
Word Cloud finds its way in numerous applications among several domains. A few of the popular applications of Word Clouds are:
1. Customer’s Feedback
Word Clouds are widely being used in industries by stakeholders to analyze the feedback received from end-users. Assume that a business launches a product and wants to know customer’s feedback. Say the firm received 1000 feedbacks from different users. It would be very difficult for the stakeholders to read and make note of every feedback. Thus, Word Cloud would play a key role in getting top keywords among the feedback. This would help the firm determine if the feedback is positives or negatives and respective areas of improvement. For example, A firm ‘ABC’ released a new Television, and based on the feedback received, the firm can make changes accordingly in the next series of Televisions.
2. Political Agenda of Candidates
Often the candidates of elections keep a checklist of agenda to talk about during the campaigns. Thus, the candidate’s support team would analyze the candidate’s speech and create a Word Cloud to select words for the next speech to keep a balance of agenda checklist. Analysts often create a Word Cloud of candidate’s speeches from different parties to analyze and produce results to let people know which candidate is focusing on what areas of improvisation. For example, in the 2021 U.S. Elections, the word Clouds of both the Republican Party and Democratic Party candidate speeches were readily available by analysts to let people decide.
3. Trending Topics
Advertising Agencies would often need to know what is trending to create the next advertisement in context with trending topics. For example, Amul comes up with a creative advertisement based on the current issue or trend.
How To Create Word Cloud in Python
A Word Cloud in Python can be created in the following steps:
1. Import Necessary Libraries
Import the following libraries which are required to create a Word Cloud
import pandas as pd import matplotlib.pyplot as plt from wordcloud import WordCloud
2. Selecting the Dataset
For this example, we are using Popular Dataset Top Games on Google Play Store from Kaggle.
Download the Dataset and save it in your current working directory for hassle-free code implementation.
Import the dataset into a variable of your choice. Here our data is imported to variable df.
Text for the Word Cloud does not need to be from a Dataset. To get a meaningful text with fewer efforts, we are using the Dataset for our example.
df = pd.read_csv("android-games.csv")
3. Selecting the Text and Amount of Text for Word Cloud
Selecting text for creating a Word Cloud is an important task. One must check for various factors for the selection of Text such as:
- Do we have Problem Statement?
- Does the Selected Text have meaning in it?
- Can we conclude the created Word Cloud?
- Does our Text have an adequate amount of Text?
Word Cloud requires text in an adequate amount. A large number of words would hinder the visual appearance of Word Cloud and a lesser number of words would make no sense.
We can use the .head() method of DataFrame to check the Columns and the type of data present in them. In our example, we have taken the column category as Text.
Since the columns category has a prefix of GAME before each category game, our Word Cloud would end up creating GAME as the most frequent word and Word Cloud will have no meaning in int. Thus, we will perform filtering while adding the category column to the Text.
4. Check for NULL values
It is required to check for the null values in our dataset as while creating the Word Cloud, it would not accept text with nan values.
If our dataset had any NaN values, we need to treat the missing values accordingly. Fortunately, this dataset has no NaN values, thus we can move to the next step.
If there are very few NaN values, it is always advisable to remove such rows as it would not affect the Word Cloud to a larger extent.
4. Adding Text to a Variable
Based on the parameters from Step 3, add the Text Data to a variable of your choice. Here, we are adding the data into variable text.
text = " ".join(cat.split() for cat in df.category)
Since we need to filter the GAME from the category, we have split each row value and took the 2nd item, i.e. the category name from the category column.
5. Creating the Word Cloud
Create an object of class WordCloud with the name of your choice and call the generate() method. Here we have created the object with the name word_cloud.
WordCloud() takes several arguments as per the need. Here we are adding two arguments:
1. collocations = False, which will ignore the collocation words from the Text
2. background_color = ‘White’, which will make the words look clearer
The .generate() method takes one argument of the text we created. In our case, we will give the text variable as an argument to .generate().
word_cloud = WordCloud(collocations = False, background_color = 'white').generate(text)
6. Plotting the Word Cloud
Using the .imshow() method of matplotlib.pyplot to display the Word Cloud as an image.
.imshow() takes several arguments, but in our example, we are taking two arguments:
1. word_cloud created in Step 5
2. interpolation = ‘bilinear’
Since we are creating an image with .imshow(), the resampling of the image is done as the image pixel size and screen resolution doesn’t not match. This resampling is controlled with the interpolation argument to produce softer or crisper images as per our need. There are several types of interpolation available such as gaussian, quadric, bicubic. Here we are using bilinear interpolation.
Plotting the image with axis off as we don’t want axis ticks in our image.
plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show()
7. The Complete Code
#Importing Libraries import pandas as pd import matplotlib.pyplot as plt %matplotlib inline from wordcloud import WordCloud #Importing Dataset df = pd.read_csv("android-games.csv") #Checking the Data df.head() #Checking for NaN values df.isna().sum() #Removing NaN Values #df.dropna(inplace = True) #Creating the text variable text = " ".join(cat.split() for cat in df.category) # Creating word_cloud with text as argument in .generate() method word_cloud = WordCloud(collocations = False, background_color = 'white').generate(text) # Display the generated Word Cloud plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show()
Word Cloud of category column (Image Source – Personal Computer) *The attached image size is irrespective of output image size
Similarly, let’s create Word Cloud for the title column from the imported dataset.
#Importing Libraries import pandas as pd import matplotlib.pyplot as plt %matplotlib inline from wordcloud import WordCloud #Importing Dataset df = pd.read_csv("1.csv") #Checking the Data df.head() #Creating the text variable text2 = " ".join(title for title in df.title) # Creating word_cloud with text as argument in .generate() method word_cloud2 = WordCloud(collocations = False, background_color = 'white').generate(text2) # Display the generated Word Cloud plt.imshow(word_cloud2, interpolation='bilinear') plt.axis("off") plt.show()
Word Cloud of title column (Image Source – Personal Computer) *The attached image size is irrespective of the output image size
Word Clouds created above can be customized further by adding advanced functionalities such as masking, contouring, and adjusting the cloud size.
Word Clouds can be created not just using Python but other tools as well such as Microsoft Word, Business Intelligence Tools such as Tableau. There are currently numerous tools available on the internet that create a Word Cloud just by giving the input text. Since the trend for the Word Cloud is increasing, the number of ways and tools are also increasing.
Word Cloud can be misleading sometimes. In few scenarios, it may happen that many words in our Word Cloud will have the same size, Thus, it might get difficult to say which of them is more frequent than the other. We can use more advanced techniques to counter the problem.
About the Author
Connect with me on LinkedIn Here.
Check out my other Articles Here
You can provide your valuable feedback to me on LinkedIn.
Thanks for giving your time!
The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.