I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Parameter tuning
We will use GridSearchCV, which exhaustively considers all parameter combinations, to find the best model for the data. A search consists of:
- an estimator (regressor or classifier )
- a parameter space;
- a method for searching or sampling candidates;
- a cross-validation scheme;
- a score function, such as accurracy_score()
Set parameters we will test for each algorithm
Can you write Python code for it?

parameters_log = {'vect__ngram_range': [(1, 1), (1, 2)],'tfidf__use_idf': (True, False), 'clf__penalty': ['l1','l2'], 'clf__solver':['liblinear']}
parameters_mnb = {'vect__ngram_range': [(1, 1), (1, 2)],'tfidf__use_idf': (True, False),'clf__alpha': (1,1e-2, 1e-3)}


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Plot Confusion Matrix
Can you write Python code for it?

Log_matrix=confusion_matrix(y_test, predicted_Log)
Log_matrix=Log_matrix[::-1, ::-1]
MNB_matrix=confusion_matrix(y_test, predicted_MNB)
MNB_matrix=MNB_matrix[::-1, ::-1]

def plot_confusion_matrix(cm, classes,  normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i,round(cm[i, j],2), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

plot_confusion_matrix(Log_matrix, classes=["Positive","Negative"], normalize=True, title='Normalized confusion matrix')

plot_confusion_matrix(MNB_matrix, classes=["Positive","Negative"], normalize=True,  title='Normalized confusion matrix')


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Predict on the test set and check metrics
Can you write Python code for it?

predicted_Log = Log_clf.predict(X_test)
predicted_MNB =MNB_clf.predict(X_test)
dec_Log = Log_clf.decision_function(X_test)
dec_MNB =MNB_clf.predict_proba(X_test)


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Predict on unclassified data
Can you write Python code for it?

with open("/resources/data/unclassified_tweets.txt", "r",encoding="utf8") as myfile:
     unclass_data = myfile.readlines()


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Print the metrics of the performance of the algorithms
Can you write Python code for it?

X_test_new_Log=[]
X_test_new_MNB=[]
y_new=[]
Log_list=predicted_Log.tolist()
MNB_list=predicted_MNB.tolist()
for x in Log_list:
    X_test_new_Log.append(int(x))
for x in MNB_list:
    X_test_new_MNB.append(int(x))
for x in y_test:
    y_new.append(int(x))

Log_list_prediction=[1 if x==4 else x for x in X_test_new_Log]
MNB_list_prediction=[1 if x==4 else x for x in X_test_new_MNB]
target_new=[1 if x==4 else x for x in y_new]

print('Metrics Logistic Regression')
print('-------------------------------------')
print("Accuracy:",accuracy_score(target_new, Log_list_prediction))
print("Recall:",recall_score(target_new,Log_list_prediction))
print("Precision:",precision_score(target_new, Log_list_prediction))
print("F1 Score:",f1_score(target_new, Log_list_prediction))
print('' '')
print('Metrics Multinomial Naive Bayes')
print('-------------------------------------')
print("Accuracy:",accuracy_score(target_new, MNB_list_prediction))
print("Recall:",recall_score(target_new,MNB_list_prediction))
print("Precision:",precision_score(target_new, MNB_list_prediction))
print("F1 Score:",f1_score(target_new, MNB_list_prediction))


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Put data into a dataframe
Can you write Python code for it?

predictions=[]
for x in range(0,len(unclass_data)):
    predictions.append((unclass_data[x],party_af[x],predicted_MNB_unclass[x]))
tweets=pd.DataFrame(predictions, columns=["Tweet","Party","Classification - MNB"])
tweets.head()

def get_sent(tweet):
    classf = tweet
    return 'Positive' if classf=='4' else 'Negative'
tweets_clean= tweets[tweets.Party !="Other"]
tweets_clean['Sentiment'] = tweets_clean['Classification - MNB'].apply(get_sent)
tweets_clean.drop(labels=['Classification - MNB'], axis=1, inplace=True)
tweets_clean.head()

sns.countplot(x='Sentiment', hue="Party", data=tweets_clean)


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
ROC Curves
Can you write Python code for it?

predicted_Log_new=[]
y_actual=[]
for x in dec_Log:
    predicted_Log_new.append(int(x))
for x in y_test:
    y_actual.append(int(x))

Log_list_prediction=[1 if x==4 else x for x in predicted_Log_new]
target_new=[1 if x==4 else x for x in  y_actual]

fpr, tpr, thresholds=roc_curve(target_new, Log_list_prediction, pos_label=1)
roc_auc= auc(fpr, tpr)
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve (area = %0.3f)' % roc_auc )
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Logistic Regression ROC Curve')
plt.legend(loc="lower right")
plt.show()
plt.savefig('Log_roc.jpg')

predicted_MNB_new=[]
y_actual=[]
for x in range(0,60000):
    predicted_MNB_new.append(dec_MNB[x][1])
#for x in dec_MNB:
#    predicted_MNB_new.append(int(x))
for x in y_test:
    y_actual.append(int(x))

#MNB_list_prediction=[1 if x==4 else x for x in predicted_MNB_new]
target_new=[1 if x==4 else x for x in  y_actual]

fpr, tpr, thresholds=roc_curve(target_new, predicted_MNB_new)
roc_auc= auc(fpr, tpr)
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve (area = %0.3f)' % roc_auc )
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Multinomial Naive Bayes ROC Curve')
plt.legend(loc="lower right")
plt.show()
plt.savefig('MNB_roc.jpg')


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Separate tweets into two sets: tweet data and its respective classification
Can you write Python code for it?

X=[]
y=[]
for x in data:
    X.append(x[1:])
    y.append(x[0])


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Set models and do the search of the parameter
Can you write Python code for it?

#Set models
acc_scorer = make_scorer(accuracy_score)
gs_log_clf = GridSearchCV(Log_clf, parameters_log, n_jobs=-1, scoring=acc_scorer)
gs_mnb_clf = GridSearchCV(MNB_clf, parameters_mnb, n_jobs=-1, scoring=acc_scorer)
# Grid search of best parameters
print ("-----Tunning of parameters-----")
start = time.time()
gs_log_clf = gs_log_clf.fit(X_train, y_train)
end = time.time()
print ("Logistic Regression -"," Running Time:", end - start,"s")
start = time.time()
gs_mnb_clf= gs_mnb_clf.fit(X_train, y_train)
end = time.time()
print ("Multinomial Naive Bayes -"," Running Time:", end - start,"s")


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Set the classifiers to the have the best combination of parameters
Can you write Python code for it?

Log_clf= gs_log_clf .best_estimator_
MNB_clf= gs_mnb_clf .best_estimator_


I am working on the file "Sentiment Analysis.ipynb".
The first task is:
Split the data into the training set and test set for crossvalidation
Can you write Python code for it?

X_train, X_test, y_train, y_test = train_test_split(X,y, random_state=42, test_size = 0.3)


I am working on the file "Sentiment Analysis/Handwritten Digit Recognition with TFLearn and MNIST/handwritten-digit-recognition-with-tflearn.ipynb".
The first task is:
Building the network
TFLearn lets you build the network by defining the layers in that network.
For this example, you'll define:

The input layer, which tells the network the number of inputs it should expect for each piece of MNIST data.
Hidden layers, which recognize patterns in data and connect the input to the output layer, and
The output layer, which defines how the network learns and outputs a label for a given image.

Let's start with the input layer; to define the input layer, you'll define the type of data that the network expects. For example,
net = tflearn.input_data([None, 100])
would create a network with 100 inputs. The number of inputs to your network needs to match the size of your data. For this example, we're using 784 element long vectors to encode our input data, so we need 784 input units.
Adding layers
To add new hidden layers, you use
net = tflearn.fully_connected(net, n_units, activation='ReLU')
This adds a fully connected layer where every unit (or node) in the previous layer is connected to every unit in this layer. The first argument net is the network you created in the tflearn.input_data call, it designates the input to the hidden layer. You can set the number of units in the layer with n_units, and set the activation function with the activation keyword. You can keep adding layers to your network by repeated calling tflearn.fully_connected(net, n_units).
Then, to set how you train the network, use:
net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1, loss='categorical_crossentropy')
Again, this is passing in the network you've been building. The keywords:

optimizer sets the training method, here stochastic gradient descent
learning_rate is the learning rate
loss determines how the network error is calculated. In this example, with categorical cross-entropy.

Finally, you put all this together to create the model with tflearn.DNN(net).
Exercise: Below in the build_model() function, you'll put together the network using TFLearn. You get to choose how many layers...
Can you write Python code for it?

# Define the neural network
def build_model(learning_rate):
    # This resets all parameters and variables, leave this here
    tf.reset_default_graph()

    #### Your code ####
    # Include the input layer, hidden layer(s), and set how you want to train the model

    # Input layer
    net = tflearn.input_data([None, 784])

    # Hidden layers
    net = tflearn.fully_connected(net, 200, activation='ReLU')
    net = tflearn.fully_connected(net, 40, activation='ReLU')

    # Output layers
    net = tflearn.fully_connected(net, 10, activation='softmax')
    net = tflearn.regression(net, optimizer='sgd', learning_rate=learning_rate, loss='categorical_crossentropy')

    # This model assumes that your network is named "net"
    model = tflearn.DNN(net)
    return model

# Build the model
model = build_model(learning_rate=0.1)


I am working on the file "Sentiment Analysis/Handwritten Digit Recognition with TFLearn and MNIST/handwritten-digit-recognition-with-tflearn.ipynb".
The first task is:
Training the network
Now that we've constructed the network, saved as the variable model, we can fit it to the data. Here we use the model.fit method. You pass in the training features trainX and the training targets trainY. Below I set validation_set=0.1 which reserves 10% of the data set as the validation set. You can also set the batch size and number of epochs with the batch_size and n_epoch keywords, respectively.
Too few epochs don't effectively train your network, and too many take a long time to execute. Choose wisely!
Can you write Python code for it?

# Training
model.fit(trainX, trainY, validation_set=0.1, show_metric=True, batch_size=100, n_epoch=10)


I am working on the file "Sentiment Analysis/Handwritten Digit Recognition with TFLearn and MNIST/handwritten-digit-recognition-with-tflearn.ipynb".
The first task is:
Visualize the training data
Provided below is a function that will help you visualize the MNIST data. By passing in the index of a training example, the function show_digit will display that training image along with it's corresponding label in the title.
Can you write Python code for it?

# Visualizing the data
import matplotlib.pyplot as plt
%matplotlib inline

# Function for displaying a training image by it's index in the MNIST set
def show_digit(index):
    label = trainY[index].argmax(axis=0)
    # Reshape 784 array into 28x28 image
    image = trainX[index].reshape([28,28])
    plt.title('Training data, index: %d,  Label: %d' % (index, label))
    plt.imshow(image, cmap='gray_r')
    plt.show()

# Display the first (index 0) training image
show_digit(0)
show_digit(10)


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/1-framing-problems-for-nns.ipynb".
The first task is:
Lesson: Develop a Predictive Theory
Can you write Python code for it?

