Kaggle competition: Sentiment Analysis on Movie Reviews
df['TotalSF'] = df['1stFlrSF'] + df['2ndFlrSF']
df['AltTotalSF'] = df['TotalBsmtSF'] + df['ScreenPorch'] + df['WoodDeckSF'] + df['OpenPorchSF'] + df['EnclosedPorch']\
+ df['GarageArea'] + df['LowQualFinSF']<data_type_conversions>
%matplotlib inline
warnings.filterwarnings('ignore', category=UserWarning, module='bs4')
date_numericals.remove('YrSold')
 for date_col in date_numericals:
df[date_col] = df['YrSold'] - df[date_col]
df['RemodelledFlag'] =(df['YearBuilt'] != df['YearRemodAdd'] ).astype(int )<prepare_x_and_y>
train = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep='\t')
 test = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/test.tsv.zip', sep='\t')
 train.shape, test.shape
outlier_mask =(df['TotalSF'] > 4000)&(df['train_set'] == 1)
 outlier_mask_target = outlier_mask[df['train_set'] == 1]
df = df[~outlier_mask]
y = y[~outlier_mask_target.values]<create_dataframe>
lemmatizer = WordNetLemmatizer()
skew_array = skew(df[continuous_feats].fillna(df[continuous_feats].median()), axis=0)
 skew_df = pd.DataFrame({'column': continuous_feats, 'skew': skew_array} ).sort_values(by='skew', ascending=False )<feature_engineering>
def data_preprocessing(df):
reviews = []
for raw in tqdm(df['Phrase']):
text = BeautifulSoup(raw, 'lxml' ).get_text()
letters_only = re.sub('[^a-zA-Z]', ' ', text)
 words = word_tokenize(letters_only.lower())
 stops = set(stopwords.words('english'))
non_stopwords = [word for word in words if not word in stops]
lemma_words = [lemmatizer.lemmatize(word)for word in non_stopwords]
reviews.append(lemma_words)
 return reviews
%time train_sentences = data_preprocessing(train)
 %time test_sentences = data_preprocessing(test)
 len(train_sentences), len(test_sentences )
df[continuous_feats] = np.log1p(df[continuous_feats].fillna(df[continuous_feats].median()))
log_y = np.log1p(y )<normalization>
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence
scaler = MinMaxScaler()
scaled_df = pd.DataFrame(data=scaler.fit_transform(df),
columns=df.columns)
 scaled_df.head()<drop_column>
target = train.Sentiment.values
y_target = to_categorical(target)
 num_classes = y_target.shape[1]
train_df = df[df['train_set'] == df['train_set'].max() ].copy()
test_df = df[df['train_set'] == df['train_set'].min() ].copy()
for dataset in [train_df, test_df]:
dataset.drop('train_set', axis=1, inplace=True )<compute_test_metric>
X_train, X_val, y_train, y_val = train_test_split(train_sentences, y_target, test_size=0.2, stratify=y_target )
def rmsle(y_true, y_pred):
assert len(y_pred)== len(y_true), 'Input arrays different lengths'
return np.sqrt(np.mean(np.power(y_pred - y_true, 2)) )<compute_test_metric>
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)
 len(list(unique_words)) , len_max
def rmsle_for_lgbm(preds, train_data):
labels = train_data.get_label()
return 'RMSLE', np.sqrt(np.mean(np.power(np.log1p(preds)- np.log1p(labels), 2))), False<compute_test_metric>
tokenizer = Tokenizer(num_words=len(list(unique_words)))
 tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
 X_val = tokenizer.texts_to_sequences(X_val)
 X_test = tokenizer.texts_to_sequences(test_sentences)
 X_train = sequence.pad_sequences(X_train, maxlen=len_max)
 X_val = sequence.pad_sequences(X_val, maxlen=len_max)
 X_test = sequence.pad_sequences(X_test, maxlen=len_max)
 X_train.shape, X_val.shape, X_test.shape
rmsle_score = make_scorer(rmsle )<compute_train_metric>
from keras.callbacks import EarlyStopping
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense, Dropout
from keras.optimizers import Adam
regressions_to_try = [LinearRegression() , Ridge() , Lasso() , ElasticNet() ]
preds_list = []
for model in regressions_to_try:
scores = cross_validate(model, train_df, log_y, cv=3, scoring=rmsle_score, return_train_score=False)
 preds_list.append(cross_val_predict(model, train_df, log_y, cv=3))
print('{} RSMLE loss = {:.4f}'.format(type(model), scores['test_score'].mean()))<compute_train_metric>
early_stopping = EarlyStopping(min_delta=0.001, mode='max', monitor='val_acc', patience=2)
 callback = [early_stopping]
for alph in [1, 3, 6, 9, 12, 15]:
test_model = Ridge(alpha=alph)
 scores = cross_validate(test_model, train_df, log_y, cv=3, scoring=rmsle_score, return_train_score=False)
 print('Alpha = {}, Ridge RSMLE loss = {:.4f}'.format(alph, scores['test_score'].mean()))<choose_model_class>
model = Sequential()
model.add(Embedding(len(list(unique_words)) , 300, input_length=len_max))
model.add(LSTM(128, dropout=0.5, recurrent_dropout=0.5, return_sequences=True))
model.add(LSTM(64, dropout=0.5, recurrent_dropout=0.5, return_sequences=False))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.005), metrics=['accuracy'])
 model.summary()
lin = Ridge(alpha=9)
 lin.fit(train_df, log_y)
 lin_preds = lin.predict(test_df )<choose_model_class>
%%time
history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, batch_size=256, verbose=1, callbacks=callback )
lgbm_model = lgbm.LGBMRegressor(
 boosting_type='gbdt',
objective='huber',
learning_rate=0.2,
min_child_samples=30,
colsample_bytree=0.9,
max_depth=-1,
num_leaves=31,
reg_lambda=0,
n_estimators=1000 
)
 skgbm_model = GradientBoostingRegressor(
 loss='huber',
n_estimators=700,
alpha=.5,
max_depth=3,
subsample=.6
)<find_best_model_class>
y_pred = model.predict_classes(X_test)
 submission = pd.read_csv('/kaggle/input/sentiment-analysis-on-movie-reviews/sampleSubmission.csv')
 submission.Sentiment = y_pred
submission.to_csv('submission.csv', index=False )