This file contains hidden or 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
# Function to generate word cloud with basic contour | |
def generate_better_wordcloud(data, title, mask=None): | |
cloud = WordCloud(scale=3, | |
max_words=150, | |
colormap='RdYlGn', | |
mask=mask, | |
background_color='white', | |
stopwords=stopwords, | |
collocations=True, | |
contour_color='black', |
This file contains hidden or 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 ImageColorGenerator | |
#Create the mask | |
colosseum_mask = np.array(Image.open('/Users/tiaplagata/Downloads/colosseum.jpg')) | |
#Grab the mask colors | |
colors = ImageColorGenerator(colosseum_mask) | |
#Instantiate the wordcloud using color_func argument | |
cloud = WordCloud(mask=colosseum_mask, |
This file contains hidden or 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
# Function to generate word cloud with dark red contour | |
def generate_better_wordcloud(data, title, mask=None): | |
cloud = WordCloud(scale=3, | |
max_words=150, | |
colormap='RdYlGn', | |
mask=mask, | |
background_color='white', | |
stopwords=stopwords, | |
collocations=True, | |
contour_color='#5d0f24', |
This file contains hidden or 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
# A similar function, but using the mask | |
def generate_better_wordcloud(data, title, mask=None): | |
cloud = WordCloud(scale=3, | |
max_words=150, | |
colormap='RdYlGn', | |
mask=mask, | |
background_color='white', | |
stopwords=stopwords, | |
collocations=True).generate_from_text(data) | |
plt.figure(figsize=(10,8)) |
This file contains hidden or 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
# Create an array from the image you want to use as a mask | |
## Your file path will look different | |
rome_mask = np.array(Image.open('/Users/tiaplagata/Downloads/italy.jpg')) |
This file contains hidden or 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
#Define a list of stop words | |
stopwords = ['private', 'tour', 'transfer', 'guide', 'skip', 'line', | |
'skiptheline', 'vice', 'versa'] | |
#A function to generate the word cloud from text | |
def generate_basic_wordcloud(data, title): | |
cloud = WordCloud(width=400, | |
height=330, | |
max_words=150, | |
colormap='tab20c', |
This file contains hidden or 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
# Store our document term matrix data for Rome | |
data = dtm.transpose()['Rome, Italy'].sort_values(ascending=False) | |
# Generate the word cloud from frequencies | |
wc = WordCloud().generate_from_frequencies(data) | |
plt.imshow(wc) | |
plt.axis('off') | |
plt.show() |
This file contains hidden or 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
#Give our Rome corpus a variable name | |
rome_corpus = df.lemmatized[10] | |
#Instantiate wordcloud object and use method to feed it our corpus | |
wc = WordCloud().generate_from_text(rome_corpus) | |
#Use matplotlib.pyplot to display the fitted wordcloud | |
#Turn axis off to get rid of axis numbers | |
plt.imshow(wc) | |
plt.axis('off') |
This file contains hidden or 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, ImageColorGenerator | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import numpy as np |
This file contains hidden or 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 tensorflow.keras.preprocessing.image import ImageDataGenerator | |
# Create data generators | |
# File path = path to train/test/val folders respectively | |
# Use a target size of 224x224 px for each image (or whatever size you choose) | |
# Batch size = total number of images in the train set, test set, val set respectively | |
# Ensure class_mode is binary | |
train_generator = ImageDataGenerator(rescale=1./255).flow_from_directory( | |
'/content/chest_xray/train', | |
target_size=(224, 224), |
NewerOlder