print("labels.txt \t : \t reviews.txt\n")
pretty_print_review_and_label(2137)
pretty_print_review_and_label(12816)
pretty_print_review_and_label(6267)
pretty_print_review_and_label(21934)
pretty_print_review_and_label(5297)
pretty_print_review_and_label(4998)


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/1-framing-problems-for-nns.ipynb".
The first task is:
Test Theory
Can you write Python code for it?

from collections import Counter
import numpy as np

positive_count = Counter()
negative_count = Counter()
total_count = Counter()

for i in range(len(reviews)):
    if (labels[i] == 'POSITIVE'):
        for word in reviews[i].split(" "):
            positive_count[word] += 1
            total_count[word] += 1
    else:
        for word in reviews[i].split(" "):
            negative_count[word] +=1
            total_count[word] +=1

positive_count.most_common()[:15]

pos_ratios = Counter()
neg_ratios = Counter()

for word, count in list(total_count.most_common()):
        if count > 100:
            pos_ratio = positive_count[word] / float(total_count[word] + 1)
            neg_ratio = negative_count[word] / float(total_count[word] + 1)
            pos_ratios[word] = pos_ratio
            neg_ratios[word] = neg_ratio

pos_ratios.most_common()[:15]

neg_ratios.most_common()[:15]


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/2-creating-input-output-data.ipynb".
The first task is:
Lesson: Develop a Predictive Theory
Can you write Python code for it?


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/2-creating-input-output-data.ipynb".
The first task is:
Transforming Text into Numbers
Can you write Python code for it?

from IPython.display import Image

review = "This was a horrible, terrible movie."

Image(filename='sentiment_network.png')

review = "The movie was excellent"

Image(filename='sentiment_network_pos.png')


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/3-building-the-nn.ipynb".
The first task is:
Lesson: Develop a Predictive Theory
Can you write Python code for it?


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/3-building-the-nn.ipynb".
The first task is:
Project 1: Quick Theory Validation
Can you write Python code for it?

from collections import Counter
import numpy as np

positive_counts = Counter()
negative_counts = Counter()
total_counts = Counter()

for i in range(len(reviews)):
    if(labels[i] == 'POSITIVE'):
        for word in reviews[i].split(" "):
            positive_counts[word] += 1
            total_counts[word] += 1
    else:
        for word in reviews[i].split(" "):
            negative_counts[word] += 1
            total_counts[word] += 1

positive_counts.most_common()[:15]

pos_neg_ratios = Counter()

for term,cnt in list(total_counts.most_common()):
    if(cnt > 100):
        pos_neg_ratio = positive_counts[term] / float(negative_counts[term]+1)
        pos_neg_ratios[term] = pos_neg_ratio

for word,ratio in pos_neg_ratios.most_common():
    if(ratio > 1):
        pos_neg_ratios[word] = np.log(ratio)
    else:
        pos_neg_ratios[word] = -np.log((1 / (ratio+0.01)))

# words most frequently seen in a review with a "POSITIVE" label
pos_neg_ratios.most_common()[:15]

# words most frequently seen in a review with a "NEGATIVE" label
list(reversed(pos_neg_ratios.most_common()))[0:30]


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/3-building-the-nn.ipynb".
The first task is:
Project 2: Creating the Input/Output Data
Can you write Python code for it?

vocab = set(total_counts.keys())
vocab_size = len(vocab)
print(vocab_size)

list(vocab)[:15]

import numpy as np

layer_0 = np.zeros((1,vocab_size))
layer_0

from IPython.display import Image
Image(filename='sentiment_network.png')

word2index = {}

for i,word in enumerate(vocab):
    word2index[word] = i

word2index_sample = {k: word2index[k] for k in list(word2index.keys())[:15]}

word2index_sample

def update_input_layer(review):

    global layer_0

    # clear out previous state, reset the layer to be all 0s
    layer_0 *= 0
    for word in review.split(" "):
        layer_0[0][word2index[word]] += 1

update_input_layer(reviews[0])

layer_0

def get_target_for_label(label):
    if(label == 'POSITIVE'):
        return 1
    else:
        return 0

labels[0]

get_target_for_label(labels[0])

labels[1]

get_target_for_label(labels[1])


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/3-building-the-nn.ipynb".
The first task is:
Project 3: Building a Neural Network

Start with your neural network from the last chapter
3 layer neural network
no non-linearity in hidden layer
use our functions to create the training data
create a "pre_process_data" function to create vocabulary for our training data generating functions
modify "train" to train over the entire corpus

Where to Get Help if You Need it

Re-watch previous week's Udacity Lectures
Chapters 3-5 - Grokking Deep Learning - (40% Off: traskud17)
Can you write Python code for it?

import time
import sys
import numpy as np

class SentimentNetwork:
    def __init__(self, reviews, labels, hidden_nodes=10, learning_rate=0.1):

        # Set our random number generator
        np.random.seed(1)

        self.pre_process_data(reviews, labels)

        self.init_network(len(self.review_vocab), hidden_nodes, 1, learning_rate)

    def pre_process_data(self, reviews, labels):

        review_vocab = set()
        for review in reviews:
            for word in review.split(" "):
                review_vocab.add(word)
        self.review_vocab = list(review_vocab)

        label_vocab = set()
        for label in labels:
            label_vocab.add(label)
        self.label_vocab = list(label_vocab)

        self.review_vocab_size = len(self.review_vocab)
        self.label_vocab_size = len(self.label_vocab)

        self.word2index = {}
        for i, word in enumerate(self.review_vocab):
            self.word2index[word] = i

        self.label2index = {}
        for i, label in enumerate(self.label_vocab):
            self.label2index[label] = i

    def init_network(self, input_nodes, hidden_nodes, output_nodes, learning_rate):

        # Set number of nodes in input, hidden and output layers.
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes

        # Initialize weights
        self.weights_0_1 = np.zeros((self.input_nodes, self.hidden_nodes))

        self.weights_1_2 = np.random.normal(0.0, self.output_nodes**-0.5,
                                           (self.hidden_nodes, self.output_nodes))

        self.learning_rate = learning_rate

        self.layer_0 = np.zeros((1, input_nodes))

    def update_input_layer(self, review):

        # Clear out previous state, reset the layer to be all zeros
        self.layer_0 *= 0
        for word in review.split(" "):
      ...


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/3-building-the-nn.ipynb".
The first task is:
Transforming Text into Numbers
Can you write Python code for it?


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/4-reducing-noise.ipynb".
The first task is:
Lesson: Develop a Predictive Theory
Can you write Python code for it?


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/4-reducing-noise.ipynb".
The first task is:
Project 2: Creating the Input/Output Data
Can you write Python code for it?


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/4-reducing-noise.ipynb".
The first task is:
Project 3: Building a Neural Network

Start with your neural network from the last chapter
3 layer neural network
no non-linearity in hidden layer
use our functions to create the training data
create a "pre_process_data" function to create vocabulary for our training data generating functions
modify "train" to train over the entire corpus

Where to Get Help if You Need it

Re-watch previous week's Udacity Lectures
Chapters 3-5 - Grokking Deep Learning - (40% Off: traskud17)
Can you write Python code for it?

import time
import sys
import numpy as np

