Kaggle competition: Sentiment Analysis on Movie Reviews
dropp = ['MiscFeature' , 'PoolQC' , 'Fence' ,'Alley' ]
train_cat.drop(columns=dropp , axis=1, inplace=True )<data_type_conversions>
try:
%tensorflow_version 2.x
except Exception:
pass
lemmatizer = WordNetLemmatizer()
test_cat.drop(columns=dropp , axis=1, inplace=True)
 test_cat = test_cat.astype('category')
 test_cat.info()<categorify>
tf.random.set_seed(123)
 random.seed(123 )
most_freq = {}
for col in train_cat.columns:
p = train_cat[col].mode() [0]
train_cat[col].fillna(p, inplace=True)
 most_freq[col] = p<data_type_conversions>
for zip_path in ['.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip',
'.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip']:
with ZipFile(zip_path, 'r')as zip:
zip.printdir()
print('Extracting all the files now...')
 zip.extractall()
print('Done!' )
for col in train_cat.columns:
test_cat[col].fillna(most_freq[col], inplace=True )<prepare_output>
sample = pd.read_csv(".. /input/sentiment-analysis-on-movie-reviews/sampleSubmission.csv")
 train = pd.read_csv("train.tsv", delimiter='\t')
 test = pd.read_csv("test.tsv", delimiter='\t' )
train_num =pd.DataFrame(train_num)
 train_num.head(2 )<prepare_output>
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)
 train_sentences = clean_sentences(train)
 test_sentences = clean_sentences(test)
 print(len(train_sentences))
print(len(test_sentences))
test_num =pd.DataFrame(test_num)
 test_num.head(2 )<feature_engineering>
target=train.Sentiment.values
y_target=to_categorical(target)
 num_classes=y_target.shape[1]
for col in train_cat:
train_cat[col] = train_cat[col].cat.codes
for col in test_cat:
test_cat[col] = test_cat[col].cat.codes<feature_engineering>
X_train,X_val,y_train,y_val = train_test_split(train_sentences,
y_target,
test_size=0.2,
stratify=y_target )
train_num.index = train_cat.index<feature_engineering>
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 )
test_num.index = test_cat.index<categorify>
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_cat = pd.get_dummies(train_cat)
 test_cat = pd.get_dummies(test_cat )<merge>
early_stopping = EarlyStopping(min_delta = 0.001, mode = 'max', monitor='val_acc', patience = 2)
 callback = [early_stopping]
train_ = train_num.join(train_cat )<merge>
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()
test_ = test_num.join(test_cat )<normalization>
history=model.fit(X_train, y_train, validation_data=(X_val, y_val),epochs=6, batch_size=256, verbose=1, callbacks=callback )
scalar = MinMaxScaler()
train_ = scalar.fit_transform(train_)
 test_ = scalar.transform(test_ )<choose_model_class>
y_pred=model.predict_classes(X_test )
lightgbm = lgb.LGBMRegressor(objective='regression',
num_leaves=8,
learning_rate=0.03,
n_estimators=4000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1, 
)
 scores = cross_val_score(lightgbm, train_, label, cv=5 ).mean()
scores<save_to_csv>
sample.Sentiment=y_pred
sample.to_csv('Submission.csv',index=False )