Identify Top Topics using Word Cloud - Towards Data Science

6 Pages • 1,139 Words • PDF • 1.8 MB
Uploaded at 2021-09-24 14:08

This document was submitted by our user and they confirm that they have the consent to share it. Assuming that you are writer or own the copyright of this document, report to us by using this DMCA report button.


8/25/2019

Identify Top Topics using Word Cloud - Towards Data Science

Identify Top Topics using Word Cloud Karan Bhanot Jan 18 · 4 min read

Photo by AbsolutVision on Unsplash

I was recently working with textual data when I discovered Word Clouds. I was really fascinated by how they could reveal so much information just through an image and how easily they could be created through a library. Thus, I decided to work on a quick project to understand them. Word clouds or tag clouds are graphical representations of word frequency that give greater prominence to words that appear more frequently in a source text. — BetterEvaluation https://towardsdatascience.com/identify-top-topics-using-word-cloud-9c54bc84d911

1/6

8/25/2019

Identify Top Topics using Word Cloud - Towards Data Science

Basically, Word Clouds display a set of words in the form of a cloud. The more frequent a word appears in the text, the bigger it will become. Thus, by simply looking at the cloud, you can identify the big words and hence the top topics.

Numerous Areas of Word Cloud Usage I identified that word clouds can actually be used in many areas. Some of them are: 1. Top topics on Social Media: If we could read and get text of posts/tweets that users are sending out, we can extract the top words out of them and they could be used in the trending section to classify and organise posts/tweets under respective sections. 2. Trending News Topics: If we can analyse the text or headings of various news articles, we can extract the top words out of them and identify what are the most trending news topics around a city, country or the whole world. 3. Navigation systems for Websites: Whenever you visit a website that is driven by categories or tags, a word cloud can actually be created and the users can directly jump to any topic while knowing the relevance of the topic across the community.

Project — Detecting top news topics I worked on a project, where I took the dataset of news articles from here and created a word cloud from the headlines of the news articles. The complete code is present as a Jupyter notebook in the Word Cloud repository.

Import libraries While working with importing libraries, I identified that I did not have the package wordcloud . Jupyter provides an easy way to execute command line commands inside

the notebook itself. Just use

!

before the command and it’ll work like it is in a

command line. I am using it to get the

wordcloud

package.

!pip install wordcloud

I now have all the libraries that I need so I import all of them. 1

import collections

2

import numpy as np

https://towardsdatascience.com/identify-top-topics-using-word-cloud-9c54bc84d911

2/6

8/25/2019

Identify Top Topics using Word Cloud - Towards Data Science

3

import pandas as pd

4

import matplotlib.cm as cm

5

import matplotlib.pyplot as plt

6

from matplotlib import rcParams

7

from wordcloud import WordCloud, STOPWORDS

8

%matplotlib inline

import_wordcloud.py hosted with ❤ by GitHub

We get the libraries wordcloud

view raw

numpy , pandas , matplotlib , collections

to use Counter and

to create our Word Cloud.

Working with dataset To begin with, I first import the dataset file into a pandas DataFrame. Note that the encoding of this file for proper reading is

latin-1 . Then, I output the column names to

identify which one matches with the headings. 1

dataset = pd.read_csv('dataset.csv', encoding='latin-1')

2

dataset.columns

3

## Output:

4

# Index(['author', 'date', 'headlines', 'read_more', 'text', 'ctext'], dtype='object')

import_dataset.py hosted with ❤ by GitHub

view raw

We can see that there are 6 columns: author, date, headlines, read_more, text and ctext. However, in this project I will be working with headlines. So, I convert all the headlines to lower case using

lower()

method and combine them into a variable

all_headlines .

1

all_headlines = ' '.join(dataset['headlines'].str.lower())

combine_headings.py hosted with ❤ by GitHub

view raw

Word Cloud Now, we’re ready to create our Word Cloud. After doing one round of analysis, I identified one of the top words being

will . However, it does not provide any useful