# Let's tweak our network from before to model these phenomena
class SentimentNetwork:
    def __init__(self, reviews,labels,hidden_nodes = 10, learning_rate = 0.1):

        # set our random number generator
        np.random.seed(1)

        self.pre_process_data(reviews, labels)

        self.init_network(len(self.review_vocab),hidden_nodes, 1, learning_rate)

    def pre_process_data(self, reviews, labels):

        review_vocab = set()
        for review in reviews:
            for word in review.split(" "):
                review_vocab.add(word)
        self.review_vocab = list(review_vocab)

        label_vocab = set()
        for label in labels:
            label_vocab.add(label)

        self.label_vocab = list(label_vocab)

        self.review_vocab_size = len(self.review_vocab)
        self.label_vocab_size = len(self.label_vocab)

        self.word2index = {}
        for i, word in enumerate(self.review_vocab):
            self.word2index[word] = i

        self.label2index = {}
        for i, label in enumerate(self.label_vocab):
            self.label2index[label] = i

    def init_network(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
        # Set number of nodes in input, hidden and output layers.
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes

        # Initialize weights
        self.weights_0_1 = np.zeros((self.input_nodes,self.hidden_nodes))

        self.weights_1_2 = np.random.normal(0.0, self.output_nodes**-0.5,
                                                (self.hidden_nodes, self.output_nodes))

        self.learning_rate = learning_rate

        self.layer_0 = np.zeros((1,input_nodes))

    def update_input_layer(self,review):

        # clear out previous state, reset the layer to be all 0s
        self.layer_0 *=...


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/4-reducing-noise.ipynb".
The first task is:
Transforming Text into Numbers
Can you write Python code for it?


I am working on the file "Sentiment Analysis/Sentiment Analysis with Andrew Trask/4-reducing-noise.ipynb".
The first task is:
Understanding Neural Noise
Can you write Python code for it?

from IPython.display import Image
Image(filename='sentiment_network.png')

def update_input_layer(review):

    global layer_0

    # clear out previous state, reset the layer to be all 0s
    layer_0 *= 0
    for word in review.split(" "):
        layer_0[0][word2index[word]] += 1

update_input_layer(reviews[0])

layer_0

review_counter = Counter()

for word in reviews[0].split(" "):
    review_counter[word] += 1

review_counter.most_common()[:20]


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
Intializing the model
Next we need to call the build_model() function to actually build the model. In my solution I haven't included any arguments to the function, but you can add arguments so you can change parameters in the model if you want.

Note: You might get a bunch of warnings here. TFLearn uses a lot of deprecated code in TensorFlow. Hopefully it gets updated to the new TensorFlow version soon.
Can you write Python code for it?

vocab_size = len(vocab)

model = build_model(vocab_size, learning_rate=0.1)


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
Let's keep the first 10000 most frequent words. As Andrew noted, most of the words in the vocabulary are rarely used so they will have little effect on our predictions. Below, we'll sort vocab by the count value and keep the 10000 most frequent words.
Can you write Python code for it?

vocab = sorted(total_counts, key=total_counts.get, reverse=True)[:10000]
print(vocab[:60])


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
Now, run through our entire review data set and convert each review to a word vector.
Can you write Python code for it?

word_vectors = np.zeros((len(reviews), len(vocab)), dtype=np.int_)
for ii, (_, text) in enumerate(reviews.iterrows()):
    word_vectors[ii] = text_to_vector(text[0])

# Printing out the first 5 word vectors
word_vectors[:5, :23]


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
Text to vector function
Now we can write a function that converts a some text to a word vector. The function will take a string of words as input and return a vector with the words counted up. Here's the general algorithm to do this:

Initialize the word vector with np.zeros, it should be the length of the vocabulary.
Split the input string of text into a list of words with .split(' ').
For each word in that list, increment the element in the index associated with that word, which you get from word2idx.

Note: Since all words aren't in the vocab dictionary, you'll get a key error if you run into one of those words. You can use the .get method of the word2idx dictionary to specify a default returned value when you make a key error. For example, word2idx.get(word, None) returns None if word doesn't exist in the dictionary.
Can you write Python code for it?

def text_to_vector(text):

    word_vec = np.zeros(len(vocab))

    for word in text.split(" "):
        idx = word2idx.get(word, None)
        if idx is None:
            continue
        else:
            word_vec[idx] += 1

    return np.array(word_vec)


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
The last word in our vocabulary shows up in 30 reviews out of 25000. I think it's fair to say this is a tiny proportion of reviews. We are probably fine with this number of words.
Now for each review in the data, we'll make a word vector. First we need to make a mapping of word to index, pretty easy to do with a dictionary comprehension.

Exercise: Create a dictionary called word2idx that maps each word in the vocabulary to an index. The first word in vocab has index 0, the second word has index 1, and so on.
Can you write Python code for it?

word2idx = {}

for i, word in enumerate(vocab):
    word2idx[word] = i


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
Training the network
Now that we've constructed the network, saved as the variable model, we can fit it to the data. Here we use the model.fit method. You pass in the training features trainX and the training targets trainY. Below I set validation_set=0.1 which reserves 10% of the data set as the validation set. You can also set the batch size and number of epochs with the batch_size and n_epoch keywords, respectively. Below is the code to fit our the network to our word vectors.
You can rerun model.fit to train the network further if you think you can increase the validation accuracy. Remember, all hyperparameter adjustments must be done using the validation set. Only use the test set after you're completely done training the network.
Can you write Python code for it?

# Training
model.fit(trainX, trainY, validation_set=0.1, show_metric=True, batch_size=128, n_epoch=10)


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
Try out your own text!
Can you write Python code for it?

text = "This movie is so bad. It was awful and the worst"
positive_prob = model.predict([text_to_vector(text.lower())])[0][1]
print('P(positive) = {:.3f} :'.format(positive_prob),
      'Positive' if positive_prob > 0.5 else 'Negative')


I am working on the file "Sentiment Analysis/Sentiment Analysis with TFLearn/sentiment-analysis-tflearn.ipynb".
The first task is:
What's the last word in our vocabulary? We can use this to judge if 10000 is too few. If the last word is pretty common, we probably need to keep more words.
Can you write Python code for it?

print(vocab[-1], ': ', total_counts[vocab[-1]])


I am working on the file "Sentiment Hackpad.ipynb".
The first task is:
Hackpad data
Read data
To do: Find a more automatic text-scraping method
Can you write Python code for it?

#textfile = "../hackpadtext_Wed.txt"
textfile = "../hackpadtext_Thu.txt"
#textfile = "../hackpadtext_Thu_active.txt"

rawdata = pd.read_csv(textfile, header=None, names=["text"], sep="\n", encoding="utf-8")


I am working on the file "Sentiment Hackpad.ipynb".
The first task is:
New dataset only includes hacks, not comments
Can you write Python code for it?

hackdata = rawdata[rawdata["mask"]]

#Top Five Sad Actually-Hacks (probably)
hackdata.sort_values("polarity")[:5]

# Top Five Happy Actually-Hacks (probably)
hackdata.sort_values("polarity")[::-1][:5]


I am working on the file "Sentiment Hackpad.ipynb".
The first task is:
Repeat analysis from earlier
Can you write Python code for it?

moarfeelings = []
for i in hackdata.index:
    data = hackdata.loc[i].values[0]
    polarity = textblob.TextBlob(data).sentiment.polarity
    moarfeelings.append(polarity)

average_feels = sum(moarfeelings)/len(moarfeelings)
print(average_feels)

if average_feels>0:
    print("YAY, WE'RE ACTUALLY HAPPY! wooooooooooo!")
else:
    print("oh no we're actually sad")


I am working on the file "Sentiment Hackpad.ipynb".
The first task is:
Test data
As a quick test, we feed some text into the textblob sentiment analyzer.
polarity can range from -1 to 1.
* -1 reflects extreme negative associations
* 1 reflects extreme positive associations
* 0 is neutral language
Can you write Python code for it?

textblob.TextBlob("Hello World I hate you").sentiment.polarity

textblob.TextBlob("Hello World I love you").sentiment.polarity


I am working on the file "Sentiment Hackpad.ipynb".
The first task is:
Who sounds sad?
Can you write Python code for it?

# search for sad hacks
rawdata[rawdata["polarity"]<0]

# search for happy hacks
#rawdata[rawdata["polarity"]>0]

# Top Five Happy Hacks!
rawdata.sort_values("polarity")[::-1][:5]


I am working on the file "Sentiment analysis (prediction).ipynb".
The first task is:
Benchmark my classifier on negative hashtag tweets
Can you write Python code for it?

negative_tweets = clf.predict(selector.transform(vect.transform(tweets.text[tweets.sentiment_hashtag==-1])))
negative_idx = tweets.text[tweets.sentiment_hashtag==-1].index

for i in range(1,5):
    j = random.randint(0,len(negative_idx))
    print tweets.ix[j,'NB_prob+'],tweets.text[j],'\n'

pd.Series(negative_tweets).value_counts()


I am working on the file "Sentiment analysis (prediction).ipynb".
The first task is:
Use externally trained model
IMPORTANT:
we should use the same vectorizer as in the trained instance of the classifier our training and prediction matrices have to be the same
Can you write Python code for it?

from nltk.stem.snowball import SnowballStemmer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
import random

def stemmed_words(doc):
    return (stemmer.stem(w) for w in analyzer(doc))

stemmer = SnowballStemmer('english')
analyzer = CountVectorizer().build_analyzer()


I am working on the file "Sentiment analysis (prediction).ipynb".
The first task is:
efforts with SentiStrength
Can you write Python code for it?

#SentiStrength
import subprocess
jar_path = "/home/antonis/sentistrength/SentiStrength.jar"
senti_data_path = "/home/antonis/sentistrength/SentiData/"

#define a SentiStrength function which takes a string or tokenized input and returns sentiment scores
## TODO : caution: may be slow! (using ''.join )
sample_text = 'this is something'
def SentiStrength(sample_text):
    '''Returns a list of [positive, negative] values'''
    if type(sample_text) is str:
        return subprocess.check_output(['java', '-jar', jar_path, 'sentidata', senti_data_path ,'text',sample_text]).split()
    else:
        return subprocess.check_output(['java', '-jar', jar_path, 'sentidata', senti_data_path ,'text','+'.join(sample_text)]).split()


I am working on the file "Sentiment analysis (prediction).ipynb".
The first task is:
export for each state
Can you write Python code for it?

from data.US_states import states

#initialize a df indexed by label values
state_sentiment = pd.DataFrame(index=tweets.label.unique())

for state in states.keys():
    state_sentiment[state] = tweets[tweets['state']==state]['label'].value_counts()
state_sentiment = state_sentiment.transpose()

state_sentiment.describe()

state_sentiment.head()

pickle.dump(state_sentiment,open('results/state_sentiment_0.5.pickle','wb'))


I am working on the file "Sentiment analysis (prediction).ipynb".
The first task is:
investigate the possibility of introducing a 'neutral' sentiment class
Can you write Python code for it?

sa_results = map(lambda x: 'positive' if x>0.7 else 'negative' if x<0.3 else 'neutral' , tweets['NB_prob+'])

sa_results = pd.Series(sa_results)

sa_results.value_counts().plot(kind='bar', title='# of tweets flagged in each sentiment class / at the 30-70% threshold')
plt.show()

tweets.head()


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Convert AFINN Sentiment Scores to a 1-to-5 Scale with 0.5 Increments
Can you write Python code for it?

OldMax = max(ThaiTextByUniqueUserBiz['AFINNSentScore'])
OldMin = min(ThaiTextByUniqueUserBiz['AFINNSentScore'])
NewMax = 5
NewMin = 1
OldRange = (OldMax - OldMin)
NewRange = (NewMax - NewMin)
for row in range(len(ThaiTextByUniqueUserBiz)):
    n = ThaiTextByUniqueUserBiz.loc[row, 'AFINNSentScore']
    ThaiTextByUniqueUserBiz.loc[row,"AFINNSentScore2"] = 0.5 * np.ceil(2*(((n - OldMin) * NewRange / OldRange) + NewMin))
ThaiTextByUniqueUserBiz


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Convert Yelp Review Sentiment Scores to a 1-to-5 Scale with 0.5 Increments
Can you write Python code for it?

OldMax = max(ThaiTextByUniqueUserBiz['YRsentiment'])
OldMin = min(ThaiTextByUniqueUserBiz['YRsentiment'])
NewMax = 5
NewMin = 1
OldRange = (OldMax - OldMin)
NewRange = (NewMax - NewMin)
for row in range(len(ThaiTextByUniqueUserBiz)):
    n = ThaiTextByUniqueUserBiz.loc[row, 'YRsentiment']
    ThaiTextByUniqueUserBiz.loc[row,"YRSentScore2"] = 0.5 * np.ceil(2*(((n - OldMin) * NewRange / OldRange) + NewMin))
ThaiTextByUniqueUserBiz


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Create Two Python Dictionaries with Sentiment Scores for the Sentiment Detection Algorithm
In this section, the objective of the code cells are (1) to identify the dictionary format used with the AFINN lexicon for the sentiment dectection algorithm and (2) the creation of a Python dictionary in the same format with the Yelp Restaurant Review Lexicon from the YelpSentimentAFFLEX dataframe. The Python dictionaries will be used in sentiment detection algorithms in the next section.
Create the AFINN dictionary
Can you write Python code for it?

sentiment_dictionary = {}
for line in open('AFINN-emoticons-en-165.txt'):
    word, score = line.split('\t')
    sentiment_dictionary[word] = int(score)
sentiment_dictionary


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Merge Ratings to Sentiment Scores in the ThaiTextByUniqueUserBiz Dataframe
Can you write Python code for it?

ThaiTextByUniqueUserBiz = pd.merge(ThaiTextByUniqueUserBiz, ThaiReviewRatingsByUserBiz, how='left', on=['user_id', 'business_id'])
ThaiTextByUniqueUserBiz


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Observations 1:
(1) The sentiment score results from the algorithm and the Yelp Restaurant Review Sentiment Lexicon range from -234 to 250.
(2) The sentiment score results from the algorithm and the two AFINN Lexicons combined range from -36 to 221.
Observations 2:
(1) Like the review_ratings, both sets of converted sentiment scores range from 1 to 5. The mean changes: it is the lowest at 3.0 with sentiment scores converted with no decimals, 3.3 with sentiment scores converted to a 0.5-increment scale and 3.9 with review ratings.
Perform a Count of Yelp Review Sentiment Scores Scaled to 1-to-5 with No Decimals and Compute the Median
Can you write Python code for it?


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Observations 3:
(1) The Yelp Restaurant Review Lexicon produced a range of values where 10,699 values out of 10,911 lie in the middle which happens to be the median. This median corresponds to a count of 0 in the initial score count; in other words, the median corresponds to an evenly balanced score of sentiments or a neutral sentiment, which is neither positive nor negative.
(2) This leaves us with 156 + 3 Thai restaurant reviews in the Yelp dataset (given reviewers of Phoenix restaurants) for recommendation purposes that have sentiments that are either positive or very positive. A pertinent question might be how many of these 159 positive-sentiment elements are reviews for Thai restaurants in Las Vegas.
Perform a Count of Yelp Review Sentiment Scores Scaled to 1-to-5 with 0.5 Increments and Compute the Median
Can you write Python code for it?


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Observations 4:
(1) This version of the Yelp Review Sentiment Scores Scaled 1-to-5 with 0.5 increments offers new potentials for a recommender system as 6,987 + 147 + 9 + 3 reviews fare positively. 3,712 reviews are neutral whereas 48 + 4 + 1 reviews raise a negative sentiment.
(2) The distribution is skewed to positive (although not quite as dramatically as the review_ratings (see below)).
(3) As there is no 1.5 score, we acknowledge that the one review with a score of 1 may be an outlier.
(4) The filter "0" applied to the initial score count without scaling ('0' is the neutral value in this column) suggests that the neutral value for the corresponding scaled value (without decimals) is 3 and that the neutral value for the corresponding scaled value using 0.5 increments is also 3.
(5) The median of 3.5 is different than the corresponding scaled value of 3 (using 0.5 increments) in item 4.
Perform a Count of AFINN Sentiment Scores Scaled to 1-to-5 with 0.5 Increments and Compute the Median
Can you write Python code for it?


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Observations 5:
(1) As the median is 2 (not 3 like what the results show from the Yelp Review Sentiment Score code cells above), the distribution is skewed to positive although 9,573 out of 10,911 reviews are neutral in sentiment.
(2) As there are no 4.0 and 4.5 scores, we acknowledge that the one review with a score of 5 may be an outlier.
(3) There are more neutral reviews from the computations with the AFINN Lexicon than there are with the Yelp Restaurant Review Lexicon in a 0.5 incremental scale.
(4) Only 952 + 54 + 2 + 1 reviews raise a positive sentiment.
(5) The filter "0" applied to the initial AFINN score count without scaling ('0' is the neutral value in this column) suggests  that the neutral value for the corresponding scaled value using 0.5 increments is 2. There are 252 reviews that correspond to a neutral position according to the AFINN Lexicon initial count whereas there is one only one review that corresponds to a neutral position according to the Yelp Restaurant Review Lexicon count. The latter has also a neutral value of 2 in the scaled AFINN column. Interestingly, the corresponding rating given by the user is "1" which is "strongly dislike".  The only word in that particular review is "closed" which likely means that it is not in the lexicons or that it is rated as neutral.
Perform a Count of Ratings Given by Individual Users and Compute the Median
Can you write Python code for it?


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Read and Merge the Thai Review Ratings file to the Sentiment Score Dataframe (ThaiTextByUniqueUserBiz)
Read the Thai Review Ratings File
Can you write Python code for it?

ThaiReviewRatingsByUserBiz = pd.read_pickle('ThaiReviewRatingsByUserBiz.pkl')
ThaiReviewRatingsByUserBiz


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Read the Yelp Restaurant Review Lexicon File
Although a bigram-based lexicon is available from the Yelp Restaurant Review Lexicon website file, we choose for this exercise the unigram-based lexicon for ease of comparison with the AFINN lexicon. We first assign column names to the four types of values found in the unigram-based text file although we will be using for this exercise the 'word' and 'score' values only.
Can you write Python code for it?

colnames = ['word', 'score', 'pos', 'neg']
YelpSentiment = pd.read_table("Yelp-restaurant-reviews-AFFLEX-NEGLEX-unigrams.txt", names=colnames)
YelpSentiment


I am working on the file "Sentiment-Detection/Scoring_Sentiments_in_Thai_Restaurant_Reviews_with_Lexicons.ipynb".
The first task is:
Save the Working Dataframe for Future Reference and for Recommender Activities
Can you write Python code for it?


I am working on the file "SentimentAnal.ipynb".
The first task is:
Compute the average sentiment.
Can you write Python code for it?

total_pos_score = sum(positivity)
total_neg_score = sum(negativity)
n_pos_score = len(positivity)
n_neg_score = len(negativity)

pos_avg = 0
if (n_pos_score > 0): pos_avg = total_pos_score/n_pos_score

neg_avg = 0
if (n_neg_score > 0): neg_avg = total_neg_score/n_neg_score

print n_pos_score, pos_avg

print n_neg_score, neg_avg


I am working on the file "SentimentAnal.ipynb".
The first task is:
Filter the ones with a sentiment score.
Can you write Python code for it?

positivity = [res[0] for res in sentiments if res[0] > 0]
negativity = [res[0] for res in sentiments if res[0] < 0]


I am working on the file "SentimentAnal.ipynb".
The first task is:
Search for a specific hashtag.
Can you write Python code for it?


I am working on the file "SentimentAnalysis.ipynb".
The first task is:
A cross-validaiton accuracy of 85.7% is not bad for this naive approach and a classifier that trains in only a few seconds. At this point you will be asking yourself, can this score be easily beaten with the use of a neural network. Let's see.
Neural Networks can do better
To train a neural network you should transform the data to a format the neural network can undestand. Hence, first you need to convert the reviews to numerical vectors. Let's assume the neural network would be only interested on the words: "breathtaking", "refreshing", "sucks" and "lame". Thus, we have an input vector of size 4. If the review does not contain any of these words the input vector would contain only zeros: [0, 0, 0, 0]. If the review is "Wow, that was such a refreshing experience. I was impreced by the breathtaking acting and the breathtaking visual effects.", the input vector would look like this: [1, 2, 0, 0]. A negative review such as "Wow, that was some lame acting and a lame music. Totally, lame. Sad." would be transformed to an input vector like this [0, 0, 0, 3]. Anyway, you need to create a word2index dictionary that points to an index of the vector for a given word:
Can you write Python code for it?

def create_word2index(min_occurance, sentiment_threshold):

    word2index = {}
    index = 0

    sentiment_ratio = Counter()
    for word, count in list(total_counts.most_common()):
        sentiment_ratio[word] = ((positive_counts[word] / total_counts[word]) - 0.5) / 0.5

    is_word_eligable = lambda word: word not in word2index and \
                total_counts[word] >= min_occurance and \
                abs(sentiment_ratio[word]) >= sentiment_threshold

    for i in range(len(reviews)):
        for word in reviews[i].split(" "):
            if is_word_eligable(word):
                word2index[word] = index
                index += 1

    print("Word2index contains", len(word2index), 'words.')
    return word2index


I am working on the file "SentimentAnalysis.ipynb".
The first task is:
And that is the beautiful normal destribution that I was expecting. The total word count shrinked from 74074 to 4276. Hence, there are many words that have been used only few times. Looking at the figure, there are a lot of neutral words in our new sentiment selection, but there are also some words that are used almost exclusively in POSITIVE or NEGATIVE reviews. You can try different values for 'min_occurance' and observe how the amount of total words and the plot is changing. Let's check out the words for min_occurance = 100.
Can you write Python code for it?

print('Words with the most POSITIVE sentiment' ,sentiment_ratio.most_common()[:30])
print('\nWords with the most NEGATIVE sentiment' ,sentiment_ratio.most_common()[-30:])


I am working on the file "SentimentAnalysis.ipynb".
The first task is:
At this point, you can transform both the reviews and the labels into data that the neural network can understand. Let's do that:
Can you write Python code for it?

word2index = create_word2index(min_occurance=10, sentiment_threshold=0.2)
encoded_reviews = encode_reviews_by_word_count(word2index)
encoded_labels = encode_labels()


I am working on the file "SentimentAnalysis.ipynb".
The first task is:
Labels are easily one-hot encoded. Check out this explanation on why one-hot encoding is needed:
Can you write Python code for it?

def encode_labels():
    encoded_labels = []
    for label in labels:
        if label == 'POSITIVE':
            encoded_labels.append([0, 1])
        else:
            encoded_labels.append([1, 0])
    return np.array(encoded_labels)


I am working on the file "SentimentAnalysis.ipynb".
The first task is:
The classifier has only two parameters - 'min_word_count' and 'sentiment_threshold'. A min_word_count of 20 means the classifier will only consider words that occur at least 20 times in the review corpus. The 'sentiment_threshhold' allows you to ignore words with rather neutral sentiment. A 'sentiment_threshhold' of 0.3 means that only words with sentiment ratio of more than 0.3 or less than -0.3 would be considered in the prediction process. What the classifier does is creating the sentiment ratio like previosly shown. When predicting the sentiment, the classifier uses the sentiment ratio dict to sum up all sentiment ratios of all the words used in the review. If the overall sum is positive the sentiment is also positive. If the overall sum is negative the sentiment is also negative. It is pretty simple, isn't it? Let's measure the performance in a 5 fold cross-validation setting:
Can you write Python code for it?

from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score

all_predictions = []
all_true_labels = []

for train_index, validation_index in KFold(n_splits=5, random_state=42, shuffle=True).split(labels):
    trainX, trainY = reviews[train_index], labels[train_index]
    validationX, validationY = reviews[validation_index], labels[validation_index]

    classifier = NaiveSentimentClassifier(20, 0.3)
    classifier.fit(trainX, trainY)

    predictions = classifier.predict(validationX)
    print('Fold accuracy', accuracy_score(validationY, predictions))
    all_predictions += predictions
    all_true_labels += list(validationY)

print('CV accuracy', accuracy_score(all_true_labels, all_predictions))


I am working on the file "SentimentAnalysis.ipynb".
The first task is:
The dataset is perfectly balanced across the two categories POSITIVE and NEGATIVE.
Counting words
Let's build up a simple sentiment theory. It is common sense that some of the words are more common in positive reviews and some are more frequently found in negative reviews. For example, I expect words like "supurb", "impresive", "magnificent", etc. to be common in positive reviews, while words like "miserable", "bad", "horrible", etc. to appear in negative reviews. Let's count the words in order to see what words are most common and what words appear most frequently the positive and the negative reviews.
Can you write Python code for it?

from collections import Counter

positive_counts = Counter()
negative_counts = Counter()
total_counts = Counter()

for i in range(len(reviews)):
    if(labels[i] == 'POSITIVE'):
        for word in reviews[i].split(" "):
            positive_counts[word] += 1
            total_counts[word] += 1
    else:
        for word in reviews[i].split(" "):
            negative_counts[word] += 1
            total_counts[word] += 1

# Examine the counts of the most common words in positive reviews
print('Most common words:', total_counts.most_common()[0:30])
print('\nMost common words in NEGATIVE reviews:', negative_counts.most_common()[0:30])
print('\nMost common words in POSITIVE reviews:', positive_counts.most_common()[0:30])


I am working on the file "SentimentAnalysis.ipynb".
The first task is:
Well, at a first glance, that seems dissapointing. As expected, the most common words are some linking words like "the", "of", "for", "at", etc. Counting the words for POSITIVE and NEGATIVE reviews separetely might appear pontless at first, as the same linking words are found among the most common for both the POSITIVE and NEGATIVE reviews.
Sentiment Ratio
However, counting the words that way would allow us to build a far more meaningful metric, called the sentiment ratio. A word with a sentiment ratio of 1 is used only in POSITIVE reviews. A word with a sentiment ratio of -1 is used only in NEGATIVE reviews. A word with sentiment ratio of 0 are neither POSITIVE nor NEGATIVE, but are neutral. Hence, linking words like the once shown above are expected to be close to the neutral 0. Let's draw the sentiment ratio for all words. I am expecting to see figure showing a beautiful normal distribution.
Can you write Python code for it?

import seaborn as sns

sentiment_ratio = Counter()
for word, count in list(total_counts.most_common()):
    sentiment_ratio[word] = ((positive_counts[word] / total_counts[word]) - 0.5) / 0.5
print('Total words in sentiment ratio', len(sentiment_ratio))
sns.distplot(list(sentiment_ratio.values()));


I am working on the file "Seq-to-Seq/Basic-Seq2Seq/basic sequence-to-sequence.ipynb".
The first task is:
As i expected for the loss slope, now let we test our model for 10 sentences from the input dataset
Can you write Python code for it?

def label_to_text(label):
    string = ''
    for i in xrange(len(label)):
        if label[i] == 0 or label[i] == 1:
            continue
        string += vocab_predict[label[i]] + ' '
    return string

for i in xrange(10):
    rand = np.random.randint(len(split_inputs))
    in_testing = feeding(split_inputs[rand: rand + 1], split_predict[rand: rand + 1])
    predict = sess.run(decoder_prediction, in_testing)
    print 'input: ' + str(in_testing[encoder_inputs].T)
    print 'supposed label: ' + str(in_testing[decoder_targets].T)
    print 'predict label:' + str(predict.T)
    print 'predict text: ' + str(label_to_text(predict.T[0])) + '\n'


I am working on the file "Seq-to-Seq/Basic-Seq2Seq/basic sequence-to-sequence.ipynb".
The first task is:
Decoder part
Can you write Python code for it?

# RNN size of greatestvalue_predict
decoder_cell = tf.nn.rnn_cell.LSTMCell(greatestvalue_predict)

# 2 layers of RNN
decoder_rnn_cells = tf.nn.rnn_cell.MultiRNNCell([decoder_cell] * 2)

# declare a scope for our decoder, later tensorflow will confuse
decoder_outputs, decoder_final_state = tf.nn.dynamic_rnn(decoder_rnn_cells, decoder_inputs_embedded,
                                                         initial_state = encoder_final_state,
                                           dtype = tf.float32, time_major = True, scope ='decoder')

decoder_logits = tf.contrib.layers.linear(decoder_outputs, len(vocab_predict))

decoder_prediction = tf.argmax(decoder_logits, 2)

# this might very costly if you have very large vocab
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
    labels = tf.one_hot(decoder_targets, depth = len(vocab_predict), dtype = tf.float32),
    logits = decoder_logits)

loss = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)

sess.run(tf.global_variables_initializer())


I am working on the file "Seq-to-Seq/Basic-Seq2Seq/basic sequence-to-sequence.ipynb".
The first task is:
Encoder part
Can you write Python code for it?

# RNN size of greatestvalue_inputs
encoder_cell = tf.nn.rnn_cell.LSTMCell(greatestvalue_predict)

# 2 layers of RNN
encoder_rnn_cells = tf.nn.rnn_cell.MultiRNNCell([encoder_cell] * 2)

_, encoder_final_state = tf.nn.dynamic_rnn(encoder_rnn_cells, encoder_inputs_embedded,
                                           dtype = tf.float32, time_major = True)


I am working on the file "Seq-to-Seq/Basic-Seq2Seq/basic sequence-to-sequence.ipynb".
The first task is:
Now we import our tensorflow and design our dynamic rnn
I'm still using Tensorflow 0.12 and will use most of the API from Tensorflow to make it short.
0.12, API for LSTM tf.nn.rnn_cell.LSTMCell
1.X, API for tf.contrib.rnn.LSTMCell
Can you write Python code for it?

import numpy as np
import tensorflow as tf
import helpers
sess = tf.InteractiveSession()

encoder_inputs = tf.placeholder(shape = (None, None), dtype = tf.int32)
decoder_targets = tf.placeholder(shape = (None, None), dtype = tf.int32)
decoder_inputs = tf.placeholder(shape = (None, None), dtype = tf.int32)

