Kaggle competition: Sentiment Analysis on Movie Reviews
<SOS> metric: CategorizationAccuracy Kaggle data source: sentiment-analysis-on-movie-reviews<set_options>
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Dense, GRU, Dropout, Bidirectional, SpatialDropout1D
from tensorflow.keras.utils import to_categorical
warnings.filterwarnings('ignore')
 %matplotlib inline<load_from_csv>
df_train = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep='\t', usecols=['Phrase', 'Sentiment'])
 df_submission = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/test.tsv.zip', sep='\t', usecols=['Phrase'] )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print('Train size:\t
Test size:\t 
.format(train_df.shape[0], train_df.shape[1], test_df.shape[0], test_df.shape[1]))<prepare_x_and_y>
X_train, X_test, y_train, y_test = train_test_split(df_train['Phrase'].values, df_train['Sentiment'].values, test_size=0.1 )
train_df['train_set'] = 1
test_df['train_set'] = 0
train_ids = train_df.pop('Id')
 test_ids = test_df.pop('Id')
 y = train_df.pop('SalePrice')
 df = train_df.append(test_df ).reset_index(drop=True)
 df.head()<sort_values>
tokenizer = Tokenizer()
num_rows = df_train.shape[0]
tokenizer.fit_on_texts(df_train['Phrase'].values)
 row_max_length = max([len(x.split())for x in df_train['Phrase'].values])
 vocabulary_size = len(tokenizer.word_index)+ 1
X_train_tokens = tokenizer.texts_to_sequences(X_train)
 X_test_tokens = tokenizer.texts_to_sequences(X_test)
 X_sub_tokens = tokenizer.texts_to_sequences(df_submission['Phrase'].values)
 X_train_pad = pad_sequences(X_train_tokens, maxlen=row_max_length, padding='post')
 X_test_pad = pad_sequences(X_test_tokens, maxlen=row_max_length, padding='post')
 X_sub_pad = pad_sequences(X_sub_tokens, maxlen=row_max_length, padding='post' )
cols_with_nulls, null_perc = [], []
for col in df.columns:
if sum(df[col].isnull())> 0:
cols_with_nulls.append(col)
 null_perc.append(100 * sum(df[col].isnull())/ len(df))
null_counts = pd.DataFrame({'column': cols_with_nulls, 'null_perc': null_perc} ).sort_values(by='null_perc',
ascending=False )<drop_column>
y_train_cat = to_categorical(y_train)
 y_test_cat = to_categorical(y_test)
 target_length = y_train_cat.shape[1]
print('Original vector size: {}'.format(y_train.shape))
print('Converted vector size: {}'.format(y_train_cat.shape))
null_threshold =.5
for col in df.columns:
if(sum(df[col].isnull())/ len(df)> null_threshold)&(col != 'SalePrice'):
df.drop(col, axis=1, inplace=True )<define_variables>
EMBEDDING_DIM = 256
model = Sequential()
model.add(Embedding(vocabulary_size, EMBEDDING_DIM, input_length=row_max_length))
model.add(SpatialDropout1D(0.2))
model.add(Bidirectional(GRU(128)))
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(target_length, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)
 history = model.fit(X_train_pad, y_train_cat, epochs=5, validation_data=(X_test_pad, y_test_cat), batch_size=128, callbacks=[callback] )
ordinal_numericals = ['BsmtFullBath', 'BsmtHalfBath', 'Fireplaces', 'FullBath', 'GarageCars',
'HalfBath', 'MoSold', 'OverallCond', 'OverallQual', 'TotRmsAbvGrd']
date_numericals = ['GarageYrBlt', 'YearBuilt', 'YearRemodAdd', 'YrSold']
categorical_numericals = ['MSSubClass']<define_variables>
y_sub_hat_ = model.predict(X_sub_pad)
 y_sub_hat = [np.argmax(x)for x in y_sub_hat_]
df_save = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/sampleSubmission.csv')
 df_save['Sentiment'] = y_sub_hat
df_save.to_csv('Submission.csv', index = False)
 print('Submission saved!' )
continuous_feats = [col for col in numerical_cols if col not in ordinal_numericals and\
col not in date_numericals and\
col not in categorical_numericals and\
col != 'SalePrice' and col != 'train_set']<define_variables>
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Dense, GRU, Dropout, Bidirectional, SpatialDropout1D
from tensorflow.keras.utils import to_categorical
categorical_strings = ['BldgType', 'Condition1', 'Condition2', 'Electrical', 'Exterior1st',
'Exterior2nd', 'Foundation', 'GarageType', 'Heating', 'HouseStyle',
'LandContour', 'LotConfig', 'LotShape', 'MSZoning', 'MasVnrType',
'Neighborhood', 'PavedDrive', 'RoofMatl', 'RoofStyle', 'SaleCondition',
'SaleType', 'Street', 'Utilities']
ordinal_strings = ['BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'BsmtQual', 'ExterCond',
'ExterQual', 'FireplaceQu', 'Functional', 'GarageCond', 'GarageFinish', 'GarageQual',
'HeatingQC', 'KitchenQual', 'LandSlope']
boolean_strings = ['CentralAir']<data_type_conversions>
df_train = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep='\t', usecols=['Phrase', 'Sentiment'])
 df_submission = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/test.tsv.zip', sep='\t', usecols=['Phrase'] )
