Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
binary-classification-of-tweets-about-protest/w_cloud.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
48 lines (41 sloc)
1.78 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wordcloud import WordCloud, STOPWORDS | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
#CHANGE AS NEEDED | |
df = pd.read_csv( | |
"data/computed/cluster/april-21/1-gram-8-clusters.csv", encoding="latin-1") | |
export_path = 'reports/wordclouds/april-21/' | |
cluster_no = 8 | |
stopwords = set(STOPWORDS) #need for wordcloud | |
for i in range(0, cluster_no): | |
both = df[df['cluster'] == i] | |
both['text'] = both['text'].apply(lambda x: str(x)) | |
print('printing both wordcloud for cluster no. {}'.format(i)) | |
wordcloud = WordCloud(width=800, height=800, | |
background_color='white', | |
stopwords=stopwords, | |
min_font_size=10).generate(" ".join(both.text)) | |
wordcloud.to_file(export_path + 'cluster_{}_both.png'.format(i)) | |
"""yes = df[(df['Y/N'] == 1) & (df['cluster'] == i)] | |
#print(yes.head()) | |
print('printing yes wordcloud for cluster no. {}'.format(i)) | |
wordcloud = WordCloud(width=800, height=800, | |
background_color='white', | |
stopwords=stopwords, | |
min_font_size=10).generate(" ".join(yes.text)) | |
wordcloud.to_file(export_path + 'cluster_{}_yes.png'.format(i)) | |
no = df[(df['Y/N'] == 0) & (df['cluster'] == i)] | |
#print(no.head()) | |
print('printing no wordcloud for cluster no. {}'.format(i)) | |
wordcloud = WordCloud(width=800, height=800, | |
background_color='white', | |
stopwords=stopwords, | |
min_font_size=10).generate(" ".join(no.text)) | |
wordcloud.to_file( | |
export_path + 'cluster_{}_no.png'.format(i))""" | |
#if want to display | |
# plt.figure(figsize=(8, 8), facecolor=None) | |
# plt.imshow(wordcloud) | |
# plt.axis("off") | |
# plt.tight_layout(pad=0) | |
# plt.show() |