Kaggle competition: Bag of Words Meets Bags of Popcorn
<SOS> metric: AUC Kaggle data source: bag-of-words-meets-bags-of-popcorn<feature_engineering>
warnings.filterwarnings('ignore')
 for file in os.listdir(".. /input"):
print(file )
data_info={}
data_info['word_ind']=word_ind
data_info['word_len']=len(word_ind)+1<categorify>
SMALL_SIZE = 12
MEDIUM_SIZE = 14
BIG_SIZE = 16
LARGE_SIZE = 20
params = {
'figure.figsize':(16, 8),
'font.size': SMALL_SIZE,
'xtick.labelsize': MEDIUM_SIZE,
'ytick.labelsize': MEDIUM_SIZE,
'legend.fontsize': BIG_SIZE,
'figure.titlesize': LARGE_SIZE,
'axes.titlesize': MEDIUM_SIZE,
'axes.labelsize': BIG_SIZE
}
plt.rcParams.update(params )
pad_train=pad_sequences(text_seq_tr, maxlen=400)
 pad_test=pad_sequences(text_seq_te, maxlen=400 )<split>
usecols = ['sentiment','review']
train_data = pd.read_csv(
 filepath_or_buffer='.. /input/word2vec-nlp-tutorial/labeledTrainData.tsv',
usecols=usecols, sep='\t')
 additional_data = pd.read_csv(
 filepath_or_buffer='.. /input/imdb-review-dataset/imdb_master_filtered.csv',
sep='\t')[usecols]
unlabeled_data = pd.read_csv(
 filepath_or_buffer=".. /input/word2vec-nlp-tutorial/unlabeledTrainData.tsv",
error_bad_lines=False,
sep='\t')
 submission_data = pd.read_csv(
 filepath_or_buffer=".. /input/word2vec-nlp-tutorial/testData.tsv",
sep='\t' )
x_train, x_valid, y_train, y_valid = train_test_split(pad_train, train['sentiment'], random_state=77, test_size=0.07, stratify=train['sentiment'] )<choose_model_class>
all_reviews = np.array([], dtype=str)
 for dataset in datasets:
all_reviews = np.concatenate(( all_reviews, dataset.review), axis=0)
 print('Total number of reviews:', len(all_reviews))
