![]() |
|
[Tut] How to Create Word Clouds Using Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Create Word Clouds Using Python? (/thread-99220.html) |
[Tut] How to Create Word Clouds Using Python? - xSicKxBot - 04-15-2022 How to Create Word Clouds Using Python? You may have already learned how to analyze quantitative data using graphs such as bar charts and histograms. But do you know how to study textual data? One way to analyze textual information is by using a word cloud: There are many ways to create word clouds, but we will use the Code: WordCloudCode: WordCloudWhat Are Word Clouds? Definition: A word cloud (also known as a tag cloud) is a visual representation of the words that appear most frequently in a given text. They can be used to summarize large bodies of text or to visualize the sentiment of a document.A word cloud is a graphical representation of text data in which the size of each word is proportional to the number of times it appears in the text. They can be used to visualize the most critical words in a document quickly or to get an overview of the sentiment of a piece of text. There are word clouds apps such as Wordle, but in this blog post, we will show how to create word clouds using the Python library Code: WordCloudWhat’s the WordCloud Library in Python? The WordCloud library is open source and easy to use to create word clouds in Python. It allows you to create word clouds in various formats, including PDF, SVG, and image files. In addition, it provides several options for customizing your word clouds, including the ability to control the font, color, and layout. You can install it using the following command in your terminal (without the Code: $Code: $ pip install wordcloudRelated Article: Where Are Word Clouds Used? Word clouds are a fun and easy way to visualize data. By displaying the most common words in a given text, they can provide insights into the overall themes and tone of the text.
Pros of Word Clouds The advantages of using word clouds are: First, you can use them to summarize a large body of text quickly and easily. Identifying the most frequently used words in a text can provide a quick overview of the main points. Second, with word clouds, you can quickly visualize the sentiment in a document. The size and placement of words in the Word Cloud can give you insights into the overall tone of the document. This tool is handy when analyzing a large body of text, such as customer feedback or reviews. Third, word clouds can be a valuable tool for identifying the most critical keywords in a text. By analyzing the distribution of words, you can quickly identify which terms are most prominent. The word clouds can be beneficial when monitoring changing trends or assessing the overall importance. Fourth, word clouds can be used to create designs that incorporate both visual and textual elements. By blending words and images, word clouds can add another layer of meaning to an already exciting design. How to Create Word Clouds in Python? We will be using Disneyland reviews downloaded from Kaggle to create a word cloud data visualization. You can download the file from here. In this file, we will be focussing on the Code: Review_TextFirst, you have to install the WordCloud Python library. You can do this by running the following command in a terminal: Code: pip install wordcloudOnce you have installed Code: WordCloudCode: pandasCode: matplotlib.pyplotCode: wordcloudCode: import pandas as pd
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as pltThe Code: pandasWe will show you the use of STOPWORDS in the upcoming section. The data frame variable “ Code: dfCode: disneylandreviews.csvCode: df = pd.read_csv("/Users/mohamedthoufeeq/Downloads/DisneylandReviews.csv")Now run the program and see the output. You get the following Unicode decode error. Code: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf4 in position 121844: invalid continuation byteThe Unicode decode error means that the string could not be properly decoded into UTF-8. This can happen when a file is downloaded from the Kaggle, and it is not in the correct encoding format. To solve this problem, you need to specify the encoding format for the file. You can type the following command in a terminal: Code: df = pd.read_csv("/Users/mohamedthoufeeq/Downloads/DisneylandReviews.csv",encoding='ISO-8859-1')The Code: encoding = 'ISO-8859-1'Next, create a word cloud using the Code: WordCloudCode: wordcloud = WordCloud().generate(['Review_Text'])In this above code, Code: WordCloud().generate()The Code: generate()Code: Review_TextGo ahead and run the code. You get again following error. Code: TypeError: expected string or bytes-like objectThe type error means that the word cloud object expects a string or a bytes-like object. But the data type is Pandas series. To solve this, You have to type following command Code: wordcloud = WordCloud().generate(' '.join(df['Review_Text']))The above command converts the series to strings data type. Code: plt.imshow(wordcloud)The Code: plt.imshow()Then remove the axis with the following command: Code: plt.axis("off")The Code: "off"Finally, the below commands displays the image of the word cloud. Code: plt.show()Once run the program you will see a word cloud image as shown below: The word Code: "Park"But there are words such as Code: "Disneyland"Code: "went"Code: "will"Code: "park"Code: "go"Code: "day"Code: "One"So we can exclude them from the word cloud with the following command using the stopwords parameter. Code: STOPWORDS.update(['Disneyland', 'went','will,'go',"park", "day","one"])
wordcloud = WordCloud(stopwords = STOPWORDS).generate(' '.join(df['Review_Text']))Code: STOPWORDSCode: STOPWORDSNow re-run the program, and you will get the following word cloud image. Before we can analyze the words, let us see how to customize the words’ appearance. You can also customize the appearance of your word cloud by changing the font size and background color. The maximum font size can be set with the Code: max_font_sizeCode: min_font_sizeCode: background_colorCode: wordcloud = WordCloud(min_font_size = 10, max_font_size = 70, stopwords = STOPWORDS, background_color="white").generate(' '.join(df['Review_Text']))The code sets the font size to a minimum of 10 points and a maximum of 70 points, and the background color to white. Re-run the program, and you will get the following word cloud image. Also, you can set the maximum amount of words to be generated using the Code: max_wordsCode: wordcloud = WordCloud(min_font_size = 5, max_font_size = 100, max_words = 1000, stopwords = STOPWORDS, background_color="white").generate(' '.join(df['Review_Text']))The above code sets the maximum number of words generated in the word cloud to 1000. Also, change the font size to 5 and 100. Re-run the program, and you will get the following word cloud. As you can see, when you increase the number of words to 1000, the words that are repeated more in the reviews are shown in a larger size. This makes it easier to find out which words are prominent. In this word cloud, you can see that Code: "ride"You set width and height of the word cloud image. Code: wordcloud = WordCloud(width=350, height=350, min_font_size=5, max_font_size=100, max_words=1000, stopwords=STOPWORDS, background_color="white").generate(' '.join(df['Review_Text']))The above code sets the width and height of the word cloud to 350. Re-run the program, and you will get the following word cloud image. Now let’s analyze the word cloud to get some insights. The word Code: "ride"Next, the word Code: "attraction"Also, the word Code: "time"Staffs of Disney land were very lovely. It is reflected in the word cloud as the word Code: "nice"The words Code: "lines"Code: "queue"But the word Code: "hotel" Exercise: You can get more insights by analyzing the word cloud data. Try it out!Summary Word clouds are a great way to summarize large bodies of text or visualize a document’s sentiment. Word clouds are a great way to understand large bodies of text and can be used for various purposes. This blog post showed how to create word clouds using the Python library Code: WordCloudWe also discussed how to customize the appearance of the word cloud and analyzed the word cloud data to get insights into the text. What do you use? |