encoder_embeddings = tf.Variable(tf.random_uniform([len(vocab_inputs), greatestvalue_predict]
                                                   , -1.0, 1.0), dtype = tf.float32)

decoder_embeddings = tf.Variable(tf.random_uniform([len(vocab_predict), greatestvalue_predict]
                                                   , -1.0, 1.0), dtype = tf.float32)

encoder_inputs_embedded = tf.nn.embedding_lookup(encoder_embeddings, encoder_inputs)
decoder_inputs_embedded = tf.nn.embedding_lookup(decoder_embeddings, decoder_inputs)


I am working on the file "Seq-to-Seq/Basic-Seq2Seq/basic sequence-to-sequence.ipynb".
The first task is:
Now we need vocabulary and dictionary for our input and predict.
step:
1- We split the dataset into input and predict. Even line is predict, else is input.
2- Then we tokenized each sentence in both dataset, turn into vocabulary.
['saya suka makan nasi goreng', 'makan mee udang']
[['saya', 'suka'.. n], ['makan', 'mee', 'udang']]
3- Then we sorted our tokenized words from highest freq to lowest freq.
4- Then we turned our sorted words into unique words, while maintaining the position of sorting.
5- Then turned into dictionary {'husein': 0, 'suka': 1.. n}
Can you write Python code for it?

inputs = []; predict = []

# We split the dataset into input and predict. Even line is predict, else is input.
for i in xrange(len(text)):
    if i % 2 == 0:
        predict.append(text[i])
    else:
        inputs.append(text[i])

vocab_inputs = []; vocab_predict = []
# Then we tokenized each sentence in both dataset, turn into vocabulary.
for i in xrange(len(inputs)):
    vocab_inputs += inputs[i].split(); vocab_predict += predict[i].split()

# Then we sorted our tokenized words from highest freq to lowest freq.
vocab_inputs = sorted(vocab_inputs,key = vocab_inputs.count,reverse = True)
vocab_predict = sorted(vocab_predict,key = vocab_predict.count,reverse = True)

d1 = dict((k,v) for v,k in enumerate(reversed(vocab_inputs)))
d2 = dict((k,v) for v,k in enumerate(reversed(vocab_predict)))

# Then we turned our sorted words into unique words, while maintaining the position of sorting.
vocab_inputs = ['PAD', 'EOS'] + sorted(d1, key = d1.get, reverse = True)
vocab_predict = ['PAD', 'EOS'] + sorted(d2, key = d2.get, reverse = True)

print 'vocab size for inputs: ' + str(len(vocab_inputs))
print 'vocab size for predict: ' + str(len(vocab_predict))

# Then turned into dictionary {'husein': 0, 'suka': 1.. n}
dict_inputs = dict(zip(vocab_inputs, [i for i in xrange(len(vocab_inputs))]))
dict_predict = dict(zip(vocab_predict, [i for i in xrange(len(vocab_predict))]))

split_inputs = []; split_predict = []
for i in xrange(len(inputs)):
    split_inputs.append(inputs[i].split()); split_predict.append(predict[i].split())

greatestvalue_inputs = 0; greatestvalue_predict = 0
for i in xrange(len(split_inputs)):
    if len(split_inputs[i]) > greatestvalue_inputs:
        greatestvalue_inputs = len(split_inputs[i])

for i in xrange(len(split_predict)):
    if len(split_predict[i]) > greatestvalue_predict:
        greatestvalue_predict = len(split_predict[i])

# need to add one because our decoder need to include EOS
greatestvalue_predict += 1

print 'longest sentence in our input dataset: ' +...


I am working on the file "Sequence Models/Dinosaurus+Island+--+Character+level+language+model+final+-+v3.ipynb".
The first task is:
1.2 - Overview of the model
Your model will have the following structure:

Initialize parameters
Run the optimization loop
Forward propagation to compute the loss function
Backward propagation to compute the gradients with respect to the loss function
Clip the gradients to avoid exploding gradients
Using the gradients, update your parameter with the gradient descent update rule.

Return the learned parameters

 Figure 1: Recurrent Neural Network, similar to what you had built in the previous notebook "Building a RNN - Step by Step".

At each time-step, the RNN tries to predict what is the next character given the previous characters. The dataset $X = (x^{\langle 1 \rangle}, x^{\langle 2 \rangle}, ..., x^{\langle T_x \rangle})$ is a list of characters in the training set, while $Y = (y^{\langle 1 \rangle}, y^{\langle 2 \rangle}, ..., y^{\langle T_x \rangle})$ is such that at every time-step $t$, we have $y^{\langle t \rangle} = x^{\langle t+1 \rangle}$.
2 - Building blocks of the model
In this part, you will build two important blocks of the overall model:
- Gradient clipping: to avoid exploding gradients
- Sampling: a technique used to generate characters
You will then apply these two functions to build the model.
2.1 - Clipping the gradients in the optimization loop
In this section you will implement the clip function that you will call inside of your optimization loop. Recall that your overall loop structure usually consists of a forward pass, a cost computation, a backward pass, and a parameter update. Before updating the parameters, you will perform gradient clipping when needed to make sure that your gradients are not "exploding," meaning taking on overly large values.
In the exercise below, you will implement a function clip that takes in a dictionary of gradients and returns a clipped version of gradients if needed. There are different ways to clip gradients; we will use a simple element-wise clipping...
Can you write Python code for it?

### GRADED FUNCTION: clip

def clip(gradients, maxValue):
    '''
    Clips the gradients' values between minimum and maximum.

    Arguments:
    gradients -- a dictionary containing the gradients "dWaa", "dWax", "dWya", "db", "dby"
    maxValue -- everything above this number is set to this number, and everything less than -maxValue is set to -maxValue

    Returns:
    gradients -- a dictionary with the clipped gradients.
    '''

    dWaa, dWax, dWya, db, dby = gradients['dWaa'], gradients['dWax'], gradients['dWya'], gradients['db'], gradients['dby']

    ### START CODE HERE ###
    # clip to mitigate exploding gradients, loop over [dWax, dWaa, dWya, db, dby]. (≈2 lines)
    for gradient in [dWax, dWaa, dWya, db, dby]:
        np.clip(gradient, -maxValue, maxValue, out=gradient)
    ### END CODE HERE ###

    gradients = {"dWaa": dWaa, "dWax": dWax, "dWya": dWya, "db": db, "dby": dby}

    return gradients

np.random.seed(3)
dWax = np.random.randn(5,3)*10
dWaa = np.random.randn(5,5)*10
dWya = np.random.randn(2,5)*10
db = np.random.randn(5,1)*10
dby = np.random.randn(2,1)*10
gradients = {"dWax": dWax, "dWaa": dWaa, "dWya": dWya, "db": db, "dby": dby}
gradients = clip(gradients, 10)
print("gradients[\"dWaa\"][1][2] =", gradients["dWaa"][1][2])
print("gradients[\"dWax\"][3][1] =", gradients["dWax"][3][1])
print("gradients[\"dWya\"][1][2] =", gradients["dWya"][1][2])
print("gradients[\"db\"][4] =", gradients["db"][4])
print("gradients[\"dby\"][1] =", gradients["dby"][1])


