GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing pythonprogramming.net — Python Programming Tutorials Simple Preprocessing Language Data for Deep LearningWelcome to part six of the Deep Learning with Neural Networks and TensorFlow tutorials. Where we left off, we explained our plan and theory for applying our deep neural network to some sentiment training data, and now we're going to be working on the pre-processing script for that. To do this, we will use two files: and . The pos file has ~5,000 positive sentiment statements, and the neg file has ~5,000 negative sentiment statements. We left off with: import nltk from nltk.tokenize import word_tokenize import numpy as np import random import pickle from collections import Counter from nltk.stem import WordNetLemmatizer lemmatizer = WordNetLemmatizer() hm_lines = 100000 Now we'll begin to build the lexicon: def create_lexicon(pos,neg): lexicon = [] with open(pos,'r') as f: contents = f.readlines() for l in contents[:hm_lines]: all_words = word_tokenize(l) lexicon += list(all_words) with open(neg,'r') as f: contents = f.readlines() for l in contents[:hm_lines]: all_words = word_tokenize(l) lexicon += list(all_words) Here, we've begun the function, which takes a path to the positive file and the negative file. From here, we open the files, read the lines, tokenize the words, and add them to the lexicon. At this point, our lexicon is just a list of every word in our training data. If you had a huge dataset, too large to fit into your memory, then you'd need to adjust the Continuing along in our lexicon = [lemmatizer.lemmatize(i) for i in lexicon] w_counts = Counter(lexicon) l2 = [] for w in w_counts: #print(w_counts[w]) if 1000 > w_counts[w] > 50: l2.append(w) print(len(l2)) return l2 Here, we lemmatize, then count the word occurance. If the word occurs less than 1,000 times, but more than 50 times, we want to include it in our lexicon. These two values are definitely something you may want to tweak, and really ought to be some sort of % of the entire dataset. I will just mention here that none of this code is optimized or meant to be used in production. This is just conceptual code, with tons of room for improvement. Great, so we have a lexicon. Now we can take this lexicon, and use it as our bag of words that we will look for in a string. Each time we find a lemma in our lexicon that exists in the lemmatized and word tokenized sample sentence, the index of that lemma in the lexicon is turned "on" in our previously numpy zeros array that is the same length as the lexicon. To do this, we'll build a def sample_handling(sample,lexicon,classification): featureset = [] with open(sample,'r') as f: contents = f.readlines() for l in contents[:hm_lines]: current_words = word_tokenize(l.lower()) current_words = [lemmatizer.lemmatize(i) for i in current_words] features = np.zeros(len(lexicon)) for word in current_words: if word.lower() in lexicon: index_value = lexicon.index(word.lower()) features[index_value] += 1 features = list(features) featureset.append([features,classification]) return featureset This will iterate through the "sample" file that we choose. In our case, this is the To be well suited for trainiong and testing with our current model, we ideally want to see have a training set of features, another of the associated labels, and then the same thing for the testing data. Let's make a quick function to do that too: def create_feature_sets_and_labels(pos,neg,test_size = 0.1):
lexicon = create_lexicon(pos,neg)
features = []
features += sample_handling('pos.txt',lexicon,[1,0])
features += sample_handling('neg.txt',lexicon,[0,1])
random.shuffle(features)
features = np.array(features)
testing_size = int(test_size*len(features))
train_x = list(features[:,0][:-testing_size])
train_y = list(features[:,1][:-testing_size])
test_x = list(features[:,0][-testing_size:])
test_y = list(features[:,1][-testing_size:])
return train_x,train_y,test_x,test_y
The Next, we want to shuffle this data, convert to a numpy array, and then we build the training and testing sets. From here, we return the data into individual variables. Now we are ready to go ahead and try to run this. if __name__ == '__main__':
train_x,train_y,test_x,test_y = create_feature_sets_and_labels('/path/to/pos.txt','/path/to/neg.txt')
# if you want to pickle this data:
with open('/path/to/sentiment_set.pickle','wb') as f:
pickle.dump([train_x,train_y,test_x,test_y],f)
Full code for import nltk
from nltk.tokenize import word_tokenize
import numpy as np
import random
import pickle
from collections import Counter
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
hm_lines = 100000
def create_lexicon(pos,neg):
lexicon = []
with open(pos,'r') as f:
contents = f.readlines()
for l in contents[:hm_lines]:
all_words = word_tokenize(l)
lexicon += list(all_words)
with open(neg,'r') as f:
contents = f.readlines()
for l in contents[:hm_lines]:
all_words = word_tokenize(l)
lexicon += list(all_words)
lexicon = [lemmatizer.lemmatize(i) for i in lexicon]
w_counts = Counter(lexicon)
l2 = []
for w in w_counts:
#print(w_counts[w])
if 1000 > w_counts[w] > 50:
l2.append(w)
print(len(l2))
return l2
def sample_handling(sample,lexicon,classification):
featureset = []
with open(sample,'r') as f:
contents = f.readlines()
for l in contents[:hm_lines]:
current_words = word_tokenize(l.lower())
current_words = [lemmatizer.lemmatize(i) for i in current_words]
features = np.zeros(len(lexicon))
for word in current_words:
if word.lower() in lexicon:
index_value = lexicon.index(word.lower())
features[index_value] += 1
features = list(features)
featureset.append([features,classification])
return featureset
def create_feature_sets_and_labels(pos,neg,test_size = 0.1):
lexicon = create_lexicon(pos,neg)
features = []
features += sample_handling('pos.txt',lexicon,[1,0])
features += sample_handling('neg.txt',lexicon,[0,1])
random.shuffle(features)
features = np.array(features)
testing_size = int(test_size*len(features))
train_x = list(features[:,0][:-testing_size])
train_y = list(features[:,1][:-testing_size])
test_x = list(features[:,0][-testing_size:])
test_y = list(features[:,1][-testing_size:])
return train_x,train_y,test_x,test_y
if __name__ == '__main__':
train_x,train_y,test_x,test_y = create_feature_sets_and_labels('/path/to/pos.txt','/path/to/neg.txt')
# if you want to pickle this data:
with open('/path/to/sentiment_set.pickle','wb') as f:
pickle.dump([train_x,train_y,test_x,test_y],f)
Now that we've got our data, in the next tutorial, we're going to feed this new data through to the same model that we recently used to classify hand-written digits.
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |