Kaggle competition: Sentiment Analysis on Movie Reviews
<SOS> metric: CategorizationAccuracy Kaggle data source: sentiment-analysis-on-movie-reviews<data_type_conversions>
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import random
import os
training_data['hasCabin'] = training_data.Cabin.notnull().astype(int)
 test_data['hasCabin'] = test_data.Cabin.notnull().astype(int )<drop_column>
print("TF version: ", tf.__version__)
 if tf.__version__ < "2.0.0":
tf.enable_eager_execution()
print("Eager execution enabled.")
 else:
print("Eager execution enabled by default.")
 if tf.test.gpu_device_name() :
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF" )
training_data.drop(labels='Cabin', axis=1, inplace=True)
 training_data.head()<drop_column>
def setseed(seed = 0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
SEED = 0
setseed(SEED)
 setseed()
test_data.drop(labels='Cabin', axis=1, inplace=True)
 test_data.head()<categorify>
train = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep = '\t')
 test = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip', sep = '\t' )
<feature_engineering>
from tqdm import tqdm
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import re
training_data['Embarked'] = training_data['Embarked'].fillna('S' )<groupby>
def clean_sentences(df):
reviews = []
for sent in tqdm(df['Phrase']):
review_text = re.sub("[^a-zA-Z]"," ", sent)
 words = word_tokenize(review_text.lower())
 lemmatizer = WordNetLemmatizer()
lemma_words = [lemmatizer.lemmatize(i)for i in words]
reviews.append(lemma_words)
 return(reviews )
print(training_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index = False ).mean() )<groupby>
%%time
train_sentences = clean_sentences(train)
 test_sentences = clean_sentences(test)
 print(len(train_sentences))
print(len(test_sentences))
print(training_data[['Embarked', 'Fare']].groupby(['Embarked'], as_index = False ).mean() )<groupby>
print(train['Phrase'][0])
 print(' '.join(train_sentences[0]))
<split>
target = train.Sentiment.values
y_target = to_categorical(target)
 num_classes = y_target.shape[1]
training_data.head()<prepare_x_and_y>
from sklearn.model_selection import train_test_split
X = training_data.iloc[:, 1:12].values
y = training_data.iloc[:, 0].values<categorify>
from sklearn.model_selection import train_test_split
label_encoder_sex_tr = LabelEncoder()
label_encoder_title_tr = LabelEncoder()
label_encoder_embarked_tr = LabelEncoder()
X[:, 1] = label_encoder_sex_tr.fit_transform(X[:, 1])
 X[:, 5] = label_encoder_title_tr.fit_transform(X[:, 5])
 X[:, 4] = label_encoder_embarked_tr.fit_transform(X[:, 4])
<split>
X_train, X_val, y_train, y_val = train_test_split(train_sentences,
y_target,
test_size = 0.2,
stratify = y_target )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.17 )<normalization>
unique_words = set()
len_max = 0
for sent in tqdm(X_train):
unique_words.update(sent)
 if(len_max < len(sent)) :
len_max = len(sent)
 print('Number of vocabs: ', len(list(unique_words)))
 print('Max length of text is: ', len_max )
scaler_x = MinMaxScaler(( -1,1))
X_train = scaler_x.fit_transform(X_train)
 X_test = scaler_x.transform(X_test)
<define_variables>
vocab_size = len(list(unique_words))
embedding_dim = 300
max_length = len_max
trunc_type='post'
padding_type='post'
oov_tok = "<OOV>"
accuracies = []<train_model>
%%time
tokenizer = Tokenizer(num_words = vocab_size,
',
oov_token = oov_tok,
char_level = False)
 tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
 X_train = pad_sequences(X_train,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type)
 X_val = tokenizer.texts_to_sequences(X_val)
 X_val = pad_sequences(X_val,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type)
 X_test = tokenizer.texts_to_sequences(test_sentences)
 X_test = pad_sequences(X_test,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type )
classifier = LogisticRegression()
classifier.fit(X_train, y_train )<compute_test_metric>
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length = max_length),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, dropout = 0.8, recurrent_dropout=0.8, return_sequences=True)) ,
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, dropout = 0.5, recurrent_dropout=0.5, return_sequences=False)) ,
tf.keras.layers.Dense(128, activation = 'relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(num_classes, activation = 'softmax')
 ])
 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 model.summary()
lr_score = classifier.score(X_test, y_test)
 accuracies.append(lr_score)
 print(lr_score )<train_on_grid>
early_stopping = EarlyStopping(min_delta = 0.001,
mode = 'max',
monitor = 'val_acc',
patience = 2)
 callback = [early_stopping]
svm = SVC(kernel='linear')
 svm.fit(X_train, y_train )<compute_test_metric>
%%time
num_epochs = 5
history = model.fit(X_train,
y_train,
validation_data =(X_val, y_val),
epochs = num_epochs,
batch_size = 256,
verbose = 1,
callbacks = callback )
svm_score = svm.score(X_test, y_test)
 accuracies.append(svm_score)
 print(svm_score )<train_model>
test_id = test['PhraseId']
k_svm = SVC(kernel='rbf')
 k_svm.fit(X_train, y_train )<compute_test_metric>
%%time
y_pred = np.argmax(model.predict(X_test), axis=-1 )
<train_model><EOS> 
submission = pd.DataFrame({'PhraseId': test_id, 'Sentiment': y_pred})
 submission.to_csv('movie_review_prediction_5EP_MLBDLSTM_submission.csv', index=False)
 submission.head()