<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Sentiment Classification & How To "Frame Problems" for a Neural Network
by Andrew Trask
Twitter
Step1:  Lesson
Step2:  Project 1
<ASSISTANT_TASK:>
Python Code:
def pretty_print_review_and_label(i):
    print(labels[i] + "\t:\t" + reviews[i][:80] + "...")
g = open('reviews.txt','r') # What we know!
reviews = list(map(lambda x:x[:-1],g.readlines()))
g.close()
g = open('labels.txt','r') # What we WANT to know!
labels = list(map(lambda x:x[:-1].upper(),g.readlines()))
g.close()
len(reviews)
reviews[0]
labels[0]
Explanation: Sentiment Classification & How To "Frame Problems" for a Neural Network
by Andrew Trask
Twitter: @iamtrask
Blog: http://iamtrask.github.io
What You Should Already Know
neural networks, forward and back-propagation
stochastic gradient descent
mean squared error
and train/test splits
Where to Get Help if You Need it
Re-watch previous Udacity Lectures
Leverage the recommended Course Reading Material - Grokking Deep Learning (40% Off: traskud17)
Shoot me a tweet @iamtrask
Tutorial Outline:
Intro: The Importance of "Framing a Problem"
Curate a Dataset
Developing a "Predictive Theory"
PROJECT 1: Quick Theory Validation
Transforming Text to Numbers
PROJECT 2: Creating the Input/Output Data
Putting it all together in a Neural Network
PROJECT 3: Building our Neural Network
Understanding Neural Noise
PROJECT 4: Making Learning Faster by Reducing Noise
Analyzing Inefficiencies in our Network
PROJECT 5: Making our Network Train and Run Faster
Further Noise Reduction
PROJECT 6: Reducing Noise by Strategically Reducing the Vocabulary
Analysis: What's going on in the weights?
Lesson: Curate a Dataset
End of explanation
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)
Explanation: Lesson: Develop a Predictive Theory
End of explanation
import numpy as np
bag_of_words = {}
pos_words = {}
neg_words = {}
for i in range(len(reviews)):
    words = reviews[i].split(' ')
    for word in words:
        if word in bag_of_words.keys():
            bag_of_words[word] += 1
        else:
            bag_of_words[word] = 1
            pos_words[word] = 0
            neg_words[word] = 0
            
        if labels[i] == 'POSITIVE':
            if word in pos_words.keys():
                pos_words[word] += 1
        elif labels[i] == 'NEGATIVE':
            if word in neg_words.keys():
                neg_words[word] += 1
words_pos_neg_ratio = []
for word in bag_of_words.keys():
    if bag_of_words[word] > 500:
        pos_neg_ratio = pos_words[word] / float(neg_words[word] + 1)
        words_pos_neg_ratio.append((word, np.log(pos_neg_ratio)))
            
words_pos_neg_ratio = sorted(words_pos_neg_ratio, key=lambda x: x[1], reverse=True)
print('\nTop positive words: \n')
for i in range(10):
    print(words_pos_neg_ratio[i][0],': ', round(words_pos_neg_ratio[i][1], 10), sep='')
print('\nTop negative words: \n')
for i in range(-1, -11, -1):
    print(words_pos_neg_ratio[i][0],': ', round(words_pos_neg_ratio[i][1], 10), sep='')
Explanation: Project 1: Quick Theory Validation
End of explanation
<END_TASK>