information on the topic. Thus, I included it in the set of stopwords so that it is not considered while identifying the top words from the headings. 1

stopwords = STOPWORDS

2

stopwords.add('will')

3 https://towardsdatascience.com/identify-top-topics-using-word-cloud-9c54bc84d911

3/6

8/25/2019

4

Identify Top Topics using Word Cloud - Towards Data Science

wordcloud = WordCloud(stopwords=stopwords, background_color="white", max_words=1000).generate(al

word_cloud.py hosted with ❤ by GitHub

I then call the

WordCloud

output image as

white

view raw

method using these stopwords, keep the background of the

and set maximum words to be

1000 . The image is saved as

wordcloud .

1

rcParams['figure.figsize'] = 10, 20

2

plt.imshow(wordcloud)

3

plt.axis("off")

4

plt.show()

plot_wordcloud.py hosted with ❤ by GitHub

I use

rcParams

imshow

view raw

to define the size of the figure and set the

to display the image and

show

axis

as

off . I then use

to show it.

Word Cloud

From the image, we can clearly see the top two topics as

India

and

Delhi . One can

clearly see how useful a word cloud is to identify the top words in a collection of text. We can even verify the top words using the bar charts. 1

filtered_words = [word for word in all_headlines.split() if word not in stopwords] d

d

ll

i

(fil

d

d )

https://towardsdatascience.com/identify-top-topics-using-word-cloud-9c54bc84d911

4/6

8/25/2019

2

Identify Top Topics using Word Cloud - Towards Data Science

counted_words = collections.Counter(filtered_words)

3 4

words = []

5

counts = []

6

for letter, count in counted_words.most_common(10):

7

words.append(letter)

8

counts.append(count)

most_common_words.py hosted with ❤ by GitHub

I first get

filtered_words

view raw

by splitting all words from the combined headings while

avoiding the stopwords. Then, I used

Counter

to count the frequency of each word. I

then extract the top 10 words and their count. 1

colors = cm.rainbow(np.linspace(0, 1, 10))

2

rcParams['figure.figsize'] = 20, 10

3 4

plt.title('Top words in the headlines vs their count')

5

plt.xlabel('Count')

6

plt.ylabel('Words')

7

plt.barh(words, counts, color=colors)

plot_barchart.py hosted with ❤ by GitHub

Next, I plot the data and label the axis and define a title for the chart. I used

view raw

barh

to

display a horizontal bar chart.

Bar Chart of top 10 most frequent words https://towardsdatascience.com/identify-top-topics-using-word-cloud-9c54bc84d911

5/6

8/25/2019

Identify Top Topics using Word Cloud - Towards Data Science

This also is in alignment with the results from the Word Cloud. Moreover, as a higher count, it is bolder and bigger than

India

Delhi

has

in the Word Cloud.

Conclusion In this article, I discussed about what Word Clouds are, their potential application areas and a project that I worked on to understand them.

. . . As always, please feel free to share your views and opinions.

Data Science

Visualization

Analysis

Data

Data Visualization

About

https://towardsdatascience.com/identify-top-topics-using-word-cloud-9c54bc84d911

Help

Legal

6/6
Identify Top Topics using Word Cloud - Towards Data Science

Related documents

6 Pages • 1,139 Words • PDF • 1.8 MB

11 Pages • 1,314 Words • PDF • 144.9 KB

186 Pages • 63,079 Words • PDF • 9.2 MB

2 Pages • 342 Words • PDF • 262.2 KB

345 Pages • 112,054 Words • PDF • 2.8 MB

2 Pages • 3,160 Words • PDF • 94 KB

4 Pages • 637 Words • PDF • 190.6 KB

8 Pages • 361 Words • PDF • 7.6 MB

28 Pages • 7,937 Words • PDF • 16.6 MB

1 Pages • 394 Words • PDF • 732.8 KB

48 Pages • 2,633 Words • PDF • 1.7 MB

4 Pages • 681 Words • PDF • 154.4 KB