Generating a word cloud in Python

Python Methods and Functions

The following modules are required to generate a word cloud in Python: matplotlib, pandas, and wordcloud. To install these packages, run the following commands:

 pip install matplotlib pip install pandas pip install wordcloud 

The dataset used to generate the wordcloud is compiled from the UCI Machine Learning Repository. It consists of YouTube comments on videos of popular artists. 
Dataset Link: https://archive.ics.uci.edu/ml/ machine-learning-databases / 00380 /

Below is the implementation:

# Python program for generating WordCloud

 
# import all required modules

from wordcloud import WordCloud, STOPWORDS

import matplotlib.pyplot as plt

import pandas as pd

 
# Reads the Youtube04-Eminem.csv file

df < code class = "keyword"> = pd.read_csv (r "Youtube04-Eminem.csv" , encoding = "latin-1" )

 

comment_words = ' '

stopwords = set (STOPWORDS)

 
# iterate over the csv file

for val in df.CONTENT:

  

# typecaste each shaft per line

  val = str ( val)

 

# split value

tokens = val.split ()

  

# Converts each token to lowercase

for i in range ( len (tokens)):

tokens [i] = tokens [i] .lower ()

  

  for words in tokens:

  comment_words = comment_words + words + ''

 

 

wordcloud = WordCloud (width = 800 , height = 800 ,

background_color = ' white' ,

stopwords = stopwords,

  min_font_size = 10 ). generate (comment_words)

 
# build WordCloud image

plt.figure (figsize = ( 8 , 8 ), facecolor = None )

plt.imshow (wordcloud)

plt.axis ( " off " )

plt.tight_layout (pad = 0 )

 
plt.show ()

Output:

The above word cloud was generated using Youtube04-Eminem.csv in the dataset. One interesting task might be to generate word clouds using other CSV files available in the dataset.

Benefits of Word Clouds:

  1. Analyze customer and employee feedback.
  2. Revealing new SEO keywords for a goal.

Disadvantages of word clouds:

  1. Word clouds are not ideal for every situation.
  2. Data should be optimized for context.
  3. Link: https://en.wikipedia.org/wiki/Tag_cloud





Get Solution for free from DataCamp guru