I am working on the file "Sequence Models/Dinosaurus+Island+--+Character+level+language+model+final+-+v3.ipynb".
The first task is:
Conclusion
You can see that your algorithm has started to generate plausible dinosaur names towards the end of the training. At first, it was generating random characters, but towards the end you could see dinosaur names with cool endings. Feel free to run the algorithm even longer and play with hyperparameters to see if you can get even better results. Our implemetation generated some really cool names like maconucon, marloralus and macingsersaurus. Your model hopefully also learned that dinosaur names tend to end in saurus, don, aura, tor, etc.
If your model generates some non-cool names, don't blame the model entirely--not all actual dinosaur names sound cool. (For example, dromaeosauroides is an actual dinosaur name and is in the training set.) But this model should give you a set of candidates from which you can pick the coolest!
This assignment had used a relatively small dataset, so that you could train an RNN quickly on a CPU. Training a model of the english language requires a much bigger dataset, and usually needs much more computation, and could run for many hours on GPUs. We ran our dinosaur name for quite some time, and so far our favoriate name is the great, undefeatable, and fierce: Mangosaurus!

4 - Writing like Shakespeare
The rest of this notebook is optional and is not graded, but we hope you'll do it anyway since it's quite fun and informative.
A similar (but more complicated) task is to generate Shakespeare poems. Instead of learning from a dataset of Dinosaur names you can use a collection of Shakespearian poems. Using LSTM cells, you can learn longer term dependencies that span many characters in the text--e.g., where a character appearing somewhere a sequence can influence what should be a different character much much later in ths sequence. These long term dependencies were less important with dinosaur names, since the names were quite short.
<img src="images/shakespeare.jpg"...
Can you write Python code for it?

from __future__ import print_function
from keras.callbacks import LambdaCallback
from keras.models import Model, load_model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, Masking
from keras.layers import LSTM
from keras.utils.data_utils import get_file
from keras.preprocessing.sequence import pad_sequences
from shakespeare_utils import *
import sys
import io


I am working on the file "Sequence Models/Dinosaurus+Island+--+Character+level+language+model+final+-+v3.ipynb".
The first task is:
Expected output:

    **Loss **

    126.503975722

    **gradients["dWaa"][1][2]**

    0.194709315347

    **np.argmax(gradients["dWax"])**

     93

    **gradients["dWya"][1][2]**

     -0.007773876032

    **gradients["db"][4]**

     [-0.06809825]

    **gradients["dby"][1]**

    [ 0.01538192]

    **a_last[4]**

     [-1.]

3.2 - Training the model
Given the dataset of dinosaur names, we use each line of the dataset (one name) as one training example. Every 100 steps of stochastic gradient descent, you will sample 10 randomly chosen names to see how the algorithm is doing. Remember to shuffle the dataset, so that stochastic gradient descent visits the examples in random order.
Exercise: Follow the instructions and implement model(). When examples[index] contains one dinosaur name (string), to create an example (X, Y), you can use this:
python
        index = j % len(examples)
        X = [None] + [char_to_ix[ch] for ch in examples[index]]
        Y = X[1:] + [char_to_ix["\n"]]
Note that we use: index= j % len(examples), where j = 1....num_iterations, to make sure that examples[index] is always a valid statement (index is smaller than len(examples)).
The first entry of X being None will be interpreted by rnn_forward() as setting $x^{\langle 0 \rangle} = \vec{0}$. Further, this ensures that Y is equal to X but shifted one step to the left, and with an additional "\n" appended to signify the end of the dinosaur name.
Can you write Python code for it?

# GRADED FUNCTION: model

def model(data, ix_to_char, char_to_ix, num_iterations = 35000, n_a = 50, dino_names = 7, vocab_size = 27):
    """
    Trains the model and generates dinosaur names.

    Arguments:
    data -- text corpus
    ix_to_char -- dictionary that maps the index to a character
    char_to_ix -- dictionary that maps a character to an index
    num_iterations -- number of iterations to train the model for
    n_a -- number of units of the RNN cell
    dino_names -- number of dinosaur names you want to sample at each iteration.
    vocab_size -- number of unique characters found in the text, size of the vocabulary

    Returns:
    parameters -- learned parameters
    """

    # Retrieve n_x and n_y from vocab_size
    n_x, n_y = vocab_size, vocab_size

    # Initialize parameters
    parameters = initialize_parameters(n_a, n_x, n_y)

    # Initialize loss (this is required because we want to smooth our loss, don't worry about it)
    loss = get_initial_loss(vocab_size, dino_names)

    # Build list of all dinosaur names (training examples).
    with open("dinos.txt") as f:
        examples = f.readlines()
    examples = [x.lower().strip() for x in examples]

    # Shuffle list of all dinosaur names
    np.random.seed(0)
    np.random.shuffle(examples)

    # Initialize the hidden state of your LSTM
    a_prev = np.zeros((n_a, 1))

    # Optimization loop
    for j in range(1,num_iterations+1):

        ### START CODE HERE ###

        # Use the hint above to define one training example (X,Y) (≈ 2 lines)
        index = j % len(examples)
        X = [None] + [char_to_ix[ch] for ch in examples[index]]
        Y = X[1:] + [char_to_ix["\n"]]

        # Perform one optimization step: Forward-prop -> Backward-prop -> Clip -> Update parameters
        # Choose a learning rate of 0.01
        curr_loss, gradients, a_prev = optimize(X, Y, a_prev, parameters, learning_rate = 0.01)

        ### END CODE HERE ###

        #...


I am working on the file "Sequence Models/Dinosaurus+Island+--+Character+level+language+model+final+-+v3.ipynb".
The first task is:
Expected output:

    **list of sampled indices:**

    [12, 17, 24, 14, 13, 9, 10, 22, 24, 6, 13, 11, 12, 6, 21, 15, 21, 14, 3, 2, 1, 21, 18, 24,

    7, 25, 6, 25, 18, 10, 16, 2, 3, 8, 15, 12, 11, 7, 1, 12, 10, 2, 7, 7, 11, 5, 6, 12, 25, 0, 0]

    **list of sampled characters:**

    ['l', 'q', 'x', 'n', 'm', 'i', 'j', 'v', 'x', 'f', 'm', 'k', 'l', 'f', 'u', 'o',

    'u', 'n', 'c', 'b', 'a', 'u', 'r', 'x', 'g', 'y', 'f', 'y', 'r', 'j', 'p', 'b', 'c', 'h', 'o',

    'l', 'k', 'g', 'a', 'l', 'j', 'b', 'g', 'g', 'k', 'e', 'f', 'l', 'y', '\n', '\n']

3 - Building the language model
It is time to build the character-level language model for text generation.
3.1 - Gradient descent
In this section you will implement a function performing one step of stochastic gradient descent (with clipped gradients). You will go through the training examples one at a time, so the optimization algorithm will be stochastic gradient descent. As a reminder, here are the steps of a common optimization loop for an RNN:

Forward propagate through the RNN to compute the loss
Backward propagate through time to compute the gradients of the loss with respect to the parameters
Clip the gradients if necessary
Update your parameters using gradient descent