all_categoricals = categorical_numericals + categorical_strings
df[all_categoricals] = df[all_categoricals].fillna('NA')
 all_ordinals = ordinal_numericals + ordinal_strings
mode_df = pd.DataFrame(data=df[all_ordinals].mode().values.tolist() [0], index=all_ordinals)
 df[all_ordinals] = df[all_ordinals].fillna(mode_df[0])
 df[continuous_feats] = df[continuous_feats].fillna(df[continuous_feats].median())
 df[date_numericals] = df[date_numericals].fillna(df[date_numericals].median() )<categorify>
X_train, X_test, y_train, y_test = train_test_split(df_train['Phrase'].values, df_train['Sentiment'].values, test_size=0.1 )
print('Shape before one-hot encoding of categoricals =\t{}'.format(df.shape))
df = pd.get_dummies(df, columns=all_categoricals)
 print('Shape after one-hot encoding of categoricals =\t{}'.format(df.shape))<define_variables>
tokenizer = Tokenizer()
num_rows = df_train.shape[0]
tokenizer.fit_on_texts(df_train['Phrase'].values)
 row_max_length = max([len(x.split())for x in df_train['Phrase'].values])
 vocabulary_size = len(tokenizer.word_index)+ 1
X_train_tokens = tokenizer.texts_to_sequences(X_train)
 X_test_tokens = tokenizer.texts_to_sequences(X_test)
 X_sub_tokens = tokenizer.texts_to_sequences(df_submission['Phrase'].values)
 X_train_pad = pad_sequences(X_train_tokens, maxlen=row_max_length, padding='post')
 X_test_pad = pad_sequences(X_test_tokens, maxlen=row_max_length, padding='post')
 X_sub_pad = pad_sequences(X_sub_tokens, maxlen=row_max_length, padding='post' )
ordinal_dict_1 = {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
ordinal_dict_2 = {'NA': 0, 'No': 1, 'Mn': 2, 'Av': 3, 'Gd': 4}
ordinal_dict_3 = {'NA': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}
ordinal_dict_4 = {'Sal': 0, 'Sev': 1, 'Maj2': 2, 'Maj1': 3, 'Mod': 4, 'Min2': 5, 'Min1': 6, 'Typ': 7}
ordinal_dict_5 = {'Na': 0, 'Unf': 1, 'RFn': 2, 'Fin': 3}
ordinal_dict_6 = {'Sev': 0, 'Mod': 1, 'Gtl': 2}<categorify>
y_train_cat = to_categorical(y_train)
 y_test_cat = to_categorical(y_test)
 target_length = y_train_cat.shape[1]
print('Original vector size: {}'.format(y_train.shape))
print('Converted vector size: {}'.format(y_train_cat.shape))
for cat in ['ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond', 'HeatingQC', 'KitchenQual', 'FireplaceQu',
'GarageQual', 'GarageCond', ]:
df[cat] = df[cat].map(ordinal_dict_1)
 for cat in ['BsmtExposure']:
df[cat] = df[cat].map(ordinal_dict_2)
 for cat in ['BsmtFinType1', 'BsmtFinType2']:
df[cat] = df[cat].map(ordinal_dict_3)
 for cat in ['Functional']:
df[cat] = df[cat].map(ordinal_dict_4)
 for cat in ['GarageFinish']:
df[cat] = df[cat].map(ordinal_dict_5)
 for cat in ['LandSlope']:
df[cat] = df[cat].map(ordinal_dict_6)
 null_counts.set_index('column', inplace=True)
 for col in ordinal_strings:
if sum(df[col].isnull())> 0:
null_counts.loc[col, 'perc_after_encoding'] = 100 * sum(df[col].isnull())/ len(df)
 assert null_counts.loc[col, 'perc_after_encoding'] == null_counts.loc[col, 'null_perc'],\
'Nulls introduced in feature {}, check corresponding dictionary'.format(col )<categorify>
EMBEDDING_DIM = 256
model = Sequential()
model.add(Embedding(vocabulary_size, EMBEDDING_DIM, input_length=row_max_length))
model.add(SpatialDropout1D(0.2))
model.add(Bidirectional(GRU(128)))
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(target_length, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)
 history = model.fit(X_train_pad, y_train_cat, epochs=5, validation_data=(X_test_pad, y_test_cat), batch_size=128, callbacks=[callback] )
for cat in boolean_strings:
encoder = LabelEncoder()
df[cat] = encoder.fit_transform(df[cat] )<feature_engineering>
y_sub_hat_ = model.predict(X_sub_pad)
 y_sub_hat = [np.argmax(x)for x in y_sub_hat_]
df_save = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/sampleSubmission.csv')
 df_save['Sentiment'] = y_sub_hat
df_save.to_csv('Submission.csv', index = False)
 print('Submission saved!' )