Kaggle competition: Sentiment Analysis on Movie Reviews
ensembles_to_try = [lgbm_model, skgbm_model]
preds_list = []
for i in range(len(ensembles_to_try)) :
scores = cross_validate(ensembles_to_try[i], train_df, log_y,
cv=3,
scoring=rmsle_score,
return_train_score=False)
 preds_list.append(cross_val_predict(ensembles_to_try[i], train_df, log_y, cv=3))
print('{} RSMLE loss = {:.4f}'.format(type(ensembles_to_try[i]), scores['test_score'].mean()))<predict_on_test>
lemmatizer = WordNetLemmatizer()
gbm = skgbm_model.fit(train_df, log_y)
 gbm_preds = gbm.predict(test_df )<prepare_output>
!unzip.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip
linear_ratio =.6
averaged_preds =(1-linear_ratio)*gbm_preds + linear_ratio*lin_preds<save_to_csv>
!unzip.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip
output = pd.DataFrame({'Id': test_ids, 'SalePrice': np.expm1(averaged_preds)})
 output.to_csv('submission.csv', index=False )<set_options>
train= pd.read_csv("./train.tsv", sep="\t")
 test = pd.read_csv("./test.tsv", sep="\t")
 print(train.shape,test.shape)
 train.head()
%matplotlib inline<load_from_csv>
train.loc[train['SentenceId'] == 1]
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 combine = [train_df, test_df]<count_values>
def clean_sentences(df):
reviews = []
for sent in tqdm(df['Phrase']):
review_text = BeautifulSoup(sent ).get_text()
review_text = re.sub("[^a-zA-Z]"," ", review_text)
 words = word_tokenize(review_text.lower())
 lemma_words = [lemmatizer.lemmatize(i)for i in words]
reviews.append(lemma_words)
 return(reviews )
print("Percentage of survival in the train set: {}%".format(round(sum(train_df.Survived)/train_df.Survived.count() , 2)) )<sort_values>
train_sentences = clean_sentences(train)
 test_sentences = clean_sentences(test)
 print(len(train_sentences))
print(len(test_sentences))
train_df[['Pclass', 'Survived']].groupby(['Pclass'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )<sort_values>
target=train.Sentiment.values
y_target=to_categorical(target)
 num_classes=y_target.shape[1]
train_df[["Sex", "Survived"]].groupby(['Sex'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )<sort_values>
X_train,X_val,y_train,y_val = train_test_split(train_sentences,y_target,
test_size=0.2,stratify=y_target )
train_df[["SibSp", "Survived"]].groupby(['SibSp'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )<sort_values>
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(len(list(unique_words)))
 print(len_max )
train_df[["Parch", "Survived"]].groupby(['Parch'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )<sort_values>
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)
 print(X_train.shape,X_val.shape,X_test.shape )
train_df[["Embarked", "Survived"]].groupby(['Embarked'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )<groupby>
early_stopping = EarlyStopping(min_delta = 0.001, mode = 'max', monitor='val_acc', patience = 2)
 callback = [early_stopping]
for dataset in combine:
df = dataset.groupby(['Sex', 'Pclass'] ).size().unstack(0)
 df['fem_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )<groupby>
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()
train_df[["Sex", "Pclass", "Survived"]].groupby(['Sex', 'Pclass'],
as_index=True ).mean()<feature_engineering>
history=model.fit(X_train, y_train, validation_data=(X_val, y_val),
epochs=6, batch_size=256, verbose=1, callbacks=callback )
<groupby><EOS> 
y_pred=model.predict_classes(X_test)
 sub_file = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/sampleSubmission.csv',sep=',')
 sub_file.Sentiment=y_pred
sub_file.to_csv('Submission.csv',index=False )