Exercise: Implement this optimization process (one step of stochastic gradient descent).
We provide you with the following functions:
```python
def rnn_forward(X, Y, a_prev, parameters):
    """ Performs the forward propagation through the RNN and computes the cross-entropy loss.
    It returns the loss' value as well as a "cache" storing values to be used in the backpropagation."""
    ....
    return loss, cache
def rnn_backward(X, Y, parameters, cache):
    """ Performs the backward propagation through time to compute the gradients of the loss with respect
    to the parameters. It returns also all the hidden states."""
    ...
    return gradients,...
Can you write Python code for it?

# GRADED FUNCTION: optimize

def optimize(X, Y, a_prev, parameters, learning_rate = 0.01):
    """
    Execute one step of the optimization to train the model.

    Arguments:
    X -- list of integers, where each integer is a number that maps to a character in the vocabulary.
    Y -- list of integers, exactly the same as X but shifted one index to the left.
    a_prev -- previous hidden state.
    parameters -- python dictionary containing:
                        Wax -- Weight matrix multiplying the input, numpy array of shape (n_a, n_x)
                        Waa -- Weight matrix multiplying the hidden state, numpy array of shape (n_a, n_a)
                        Wya -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)
                        b --  Bias, numpy array of shape (n_a, 1)
                        by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)
    learning_rate -- learning rate for the model.

    Returns:
    loss -- value of the loss function (cross-entropy)
    gradients -- python dictionary containing:
                        dWax -- Gradients of input-to-hidden weights, of shape (n_a, n_x)
                        dWaa -- Gradients of hidden-to-hidden weights, of shape (n_a, n_a)
                        dWya -- Gradients of hidden-to-output weights, of shape (n_y, n_a)
                        db -- Gradients of bias vector, of shape (n_a, 1)
                        dby -- Gradients of output bias vector, of shape (n_y, 1)
    a[len(X)-1] -- the last hidden state, of shape (n_a, 1)
    """

    ### START CODE HERE ###

    # Forward propagate through time (≈1 line)
    loss, cache = rnn_forward(X, Y, a_prev, parameters)

    # Backpropagate through time (≈1 line)
    gradients, a = rnn_backward(X, Y, parameters, cache)

    # Clip your gradients between -5 (min) and 5 (max) (≈1 line)
    gradients = clip(gradients=gradients,maxValue=5)

    # Update parameters (≈1 line)
    parameters =...


I am working on the file "Sequence Models/Dinosaurus+Island+--+Character+level+language+model+final+-+v3.ipynb".
The first task is:
Run the following cell, you should observe your model outputting random-looking characters at the first iteration. After a few thousand iterations, your model should learn to generate reasonable-looking names.
Can you write Python code for it?

parameters = model(data, ix_to_char, char_to_ix)


I am working on the file "Sequence Models/Dinosaurus+Island+--+Character+level+language+model+final+-+v3.ipynb".
The first task is:
The characters are a-z (26 characters) plus the "\n" (or newline character), which in this assignment plays a role similar to the <EOS> (or "End of sentence") token we had discussed in lecture, only here it indicates the end of the dinosaur name rather than the end of a sentence. In the cell below, we create a python dictionary (i.e., a hash table) to map each character to an index from 0-26. We also create a second python dictionary that maps each index back to the corresponding character character. This will help you figure out what index corresponds to what character in the probability distribution output of the softmax layer. Below, char_to_ix and ix_to_char are the python dictionaries.
Can you write Python code for it?

char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) }
ix_to_char = { i:ch for i,ch in enumerate(sorted(chars)) }
print(ix_to_char)


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
1 - Baseline model: Emojifier-V1
1.1 - Dataset EMOJISET
Let's start by building a simple baseline classifier.
You have a tiny dataset (X, Y) where:
- X contains 127 sentences (strings)
- Y contains a integer label between 0 and 4 corresponding to an emoji for each sentence

 Figure 1: EMOJISET - a classification problem with 5 classes. A few examples of sentences are given here.

Let's load the dataset using the code below. We split the dataset between training (127 examples) and testing (56 examples).
Can you write Python code for it?

X_train, Y_train = read_csv('data/train_emoji.csv')
X_test, Y_test = read_csv('data/tesss.csv')

maxLen = len(max(X_train, key=len).split())


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
2.1 - Overview of the model
Here is the Emojifier-v2 you will implement:

 Figure 3: Emojifier-V2. A 2-layer LSTM sequence classifier.

2.2 Keras and mini-batching
In this exercise, we want to train Keras using mini-batches. However, most deep learning frameworks require that all sequences in the same mini-batch have the same length. This is what allows vectorization to work: If you had a 3-word sentence and a 4-word sentence, then the computations needed for them are different (one takes 3 steps of an LSTM, one takes 4 steps) so it's just not possible to do them both at the same time.
The common solution to this is to use padding. Specifically, set a maximum sequence length, and pad all sequences to the same length. For example, of the maximum sequence length is 20, we could pad every sentence with "0"s so that each input sentence is of length 20. Thus, a sentence "i love you" would be represented as $(e_{i}, e_{love}, e_{you}, \vec{0}, \vec{0}, \ldots, \vec{0})$. In this example, any sentences longer than 20 words would have to be truncated. One simple way to choose the maximum sequence length is to just pick the length of the longest sentence in the training set.
2.3 - The Embedding layer
In Keras, the embedding matrix is represented as a "layer", and maps positive integers (indices corresponding to words) into dense vectors of fixed size (the embedding vectors). It can be trained or initialized with a pretrained embedding. In this part, you will learn how to create an Embedding() layer in Keras, initialize it with the GloVe 50-dimensional vectors loaded earlier in the notebook. Because our training set is quite small, we will not update the word embeddings but will instead leave their values fixed. But in the code below, we'll show you how Keras allows you to either train or leave fixed this layer.
The Embedding() layer takes an integer matrix of size (batch size, max input length) as input....
Can you write Python code for it?

# GRADED FUNCTION: sentences_to_indices

def sentences_to_indices(X, word_to_index, max_len):
    """
    Converts an array of sentences (strings) into an array of indices corresponding to words in the sentences.
    The output shape should be such that it can be given to `Embedding()` (described in Figure 4).

    Arguments:
    X -- array of sentences (strings), of shape (m, 1)
    word_to_index -- a dictionary containing the each word mapped to its index
    max_len -- maximum number of words in a sentence. You can assume every sentence in X is no longer than this.

    Returns:
    X_indices -- array of indices corresponding to words in the sentences from X, of shape (m, max_len)
    """

    m = X.shape[0]                                   # number of training examples

    ### START CODE HERE ###
    # Initialize X_indices as a numpy matrix of zeros and the correct shape (≈ 1 line)
    X_indices = np.zeros([m,max_len])

    for i in range(m):                               # loop over training examples

        # Convert the ith training sentence in lower case and split is into words. You should get a list of words.
        sentence_words = [x.lower() for x in X[i].split()]

        # Initialize j to 0
        j = 0

        # Loop over the words of sentence_words
        for w in range(len(sentence_words)):
            # Set the (i,j)th entry of X_indices to the index of the correct word.
            X_indices[i, j] = word_to_index[sentence_words[j]]
            # Increment j to j + 1
            j +=1

    ### END CODE HERE ###

    return X_indices


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
All the data is now ready to be fed into the Emojify-V1 model. Let's implement the model!
1.3 - Implementing Emojifier-V1
As shown in Figure (2), the first step is to convert an input sentence into the word vector representation, which then get averaged together. Similar to the previous exercise, we will use pretrained 50-dimensional GloVe embeddings. Run the following cell to load the word_to_vec_map, which contains all the vector representations.
Can you write Python code for it?

word_to_index, index_to_word, word_to_vec_map = read_glove_vecs('data/glove.6B.50d.txt')


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Amazing! Because adore has a similar embedding as love, the algorithm has generalized correctly even to a word it has never seen before. Words such as heart, dear, beloved or adore have embedding vectors similar to love, and so might work too---feel free to modify the inputs above and try out a variety of input sentences. How well does it work?
Note though that it doesn't get "not feeling happy" correct. This algorithm ignores word ordering, so is not good at understanding phrases like "not happy."
Printing the confusion matrix can also help understand which classes are more difficult for your model. A confusion matrix shows how often an example whose label is one class ("actual" class) is mislabeled by the algorithm with a different class ("predicted" class).
Can you write Python code for it?

print(Y_test.shape)
print('           '+ label_to_emoji(0)+ '    ' + label_to_emoji(1) + '    ' +  label_to_emoji(2)+ '    ' + label_to_emoji(3)+'   ' + label_to_emoji(4))
print(pd.crosstab(Y_test, pred_test.reshape(56,), rownames=['Actual'], colnames=['Predicted'], margins=True))
plot_confusion_matrix(Y_test, pred_test)


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
As usual, after creating your model in Keras, you need to compile it and define what loss, optimizer and metrics your are want to use. Compile your model using categorical_crossentropy loss, adam optimizer and ['accuracy'] metrics:
Can you write Python code for it?


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Expected Output (on a subset of iterations):

            **Epoch: 0**

           cost = 1.95204988128

           Accuracy: 0.348484848485

            **Epoch: 100**

           cost = 0.0797181872601

           Accuracy: 0.931818181818

            **Epoch: 200**

           cost = 0.0445636924368

           Accuracy: 0.954545454545

            **Epoch: 300**

           cost = 0.0343226737879

           Accuracy: 0.969696969697

Great! Your model has pretty high accuracy on the training set. Lets now see how it does on the test set.
1.4 - Examining test set performance
Can you write Python code for it?

print("Training set:")
pred_train = predict(X_train, Y_train, W, b, word_to_vec_map)
print('Test set:')
pred_test = predict(X_test, Y_test, W, b, word_to_vec_map)


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Expected Output:

            **Train set accuracy**

           97.7

            **Test set accuracy**

           85.7

Random guessing would have had 20% accuracy given that there are 5 classes. This is pretty good performance after training on only 127 examples.
In the training set, the algorithm saw the sentence "I love you" with the label ❤. You can check however that the word "adore" does not appear in the training set. Nonetheless, lets see what happens if you write "I adore you."
Can you write Python code for it?

X_my_sentences = np.array(["i adore you", "i love you", "funny lol", "lets play with a ball", "food is ready", "not feeling happy"])
Y_my_labels = np.array([[0], [0], [2], [1], [4],[3]])

pred = predict(X_my_sentences, Y_my_labels , W, b, word_to_vec_map)
print_predictions(X_my_sentences, pred)


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Expected Output:

            **X1 =**

           ['funny lol' 'lets play football' 'food is ready for you']

            **X1_indices =**

           [[ 155345.  225122.       0.       0.       0.]

            [ 220930.  286375.  151266.       0.       0.]

            [ 151204.  192973.  302254.  151349.  394475.]]

Let's build the Embedding() layer in Keras, using pre-trained word vectors. After this layer is built, you will pass the output of sentences_to_indices() to it as an input, and the Embedding() layer will return the word embeddings for a sentence.
Exercise: Implement pretrained_embedding_layer(). You will need to carry out the following steps:
1. Initialize the embedding matrix as a numpy array of zeroes with the correct shape.
2. Fill in the embedding matrix with all the word embeddings extracted from word_to_vec_map.
3. Define Keras embedding layer. Use Embedding(). Be sure to make this layer non-trainable, by setting trainable = False when calling Embedding(). If you were to set trainable = True, then it will allow the optimization algorithm to modify the values of the word embeddings.
4. Set the embedding weights to be equal to the embedding matrix
Can you write Python code for it?

# GRADED FUNCTION: pretrained_embedding_layer

def pretrained_embedding_layer(word_to_vec_map, word_to_index):
    """
    Creates a Keras Embedding() layer and loads in pre-trained GloVe 50-dimensional vectors.

    Arguments:
    word_to_vec_map -- dictionary mapping words to their GloVe vector representation.
    word_to_index -- dictionary mapping from words to their indices in the vocabulary (400,001 words)

    Returns:
    embedding_layer -- pretrained layer Keras instance
    """

    vocab_len = len(word_to_index) + 1                  # adding 1 to fit Keras embedding (requirement)
    emb_dim = word_to_vec_map["cucumber"].shape[0]      # define dimensionality of your GloVe word vectors (= 50)

    ### START CODE HERE ###
    # Initialize the embedding matrix as a numpy array of zeros of shape (vocab_len, dimensions of word vectors = emb_dim)
    emb_matrix = np.zeros([vocab_len, emb_dim])

    # Set each row "index" of the embedding matrix to be the word vector representation of the "index"th word of the vocabulary
    for word, index in word_to_index.items():
        emb_matrix[index, :] = word_to_vec_map[word]

    # Define Keras embedding layer with the correct output/input sizes, make it trainable. Use Embedding(...). Make sure to set trainable=False.
    embedding_layer = Embedding(input_dim=vocab_len,output_dim=emb_dim, trainable = False)
    ### END CODE HERE ###

    # Build the embedding layer, it is required before setting the weights of the embedding layer. Do not modify the "None".
    embedding_layer.build((None,))

    # Set the weights of the embedding layer to the embedding matrix. Your layer is now pretrained.
    embedding_layer.set_weights([emb_matrix])

    return embedding_layer

embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index)
print("weights[0][1][3] =", embedding_layer.get_weights()[0][1][3])


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Expected Output:

            **avg= **

           [-0.008005    0.56370833 -0.50427333  0.258865    0.55131103  0.03104983
 -0.21013718  0.16893933 -0.09590267  0.141784   -0.15708967  0.18525867
  0.6495785   0.38371117  0.21102167  0.11301667  0.02613967  0.26037767
  0.05820667 -0.01578167 -0.12078833 -0.02471267  0.4128455   0.5152061
  0.38756167 -0.898661   -0.535145    0.33501167  0.68806933 -0.2156265
  1.797155    0.10476933 -0.36775333  0.750785    0.10282583  0.348925
 -0.27262833  0.66768    -0.10706167 -0.283635    0.59580117  0.28747333
 -0.3366635   0.23393817  0.34349183  0.178405    0.1166155  -0.076433
  0.1445417   0.09808667]

Model
You now have all the pieces to finish implementing the model() function. After using sentence_to_avg() you need to pass the average through forward propagation, compute the cost, and then backpropagate to update the softmax's parameters.
Exercise: Implement the model() function described in Figure (2). Assuming here that $Yoh$ ("Y one hot") is the one-hot encoding of the output labels, the equations you need to implement in the forward pass and to compute the cross-entropy cost are:
$$ z^{(i)} = W . avg^{(i)} + b$$
$$ a^{(i)} = softmax(z^{(i)})$$
$$ \mathcal{L}^{(i)} = - \sum_{k = 0}^{n_y - 1} Yoh^{(i)}_k * log(a^{(i)}_k)$$
It is possible to come up with a more efficient vectorized implementation. But since we are using a for-loop to convert the sentences one at a time into the avg^{(i)} representation anyway, let's not bother this time.
We provided you a function softmax().
Can you write Python code for it?

# GRADED FUNCTION: model

def model(X, Y, word_to_vec_map, learning_rate = 0.01, num_iterations = 400):
    """
    Model to train word vector representations in numpy.

    Arguments:
    X -- input data, numpy array of sentences as strings, of shape (m, 1)
    Y -- labels, numpy array of integers between 0 and 7, numpy-array of shape (m, 1)
    word_to_vec_map -- dictionary mapping every word in a vocabulary into its 50-dimensional vector representation
    learning_rate -- learning_rate for the stochastic gradient descent algorithm
    num_iterations -- number of iterations

    Returns:
    pred -- vector of predictions, numpy-array of shape (m, 1)
    W -- weight matrix of the softmax layer, of shape (n_y, n_h)
    b -- bias of the softmax layer, of shape (n_y,)
    """

    np.random.seed(1)

    # Define number of training examples
    m = Y.shape[0]                          # number of training examples
    n_y = 5                                 # number of classes
    n_h = 50                                # dimensions of the GloVe vectors

    # Initialize parameters using Xavier initialization
    W = np.random.randn(n_y, n_h) / np.sqrt(n_h)
    b = np.zeros((n_y,))

    # Convert Y to Y_onehot with n_y classes
    Y_oh = convert_to_one_hot(Y, C = n_y)

    # Optimization loop
    for t in range(num_iterations):                       # Loop over the number of iterations
        for i in range(m):                                # Loop over the training examples

            ### START CODE HERE ### (≈ 4 lines of code)
            # Average the word vectors of the words from the i'th training example
            avg = sentence_to_avg(X[i], word_to_vec_map)

            # Forward propagate the avg through the softmax layer
            z = np.dot(W, avg) + b
            a = softmax(z)

            # Compute cost using the i'th training label's one hot representation and "A" (the output of the softmax)
            cost = -np.sum(Y_oh[i]*np.log(a))
            ###...


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Fit the Keras model on X_train_indices and Y_train_oh. We will use epochs = 50 and batch_size = 32.
Can you write Python code for it?

model.fit(X_train_indices, Y_train_oh, epochs = 50, batch_size = 32, shuffle=True)


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Let's see what convert_to_one_hot() did. Feel free to change index to print out different values.
Can you write Python code for it?

index = 50
print(Y_train[index], "is converted into one hot", Y_oh_train[index])


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Now you can try it on your own example. Write your own sentence below.
Can you write Python code for it?

# Change the sentence below to see your prediction. Make sure all the words are in the Glove embeddings.
x_test = np.array(['not feeling happy'])
X_test_indices = sentences_to_indices(x_test, word_to_index, maxLen)
print(x_test[0] +' '+  label_to_emoji(np.argmax(model.predict(X_test_indices))))


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Run the following cell to check what sentences_to_indices() does, and check your results.
Can you write Python code for it?

X1 = np.array(["funny lol", "lets play baseball", "food is ready for you"])
X1_indices = sentences_to_indices(X1,word_to_index, max_len = 5)
print("X1 =", X1)
print("X1_indices =", X1_indices)


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
You should get a test accuracy between 80% and 95%. Run the cell below to see the mislabelled examples.
Can you write Python code for it?

# This code allows you to see the mislabelled examples
C = 5
y_test_oh = np.eye(C)[Y_test.reshape(-1)]
X_test_indices = sentences_to_indices(X_test, word_to_index, maxLen)
pred = model.predict(X_test_indices)
for i in range(len(X_test)):
    x = X_test_indices
    num = np.argmax(pred[i])
    if(num != Y_test[i]):
        print('Expected emoji:'+ label_to_emoji(Y_test[i]) + ' prediction: '+ X_test[i] + label_to_emoji(num).strip())


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
You've loaded:
- word_to_index: dictionary mapping from words to their indices in the vocabulary (400,001 words, with the valid indices ranging from 0 to 400,000)
- index_to_word: dictionary mapping from indices to their corresponding words in the vocabulary
- word_to_vec_map: dictionary mapping words to their GloVe vector representation.
Run the following cell to check if it works.
Can you write Python code for it?

word = "cucumber"
index = 289846
print("the index of", word, "in the vocabulary is", word_to_index[word])
print("the", str(index) + "th word in the vocabulary is", index_to_word[index])


I am working on the file "Sequence Models/Emojify+-+v2.ipynb".
The first task is:
Your model should perform close to 100% accuracy on the training set. The exact accuracy you get may be a little different. Run the following cell to evaluate your model on the test set.
Can you write Python code for it?

X_test_indices = sentences_to_indices(X_test, word_to_index, max_len = maxLen)
Y_test_oh = convert_to_one_hot(Y_test, C = 5)
loss, acc = model.evaluate(X_test_indices, Y_test_oh)
print()
print("Test accuracy = ", acc)


I am working on the file "Sequence Models/Neural+machine+translation+with+attention+-+v3.ipynb".
The first task is:
1 - Translating human readable dates into machine readable dates
The model you will build here could be used to translate from one language to another, such as translating from English to Hindi. However, language translation requires massive datasets and usually takes days of training on GPUs. To give you a place to experiment with these models even without using massive datasets, we will instead use a simpler "date translation" task.
The network will input a date written in a variety of possible formats (e.g. "the 29th of August 1958", "03/30/1968", "24 JUNE 1987") and translate them into standardized, machine readable dates (e.g. "1958-08-29", "1968-03-30", "1987-06-24"). We will have the network learn to output dates in the common machine-readable format YYYY-MM-DD.
<!--
Take a look at [nmt_utils.py](./nmt_utils.py) to see all the formatting. Count and figure out how the formats work, you will need this knowledge later. !-->

1.1 - Dataset
We will train the model on a dataset of 10000 human readable dates and their equivalent, standardized, machine readable dates. Let's run the following cells to load the dataset and print some examples.
Can you write Python code for it?

m = 10000
dataset, human_vocab, machine_vocab, inv_machine_vocab = load_dataset(m)

dataset[:10]


I am working on the file "Sequence Models/Neural+machine+translation+with+attention+-+v3.ipynb".
The first task is:
Expected Output:
Here is the summary you should see

            **Total params:**

         185,484

            **Trainable params:**

         185,484

            **Non-trainable params:**

         0

            **bidirectional_1's output shape **

         (None, 30, 128)

            **repeat_vector_1's output shape **

         (None, 30, 128)

            **concatenate_1's output shape **

         (None, 30, 256)

            **attention_weights's output shape **

         (None, 30, 1)

            **dot_1's output shape **

         (None, 1, 128)

            **dense_2's output shape **

         (None, 11)

As usual, after creating your model in Keras, you need to compile it and define what loss, optimizer and metrics your are want to use. Compile your model using categorical_crossentropy loss, a custom Adam optimizer (learning rate = 0.005, $\beta_1 = 0.9$, $\beta_2 = 0.999$, decay = 0.01)  and ['accuracy'] metrics:
Can you write Python code for it?

### START CODE HERE ### (≈2 lines)
opt = Adam(lr=0.005, beta_1=0.9, beta_2=0.999 , decay=0.01)
model.compile(loss='categorical_crossentropy', optimizer=opt , metrics=['accuracy'])
### END CODE HERE ###


I am working on the file "Sequence Models/Neural+machine+translation+with+attention+-+v3.ipynb".
The first task is:
Let's get a summary of the model to check if it matches the expected output.
Can you write Python code for it?


I am working on the file "Sequence Models/Neural+machine+translation+with+attention+-+v3.ipynb".
The first task is:
Navigate through the output of model.summary() above. You can see that the layer named attention_weights outputs the alphas of shape (m, 30, 1) before dot_2 computes the context vector for every time step $t = 0, \ldots, T_y-1$. Lets get the activations from this layer.
The function attention_map() pulls out the attention values from your model and plots them.
Can you write Python code for it?

attention_map = plot_attention_map(model, human_vocab, inv_machine_vocab, "Tuesday 09 Oct 1993", num = 7, n_s = 64)


I am working on the file "Sequence Models/Neural+machine+translation+with+attention+-+v3.ipynb".
The first task is:
Now you can use these layers $T_y$ times in a for loop to generate the outputs, and their parameters will not be reinitialized. You will have to carry out the following steps:

Propagate the input into a Bidirectional LSTM

Iterate for $t = 0, \dots, T_y-1$:

Call one_step_attention() on $[\alpha^{<t,1>},\alpha^{<t,2>}, ..., \alpha^{<t,T_x>}]$ and $s^{<t-1>}$ to get the context vector $context^{<t>}$.
Give $context^{<t>}$ to the post-attention LSTM cell. Remember pass in the previous hidden-state $s^{\langle t-1\rangle}$ and cell-states $c^{\langle t-1\rangle}$ of this LSTM using initial_state= [previous hidden state, previous cell state]. Get back the new hidden state $s^{<t>}$ and the new cell state $c^{<t>}$.
Apply a softmax layer to $s^{<t>}$, get the output.
Save the output by adding it to the list of outputs.

Create your Keras model instance, it should have three inputs ("inputs", $s^{<0>}$ and $c^{<0>}$) and output the list of "outputs".
Can you write Python code for it?

# GRADED FUNCTION: model

def model(Tx, Ty, n_a, n_s, human_vocab_size, machine_vocab_size):
    """
    Arguments:
    Tx -- length of the input sequence
    Ty -- length of the output sequence
    n_a -- hidden state size of the Bi-LSTM
    n_s -- hidden state size of the post-attention LSTM
    human_vocab_size -- size of the python dictionary "human_vocab"
    machine_vocab_size -- size of the python dictionary "machine_vocab"

    Returns:
    model -- Keras model instance
    """

    # Define the inputs of your model with a shape (Tx,)
    # Define s0 and c0, initial hidden state for the decoder LSTM of shape (n_s,)
    X = Input(shape=(Tx, human_vocab_size))
    s0 = Input(shape=(n_s,), name='s0')
    c0 = Input(shape=(n_s,), name='c0')
    s = s0
    c = c0

    # Initialize empty list of outputs
    outputs = []

    ### START CODE HERE ###

    # Step 1: Define your pre-attention Bi-LSTM. Remember to use return_sequences=True. (≈ 1 line)
    a = Bidirectional(LSTM(n_a, return_sequences=True), input_shape=(m,Tx,2*n_a))(X)

    # Step 2: Iterate for Ty steps
    for t in range(Ty):

        # Step 2.A: Perform one step of the attention mechanism to get back the context vector at step t (≈ 1 line)
        context = one_step_attention(a, s)

        # Step 2.B: Apply the post-attention LSTM cell to the "context" vector.
        # Don't forget to pass: initial_state = [hidden state, cell state] (≈ 1 line)
        s, _, c = post_activation_LSTM_cell(initial_state=[s, c], inputs=context)

        # Step 2.C: Apply Dense layer to the hidden state output of the post-attention LSTM (≈ 1 line)
        out = output_layer(s)

        # Step 2.D: Append "out" to the "outputs" list (≈ 1 line)
        outputs.append(out)

    # Step 3: Create model instance taking three inputs and returning the list of outputs. (≈ 1 line)
    model = Model(inputs=[X, s0, c0], outputs=outputs)

    ### END CODE HERE ###

    return model