model=Sequential()
model.add(Embedding(101247,65, input_length=400))
model.add(Flatten())
 model.add(Dense(2,activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc'] )<train_model>
train_data = pd.concat(( train_data, additional_data[additional_data.sentiment != -1]),
axis=0, ignore_index=True)
 train_data.info()
es=EarlyStopping(patience=4)
 mc=ModelCheckpoint('best.h5',save_best_only=True)
 model.fit(x_train,y_train, batch_size=128, epochs=10, validation_data=[x_valid,y_valid], callbacks=[es,mc] )<load_pretrained>
def clean_review(raw_review: str)-> str:
review_text = BeautifulSoup(raw_review, "lxml" ).get_text()
letters_only = REPLACE_WITH_SPACE.sub(" ", review_text)
 lowercase_letters = letters_only.lower()
return lowercase_letters
def lemmatize(tokens: list)-> list:
tokens = list(map(lemmatizer.lemmatize, tokens))
lemmatized_tokens = list(map(lambda x: lemmatizer.lemmatize(x, "v"), tokens))
meaningful_words = list(filter(lambda x: not x in stop_words, lemmatized_tokens))
return meaningful_words
def preprocess(review: str, total: int, show_progress: bool = True)-> list:
if show_progress:
global counter
counter += 1
print('Processing...%6i/%6i'%(counter, total), end='\r')
 review = clean_review(review)
 tokens = word_tokenize(review)
 lemmas = lemmatize(tokens)
 return lemmas
model.load_weights('best.h5' )<predict_on_test>
counter = 0
REPLACE_WITH_SPACE = re.compile(r'[^A-Za-z\s]')
 stop_words = set(stopwords.words("english"))
lemmatizer = WordNetLemmatizer()
res=model.predict(pad_test, batch_size=128 )<feature_engineering>
all_reviews = np.array(list(map(lambda x: preprocess(x, len(all_reviews)) , all_reviews)))
 counter = 0
sub['sentiment_pro']=res[:,1]<feature_engineering>
X_train_data = all_reviews[:train_data.shape[0]]
Y_train_data = train_data.sentiment.values
X_submission = all_reviews[125000: 150000]
sub.loc[sub['sentiment_pro']>=0.5,"sentiment"]=1
sub.loc[sub['sentiment_pro']<0.5,"sentiment"]=0<prepare_output>
train_data['review_lenght'] = np.array(list(map(len, X_train_data)))
 median = train_data['review_lenght'].median()
mean = train_data['review_lenght'].mean()
mode = train_data['review_lenght'].mode() [0]
sub=sub[['id','sentiment']]<save_to_csv>
%%time
bigrams = Phrases(sentences=all_reviews )
sub.to_csv('result.csv',index=False )<set_options>
%%time
trigrams = Phrases(sentences=bigrams[all_reviews] )
%matplotlib inline<load_from_csv>
print(bigrams['space station near the solar system'.split() ] )
train_data = pd.read_csv(".. /input/word2vec-nlp-tutorial/labeledTrainData.tsv", delimiter='\t')
 train_data<load_from_csv>
%%time
embedding_vector_size = 256
trigrams_model = Word2Vec(
 sentences = trigrams[bigrams[all_reviews]],
size = embedding_vector_size,
min_count=3, window=5, workers=4 )
test_data = pd.read_csv(".. /input/word2vec-nlp-tutorial/testData.tsv", delimiter='\t' )<import_modules>
trigrams_model.wv.doesnt_match(['galaxy', 'starship', 'planet', 'dog'] )
import re
import pandas
import numpy
import json
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from tensorflow.python.keras.preprocessing.text import Tokenizer<string_transform>
%%time
def vectorize_data(data, vocab: dict)-> list:
print('Vectorize sentences...', end='\r')
 keys = list(vocab.keys())
 filter_unknown = lambda word: vocab.get(word, None)is not None
encode = lambda review: list(map(keys.index, filter(filter_unknown, review)))
 vectorized = list(map(encode, data))
print('Vectorize sentences...( done)')
 return vectorized
print('Convert sentences to sentences with ngrams...', end='\r')
 X_data = trigrams[bigrams[X_train_data]]
print('Convert sentences to sentences with ngrams...( done)')
 input_length = 150
X_pad = pad_sequences(
 sequences=vectorize_data(X_data, vocab=trigrams_model.wv.vocab),
maxlen=input_length,
padding='post')
 print('Transform sentences to sequences...( done)' )
def preprocess(review,remove_stopwords = False):
review_text = BeautifulSoup(review,"html5lib" ).get_text()
review_text = re.sub("[^a-zA-Z]"," ",review_text)
 words = review_text.lower().split()
if remove_stopwords:
stop_words = set(stopwords.words('english'))
words = [w for w in words if not w in stop_words]
clean_review = ' '.join(words)
 return clean_review<drop_column>
X_train, X_test, y_train, y_test = train_test_split(
 X_pad,
Y_train_data,
test_size=0.05,
shuffle=True,
random_state=42 )
clean_train_reviews = []
clean_test_reviews = []
for review in train_data['review']:
clean_train_reviews.append(preprocess(review,remove_stopwords = True))
for review in test_data['review']:
clean_test_reviews.append(preprocess(review, remove_stopwords = True))<string_transform>
def build_model(embedding_matrix: np.ndarray, input_length: int):
model = Sequential()
model.add(Embedding(
 input_dim = embedding_matrix.shape[0],
output_dim = embedding_matrix.shape[1],
input_length = input_length,
weights = [embedding_matrix],
trainable=False))
model.add(Bidirectional(LSTM(128, recurrent_dropout=0.1)))
 model.add(Dropout(0.25))
model.add(Dense(64))
model.add(Dropout(0.3))
model.add(Dense(1, activation='sigmoid'))
model.summary()
return model
model = build_model(
 embedding_matrix=trigrams_model.wv.vectors,
input_length=input_length )
MAX_SEQUENCE_LENGTH = 174
tokenizer = Tokenizer()
tokenizer.fit_on_texts(clean_train_reviews + clean_train_reviews)
 text_sequences = tokenizer.texts_to_sequences(clean_train_reviews)
 train_inputs = pad_sequences(text_sequences,maxlen=MAX_SEQUENCE_LENGTH, padding='post')
 text_sequences = tokenizer.texts_to_sequences(clean_test_reviews)
 test_inputs = pad_sequences(text_sequences, maxlen=MAX_SEQUENCE_LENGTH, padding='post' )<import_modules>
model.compile(
 loss="binary_crossentropy",
optimizer='adam',
metrics=['accuracy'])
 history = model.fit(
 x=X_train,
y=y_train,
validation_data=(X_test, y_test),
batch_size=100,
epochs=20 )
print(tf.__version__ )<string_transform>
%%time
y_train_pred = model.predict_classes(X_train)
 y_test_pred = model.predict_classes(X_test )
len(tokenizer.word_index )<choose_model_class>
print('Convert sentences to sentences with ngrams...', end='\r')
 X_submit = trigrams[bigrams[X_submission]]
print('Convert sentences to sentences with ngrams...( done)')
 X_sub = pad_sequences(
 sequences=vectorize_data(X_submit, vocab=trigrams_model.wv.vocab),
maxlen=input_length,
padding='post')
 print('Transform sentences to sequences...( done)' )
vocab_size = len(tokenizer.word_index)+ 1
model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16))
model.add(keras.layers.GlobalAveragePooling1D())
 model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.summary()<choose_model_class>
%%time
Y_sub_pred = model.predict_classes(X_sub )
<prepare_x_and_y><EOS> 
def submit(predictions):
submission_data['sentiment'] = predictions
submission_data.to_csv('submission.csv', index=False, columns=['id','sentiment'])
 submit(Y_sub_pred )