CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Sequence Models/Week 2/Emojify/__pycache__/emo_utils.cpython-36.pyc
Views: 13380
3

��lZf�@s�ddlZddlZddlZddlZddljZddl	m
Z
dd�Zdd�Zddd	�Z
d
d�Zdd
dddd�Zdd�Zdd�Zdejjfdd�Zdd�ZdS)�N)�confusion_matrixc
Cs�t|d���}t�}i}xF|D]>}|j�j�}|d}|j|�tj|dd�tjd�||<qWd}i}i}x(t|�D]}	|||	<|	||<|d}qtWWdQRX|||fS)N�rr�)�dtype)	�open�set�strip�split�add�np�array�float64�sorted)
Z
glove_file�f�words�word_to_vec_map�lineZ	curr_word�iZwords_to_indexZindex_to_words�w�r�-/home/jovyan/work/Week 2/Emojify/emo_utils.py�read_glove_vecss 

 rcCs tj|tj|��}||j�S)z4Compute softmax values for each sets of scores in x.)r�exp�max�sum)�xZe_xrrr�softmaxsr�data/emojify_data.csvcCspg}g}t|��:}tj|�}x(|D] }|j|d�|j|d�q"WWdQRXtj|�}tj|td�}||fS)Nrr)r)r�csv�reader�appendr�asarray�int)�filename�phrase�emojiZcsvDataFileZ	csvReader�row�X�Yrrr�read_csv!s



r)cCstj|�|jd�}|S)Nr�����)r�eye�reshape)r(�Crrr�convert_to_one_hot1sr.u❤️z
:baseball:z:smile:z:disappointed:z:fork_and_knife:)�0�1�2�3�4cCstjtt|�dd�S)zi
    Converts a label (int or string) into the corresponding emoji code (string) ready to be printed
    T)Zuse_aliases)r%Zemojize�emoji_dictionary�str)�labelrrr�label_to_emoji<sr7cCs<t�x0t|jd�D]}t||tt||���qWdS)Nr)�print�range�shaper7r")r'�predrrrr�print_predictionsCsr<zConfusion matrixcCs�tj||j|jd�dgdgdd�}||jdd�}tj||d�tj�tj	t
|j��}tj||jd	d
�tj
||j�tj|jj�tj|jj�dS)NrZActualZ	PredictedT)ZrownamesZcolnames�marginsr)�axis)�cmap�-)Zrotation)�pdZcrosstabr,r:r�pltZmatshowZcolorbarr�arange�len�columnsZxticksZyticks�indexZylabel�nameZxlabel)Zy_actuZy_pred�titler?Zdf_confusionZdf_conf_normZ
tick_marksrrr�plot_confusion_matrixIs$rIc
Cs�|jd}tj|df�}xvt|�D]j}||j�j�}tjd�}	x|D]}
|	||
7}	qFW|	t|�}	tj||	�|}t|�}tj	|�||<q"Wt
dttj|dd�|j
|jdd�dd�k���|S)a}
    Given X (sentences) and Y (emoji indices), predict emojis and compute the accuracy of your model over the given set.
    
    Arguments:
    X -- input data containing sentences, numpy array of shape (m, None)
    Y -- labels, containing index of the label emoji, numpy array of shape (m, 1)
    
    Returns:
    pred -- numpy array of shape (m, 1) with your predictions
    rr�2z
Accuracy: N)rJ)r:r�zerosr9�lowerr	rD�dotr�argmaxr8r5�meanr,)
r'r(�W�br�mr;�jr�avgr�Z�Arrr�predictZs


8rW)r)r�numpyrr%�pandasrA�matplotlib.pyplot�pyplotrBZsklearn.metricsrrrr)r.r4r7r<�cmZgray_rrIrWrrrr�<module>s$