Create your own Word Cloud. Learn to build a very simple word cloud... | by Gurucharan M K | Towards Data Science

Hi everybody! Haven’t all of us seen such interesting and new forms of word clouds being displayed and wondered if we could also generate such word clouds. What if I tell you it is possible with a Python Program comprising of not more than 20 lines of code. Let’s dive in!
In this story, I am going to show you a very simple and concise code on how to build and generate a very simple word cloud from any text data.
For generating this word cloud, I will be using a data set that was originally created for building a spam comment classifier.
The file 'Youtube05-Shakira.csv' is a data set that consists of 5 columns such as the COMMENT ID, AUTHOR, DATE, CONTENT and CLASS. The ‘CONTENT’ and ‘CLASS’ columns are used to build a machine learning model to classify a message if it is a spam message or not.
For generating our word cloud, I will be using only words in the ‘CONTENT’ column.
The first step in any python program will always be on importing the libraries. For this code we will require only three libraries, out of which two should already have been installed in your Python workspace. The only library to be additionally installed before this program is the ‘wordcloud’ library which can be easily installed using the ‘pip’ command.
# Importing the Librariesfrom wordcloud import WordCloud, STOPWORDSimport pandas as pdimport matplotlib.pyplot as plt
In the next step, we will read the data file ( .csv file) and store in into a Pandas DataFrame. The pandas data frames are always easier and faster to use when working with large datasets. The column required for our word cloud generation can be easily accessed from the pandas data frame.
# Read 'Youtube05-Shakira.csv' filedata = pd.read_csv(r"Youtube05-Shakira.csv", encoding ="latin-1")
In this step, we initialize two important strings for our word cloud generation.
The ‘comment_words’ is the string that will be used to store all the words of the CONTENT column in a single line of text
The ‘stop_words’ is used to store all the words that are very commonly used in English language such as ‘the’, ‘a’, ‘an’, ‘in’. These words will be later filtered while generating the word cloud.
Now that we have stored the data file into a pandas dataframe, we now have to convert each row of the ‘CONTENT’ column to a very long, single line of text.
The first step in this will be to split each word in a row ( a comment has a finite number of words) and store it in a variable. separate = i.split()
After splitting the words, for homogeneity of all the words, we convert all the words to lowercase using the .lower() function.
Finally, we join all the words and store it to the variable ‘comment_words’ using the function .join() The variable ‘comment_words’ now contains all the words in a single long text necessary to generate our word cloud.
# Iterating through the .csv data file for i in data.CONTENT:     i = str(i)     separate = i.split()     for j in range(len(separate)):         separate[j] = separate[j].lower()           comment_words += " ".join(separate)+" "
In this step, we finally generate the word cloud using the ‘WordCloud’ function. In this we will be using the two variables ‘stop_words’ and ‘comment_words’. The code is self-explanatory and easy to understand.
# Creating the Word Cloudfinal_wordcloud = WordCloud(width = 800, height = 800,                 background_color ='black',                 stopwords = stop_words,                 min_font_size = 10).generate(comment_words)
The last and final step is to display our word cloud that we just generated using the above code. The ‘matplotlib.pyplot’ library is used to display the word cloud. Take a look at the word cloud generated below!
# Displaying the WordCloud                    plt.figure(figsize = (10, 10), facecolor = None) plt.imshow(final_wordcloud) plt.axis("off") plt.tight_layout(pad = 0)   plt.show()
Hurray! You have now understood how to generate a very simple and basic word cloud using Python and a few lines of code. You can now use this as a base to generate various shapes and types of Word Clouds!
I’m sharing a link to my github repository where the entire code in the .ipynb file is available for you to work on!
github.com
Although, this isn't a part of Machine Learning, this is one of the most basic utilization of another important field of Artificial Intelligence, the Natural Language Processing (NLP). So, go ahead and experiment with this new field of NLP. Till then, Happy Machine Learning!