Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Sequence Models/Week 2/Emojify/emo_utils.py
Views: 13377
import csv1import numpy as np2import emoji3import pandas as pd4import matplotlib.pyplot as plt5from sklearn.metrics import confusion_matrix67def read_glove_vecs(glove_file):8with open(glove_file, 'r') as f:9words = set()10word_to_vec_map = {}11for line in f:12line = line.strip().split()13curr_word = line[0]14words.add(curr_word)15word_to_vec_map[curr_word] = np.array(line[1:], dtype=np.float64)1617i = 118words_to_index = {}19index_to_words = {}20for w in sorted(words):21words_to_index[w] = i22index_to_words[i] = w23i = i + 124return words_to_index, index_to_words, word_to_vec_map2526def softmax(x):27"""Compute softmax values for each sets of scores in x."""28e_x = np.exp(x - np.max(x))29return e_x / e_x.sum()303132def read_csv(filename = 'data/emojify_data.csv'):33phrase = []34emoji = []3536with open (filename) as csvDataFile:37csvReader = csv.reader(csvDataFile)3839for row in csvReader:40phrase.append(row[0])41emoji.append(row[1])4243X = np.asarray(phrase)44Y = np.asarray(emoji, dtype=int)4546return X, Y4748def convert_to_one_hot(Y, C):49Y = np.eye(C)[Y.reshape(-1)]50return Y515253emoji_dictionary = {"0": "\u2764\uFE0F", # :heart: prints a black instead of red heart depending on the font54"1": ":baseball:",55"2": ":smile:",56"3": ":disappointed:",57"4": ":fork_and_knife:"}5859def label_to_emoji(label):60"""61Converts a label (int or string) into the corresponding emoji code (string) ready to be printed62"""63return emoji.emojize(emoji_dictionary[str(label)], use_aliases=True)646566def print_predictions(X, pred):67print()68for i in range(X.shape[0]):69print(X[i], label_to_emoji(int(pred[i])))707172def plot_confusion_matrix(y_actu, y_pred, title='Confusion matrix', cmap=plt.cm.gray_r):7374df_confusion = pd.crosstab(y_actu, y_pred.reshape(y_pred.shape[0],), rownames=['Actual'], colnames=['Predicted'], margins=True)7576df_conf_norm = df_confusion / df_confusion.sum(axis=1)7778plt.matshow(df_confusion, cmap=cmap) # imshow79#plt.title(title)80plt.colorbar()81tick_marks = np.arange(len(df_confusion.columns))82plt.xticks(tick_marks, df_confusion.columns, rotation=45)83plt.yticks(tick_marks, df_confusion.index)84#plt.tight_layout()85plt.ylabel(df_confusion.index.name)86plt.xlabel(df_confusion.columns.name)878889def predict(X, Y, W, b, word_to_vec_map):90"""91Given X (sentences) and Y (emoji indices), predict emojis and compute the accuracy of your model over the given set.9293Arguments:94X -- input data containing sentences, numpy array of shape (m, None)95Y -- labels, containing index of the label emoji, numpy array of shape (m, 1)9697Returns:98pred -- numpy array of shape (m, 1) with your predictions99"""100m = X.shape[0]101pred = np.zeros((m, 1))102103for j in range(m): # Loop over training examples104105# Split jth test example (sentence) into list of lower case words106words = X[j].lower().split()107108# Average words' vectors109avg = np.zeros((50,))110for w in words:111avg += word_to_vec_map[w]112avg = avg/len(words)113114# Forward propagation115Z = np.dot(W, avg) + b116A = softmax(Z)117pred[j] = np.argmax(A)118119print("Accuracy: " + str(np.mean((pred[:] == Y.reshape(Y.shape[0],1)[:]))))120121return pred122123