Kaggle competition: Sentiment Analysis on Movie Reviews
df.loc[df['Embarked'].isnull() ]<feature_engineering>
train_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep = '\t')
 train_data.head()
def FillEmbk(data):
var = 'Embarked'
data.loc[(data.Embarked.isnull()),'Embarked']= 'C'
FillEmbk(df )<categorify>
test_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip',sep = '\t')
 test_data.head()
def LablFunc(data):
lst = {'Embarked','Sex'}
for i in lst:
le.fit(data[i].astype(str))
data[i] = le.transform(data[i].astype(str))
LablFunc(df )<normalization>
import matplotlib.pyplot as plt
import tensorflow as tf
target = data['Survived'].values
select_features = ['Pclass', 'Age','AgeCat','SibSp', 'Parch', 'Fare',
'Embarked', 'TicketId', 'CabinNum', 'Title','Cabin',
'FareCat', 'FamlSize','FamilySurv','Sex']
scaler = StandardScaler()
dfScaled = scaler.fit_transform(df[select_features])
 train = dfScaled[0:891].copy()
test = dfScaled[891:].copy()<train_model>
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
selector = SelectKBest(f_classif, len(select_features))
selector.fit(train, target)
 scores = -np.log10(selector.pvalues_)
 indices = np.argsort(scores)[::-1]
print('Features importance:')
 for i in range(len(scores)) :
print('%.2f %s' %(scores[indices[i]], select_features[indices[i]]))<import_modules>
print("TF version: ", tf.__version__)
 if tf.__version__ < "2.0.0":
tf.enable_eager_execution()
print("Eager execution enabled.")
 else:
print("Eager execution enabled by default.")
 if tf.test.gpu_device_name() :
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF" )
from sklearn.model_selection import KFold, cross_val_score
from sklearn.ensemble import RandomForestClassifier<train_model>
print(train_data['Sentiment'].unique())
 train_data['Sentiment'].nunique()
SrchRFC = RandomForestClassifier(max_depth = 5, min_samples_split = 4, n_estimators = 500,
random_state = 20, n_jobs = -1)
 SrchRFC.fit(train, target )<compute_test_metric>
train_data['Sentiment'].value_counts()
prc = SrchRFC.predict(train)
 accuracy_score(target,prc )<predict_on_test>
from tqdm import tqdm
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import re
prdt2 = SrchRFC.predict(test)
 print('Predicted result: ', prdt2 )<save_to_csv>
def sentence_cleaning(df):
sentence = []
for sent in tqdm(df['Phrase']):
text = re.sub("[^a-zA-Z]"," ",sent)
 word = word_tokenize(text.lower())
 lemmatizer = WordNetLemmatizer()
lemm_word = [lemmatizer.lemmatize(i)for i in word]
sentence.append(lemm_word)
 return(sentence )
sampl['Survived'] = pd.DataFrame(prdt2)
 sampl.to_csv('submission.csv', index=False )<load_from_csv>
target_col = train_data.Sentiment.values
y_target = to_categorical(target_col)
 y_target.shape
train = pd.read_csv('.. /input/train.csv' , index_col= 'Id')
 test = pd.read_csv('.. /input/test.csv' , index_col= 'Id')
 train = train[train.GrLivArea < 4500]
train.reset_index(drop=True, inplace=True)
 label = train[['SalePrice']]
train.drop('SalePrice' , axis = 1 , inplace=True)
 train.head(3 )<count_unique_values>
X_train,X_val,y_train,y_val = train_test_split(train_sent,y_target,test_size = 0.2,stratify = y_target )
numerical_col = []
cat_col = []
for x in train.columns:
if train[x].dtype == 'object':
cat_col.append(x)
 print(x+': ' + str(len(train[x].unique())))
 else:
numerical_col.append(x)
 print('CAT col
', cat_col)
 print('Numerical col
')
 print(numerical_col )<drop_column>
unique_words = set()
max_len = 0
for sent in tqdm(X_train):
unique_words.update(sent)
 if(max_len < len(sent)) :
max_len = len(sent)
 sentence = sent
numerical_col.remove('MSSubClass')
 cat_col.append('MSSubClass' )<train_model>
vocabulary = len(list(unique_words))
oov = '<OOV>'
embedding_dim = 300
padding = 'post'
trunc = 'post'
imputer = Imputer(missing_values='NaN' , strategy='median' , axis = 0)
 imputer = imputer.fit(train_num)
 train_num = imputer.transform(train_num )<categorify>
tokenizer = Tokenizer(num_words = vocabulary,oov_token = oov,char_level = False)
 tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
 X_train = pad_sequences(X_train,maxlen = max_len,padding=padding,truncating = trunc)
 X_val = tokenizer.texts_to_sequences(X_val)
 X_val = pad_sequences(X_val,maxlen = max_len,padding=padding,truncating = trunc)
 X_test = tokenizer.texts_to_sequences(test_sent)
 X_test = pad_sequences(X_test,maxlen = max_len,padding=padding,truncating = trunc )
test_num = imputer.transform(test[numerical_col] )<split>
from keras.models import Sequential
from keras.layers import Dense,Bidirectional,Activation,Dropout,LSTM,Embedding
from keras.layers.embeddings import Embedding
X_train , X_test , y_train , y_test= train_test_split(train_num , label , test_size= 0.2 , random_state=123 )<compute_train_metric>
model = tf.keras.Sequential()
model.add(Embedding(vocabulary,embedding_dim,input_length = max_len))
model.add(Bidirectional(LSTM(128, dropout = 0.8, recurrent_dropout=0.8, return_sequences=True)))
 model.add(Bidirectional(LSTM(128,dropout = 0.5,recurrent_dropout=0.5,return_sequences=False)))
 model.add(Dense(64,activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(5,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 model.summary()
clf = LinearRegression(normalize=True)
 scores = cross_val_score(clf, X_train, y_train, cv=5 ).mean()
scores<compute_train_metric>
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'] )
clf = Lasso(alpha=0.3, normalize=True)
 scores = cross_val_score(clf, X_train, y_train, cv=5 ).mean()
scores<compute_train_metric>
hist_model = model.fit(X_train,y_train,
validation_data =(X_val, y_val),
epochs = 4,
batch_size = 256,
verbose = 1 )
clf = ElasticNet(alpha=1, l1_ratio=0.5, normalize=False)
 scores = cross_val_score(clf, X_train, y_train, cv=5 ).mean()
scores<compute_train_metric>
test_id = test_data['PhraseId']
test_id
clf=xgboost.XGBRegressor(n_estimators=100, learning_rate=0.08, gamma=0, subsample=0.75,
colsample_bytree=1, max_depth=7)
 scores = cross_val_score(clf, X_train, y_train, cv=5 ).mean()
scores<train_model>
y_pred = np.argmax(model.predict(X_test), axis = -1)
 submission_df = pd.DataFrame({'PhraseId': test_id, 'Sentiment': y_pred})
 submission_df.to_csv('submission.csv', index=False)
 submission_df.head()