Create your own Word Cloud

栏目: IT技术 · 发布时间: 4年前

内容简介:In this story, I am going to show you a veryFor generating this word cloud, I will be using a data set that was originally created for building aThe file

Overview

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.

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.

Step 1: Importing the Libraries

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, STOPWORDS
import pandas as pd
import matplotlib.pyplot as plt

Step 2: Reading the Data File

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")

Step 3: Setting the comment and stop words

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.

Step 4: Iterating through the data file

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)+" "

Step 5: Creating the Word Cloud

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 Cloud
final_wordcloud = WordCloud(width = 800, height = 800, 
                background_color ='black', 
                stopwords = stop_words, 
                min_font_size = 10).generate(comment_words)

Step 6: Displaying the Word Cloud

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()

Create your own Word Cloud

Generated Word Cloud

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!

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!


以上所述就是小编给大家介绍的《Create your own Word Cloud》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

逆流而上

逆流而上

阿里巴巴集团成长集编委会 / 电子工业出版社 / 2017-11 / 59.00

本书是阿里巴巴集团荣耀背后的技术血泪史。全书通过分享业务运行过程中各个领域发生的典型“踩坑”案例,帮助大家快速提升自我及团队协作,学习到宝贵的处理经验及实践方案,为互联网生产系统的稳定共同努力。从基础架构、中间件、数据库、云计算、大数据等技术领域中不断积累经验,颠覆技术瓶颈,不断创新以适应不断增长的需求。 本书主要面向互联网技术从业人员和在校师生,使读者能够通过此书基本了解阿里在各技术领域的能力,......一起来看看 《逆流而上》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具