DAT-501_7-2 Lab Word Cloud
docx
keyboard_arrow_up
School
Southern New Hampshire University *
*We aren’t endorsed by this school
Course
501
Subject
English
Date
Feb 20, 2024
Type
docx
Pages
4
Uploaded by AdmiralFlamingoPerson763
DAT-501 Foundations in Data Science
7-2 Lab: Word Cloud
By
Kumari Sweta
Submitted To: Frederick Mobley
Table of Contents
1.
R SCRIPT:
.....................................................................................................................................
2
2.
COMPARE THE R SCRIPT WITH THE PYTHON SCRIPT.
...................................................
3
1.
R Script:
Below is the R script for word cloud.
2.
Compare the R script with the Python script.
Python script: #importing all necessary modules
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import pandas as pd
import datetime
#Read 7_2_assessment file
df = pd.read_csv(
"c:\\users\\administrator\\desktop\\501\\7-
2_assessment.csv"
)
comment_words = ' '
#iterate through the csv file
for val in df.DispensedState: val = str(val)
#typecast each val to string
val=str(val)
#split the value
tokens = val.split()
for words in tokens: comment_words = comment_words + words + ' '
wordcloud = WordCloud().generate(comment_words)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
#plot the wordcloud image
plt.figure()
plt.imshow(wordcloud)
plt.show()
Comparison
: Both R and Python scripts perform similar tasks, which is to create a CSV file and create a word cloud. However, there are differences in syntax and the specific libraries used. Below is the comparison between them:
1.
CSV File Reading:
R: Uses ‘read_csv’ from the ‘readr’ package to read the CSV file.
Python: Uses ‘pd.read_csv’ from the ‘pandas’ library to read the CSV file.
2.
Text Processing and Corpus Creation:
R: Utilizes the ‘tm’ package to create a text corpus from the ‘DispensedState’ column.
Python: Concatenates words into a single string and then uses the ‘WordCloud’ library directly on the string.
3.
Wordcloud Customization:
R: No explicit customization is done in the provided script.
Python: The word cloud is generated with default settings, no additional customization is applied in the provided script.
4.
Plotting:
R: Calls the ‘wordcloud’ function directly.
Python: Uses ‘imshow’ from the ‘matplotlib’ library to display the word cloud.