Kaggle competition: Sentiment Analysis on Movie Reviews
<SOS> metric: CategorizationAccuracy Kaggle data source: sentiment-analysis-on-movie-reviews<feature_engineering>
!pip install -U sentence-transformers
for df in combine:
titles = list(set(df.Title))
classes = list(set(df.Pclass))
for title in titles:
for cl in classes:
fil =(df.Title == title)&(df.Pclass == cl)
 med_age = df[fil].Age.dropna().median()
var_age = med_age / 5
mis_age = df[fil].MisAge.sum()
df.loc[fil &(df.Age.isnull()), 'Age'] = np.random.randint(int(med_age - var_age - 1),
int(med_age + var_age), mis_age)
 train_df.Age.describe()<data_type_conversions>
PRETRAINED_MODEL='roberta-large-nli-stsb-mean-tokens'
model = SentenceTransformer(PRETRAINED_MODEL)
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'male':1 , 'female':2} ).astype(int)
 train_df.sample(5 )<categorify>
import numpy as np
import pandas as pd
from pathlib import Path
from sklearn import preprocessing
import os
from timeit import default_timer
import tensorflow as tf
import tensorflow.keras as keras
import regex as re
import lightgbm as lgbm
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S':1 , 'C':2, 'Q':3} ).astype(int)
 train_df.sample(5 )<sort_values>
DATA_ROOT = Path(".. ")/ "/kaggle/input/sentiment-analysis-on-movie-reviews"
train = pd.read_csv(DATA_ROOT / 'train.tsv.zip', sep="\t")
 test = pd.read_csv(DATA_ROOT / 'test.tsv.zip', sep="\t")
 print(train.shape,test.shape)
 train.head()
train_df[['Title', 'Survived']].groupby(['Title'], as_index=True ).mean().sort_values(by='Survived',
ascending=False )<count_values>
def add_features(df):
df['nwords'] = df.Phrase.apply(lambda text: len(re.findall(r'\w+', text)))
 df['nchars'] = df.Phrase.apply(lambda text: len(text))
train_df.Title.value_counts()<categorify>
add_features(test)
 add_features(train)
 test.head()
for df in combine:
df['Title'] = df['Title'].map({'Mr':1 , 'Mrs':2, 'Miss':3, 'Master':4} ).astype(int)
 train_df.sample(5 )<count_values>
TRANSFORMER_BATCH=128
def count_embedd(df):
idx_chunk=list(df.columns ).index('Phrase')
 embedd_lst = []
for index in range(0, df.shape[0], TRANSFORMER_BATCH):
embedds = model.encode(df.iloc[index:index+TRANSFORMER_BATCH, idx_chunk].values, show_progress_bar=False)
 embedd_lst.append(embedds)
 return np.concatenate(embedd_lst)
for df in combine:
df['IsAlone'] = 0
fil =(df.SibSp == 0)&(df.Parch == 0)
 df.loc[fil, 'IsAlone'] = 1
print(train_df.IsAlone.value_counts())
 print("_"*40)
 print(test_df.IsAlone.value_counts() )<sort_values>
start_time = default_timer()
train_embedd = count_embedd(train)
 print("Train embeddings: {}: in: {:5.2f}s".format(train_embedd.shape, default_timer() - start_time))
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )<feature_engineering>
start_time = default_timer()
test_embedd = count_embedd(test)
 print("Test embeddings: {}: in: {:5.2f}s".format(test_embedd.shape, default_timer() - start_time))
for dataset in combine:
df = dataset.groupby(['Sex', 'IsAlone'] ).size().unstack(0)
 df['fem_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
df['male_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )<groupby>
X_train = np.array(train_embedd)
 X_test = np.array(test_embedd)
 X_train = np.concatenate(( X_train, train[['nchars','nwords']].values), axis=1)
 X_test = np.concatenate(( X_test, test[['nchars','nwords']].values), axis=1)
 X_train.shape, X_test.shape
train_df[["Sex", "IsAlone", "Survived"]].groupby(['IsAlone','Sex'],
as_index=True ).mean()<feature_engineering>
enc = preprocessing.OneHotEncoder()
label = train['Sentiment'].values.reshape(( -1,1))
enc.fit(label)
 y_train = enc.transform(label ).toarray()
y_train.shape
for dataset in combine:
df = dataset.groupby(['Pclass', 'IsAlone'] ).size().unstack(0)
 df['first_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['second_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['third_perc'] =(df[df.columns[2]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
print(df)
 print("_"*40 )<groupby>
KERAS_VALIDATION_SPLIT=0.05
KERAS_EPOCHS=10
KERAS_BATCH_SIZE=128
n_features=X_train.shape[1]
n_labels = y_train.shape[1]
start_time=default_timer()
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(2048, input_dim=n_features, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(n_labels, activation='softmax')
 ])
 LR=0.0001
adam = keras.optimizers.Adam(learning_rate=LR, beta_1=0.9, beta_2=0.999, amsgrad=False)
 model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
 model.fit(X_train, y_train, epochs=KERAS_EPOCHS, batch_size=KERAS_BATCH_SIZE, validation_split=KERAS_VALIDATION_SPLIT)
 print("Training.Dataset size: {} {:5.2f}s".format(X_train.shape, default_timer() - start_time))
train_df[["Pclass", "IsAlone", "Survived"]].groupby(['IsAlone', 'Pclass'],
as_index=True ).mean()<count_values>
y_preds = model.predict(X_test)
 y_preds.shape
for df in combine:
df['IsKid'] = 0
fil =(df.Age < 16)
 df.loc[fil, 'IsKid'] = 1
print(train_df.IsKid.value_counts())
 print("_"*40)
 print(test_df.IsKid.value_counts() )<sort_values>
sample_submission = pd.read_csv(DATA_ROOT / 'sampleSubmission.csv')
 sample_submission['Sentiment'] = np.argmax(y_preds,axis=1)
 sample_submission.to_csv("predictions.csv", index=False )
train_df[['IsKid', 'Survived']].groupby(['IsKid'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )<data_type_conversions>
params = {
'objective': 'multiclass',
'num_class':y_train.shape[1]
}
lgbm_model = lgbm.LGBMClassifier(objective='multiclass')
 lgbm_model.fit(X_train, label)
 y_preds = lgbm_model.predict_proba(X_test)
 sample_submission = pd.read_csv(DATA_ROOT / 'sampleSubmission.csv')
 sample_submission['Sentiment'] = np.argmax(y_preds,axis=1)
 sample_submission.to_csv("predictions_lgbm.csv", index=False )
<sort_values><EOS> 
def create_download_link(title = "Download CSV file", filename = "data.csv"):
html = '<a href={filename}>{title}</a>'
html = html.format(title=title,filename=filename)
 return HTML(html)
 create_download_link(filename='predictions_lgbm.csv')