Kaggle competition: Sentiment Analysis on Movie Reviews
classifier = XGBClassifier(colsample_bylevel= 0.9,
colsample_bytree = 0.8,
gamma=0.99,
max_depth= 5,
min_child_weight= 1,
n_estimators= 10,
nthread= 4,
random_state= 2,
silent= True)
 classifier.fit(x_train,y_train)
 classifier.score(x_test,y_test )<load_from_csv>
train_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep = '\t')
 train_data.head()
test_data = pd.read_csv('.. /input/test.csv')
 test_x = test_data[features]<count_missing_values>
test_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip',sep = '\t')
 test_data.head()
test_x.isnull().sum()<data_type_conversions>
import matplotlib.pyplot as plt
import tensorflow as tf
test_x['Age'] = test_x['Age'].fillna(test_x['Age'].median())
 test_x['Fare'] = test_x['Fare'].fillna(test_x['Fare'].median() )<count_missing_values>
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
test_x.isnull().sum()<categorify>
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" )
test_x['Sex'] = LE.fit_transform(test_x['Sex'])
 test_x['Embarked'] = LE.fit_transform(test_x['Embarked'] )<predict_on_test>
print(train_data['Sentiment'].unique())
 train_data['Sentiment'].nunique()
prediction = classifier.predict(test_x )<save_to_csv>
train_data['Sentiment'].value_counts()
output = pd.DataFrame({'PassengerId': test_data.PassengerId,'Survived': prediction})
 output.to_csv('submission.csv', index=False)
 output.head() 
<load_from_csv>
from tqdm import tqdm
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import re
data = pd.read_csv('.. /input/train.csv')
 target = pd.read_csv('.. /input/test.csv')
 data['logSalePrice'] = np.log10(data.SalePrice.values)
 data.drop('SalePrice', axis=1, inplace=True )<load_from_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 )
print(open('.. /input/data_description.txt', 'r' ).read() )<drop_column>
target_col = train_data.Sentiment.values
y_target = to_categorical(target_col)
 y_target.shape
continuous_features = [col for col in data.columns if data[col].dtype != 'O']
continuous_features.remove('Id')
 continuous_features.remove('logSalePrice')
 ordinal_features = ['Street', 'Alley', 'LotShape', 'Utilities', 'LandSlope', 'ExterQual',
'LandContour', 'ExterCond', 'BsmtQual', 'BsmtCond', 'BsmtExposure',
'BsmtFinType1', 'BsmtFinType2', 'HeatingQC', 'CentralAir',
'Electrical', 'KitchenQual', 'Functional', 'FireplaceQu', 'GarageType',
'GarageFinish', 'GarageQual', 'GarageCond', 'PavedDrive', 'PoolQC',
'Fence']
categorical_features = ['LotConfig', 'Neighborhood', 'Condition1', 'Condition2',
'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd',
'MasVnrType', 'Foundation', 'Heating', 'MiscFeature',
'SaleType', 'SaleCondition', 'MSZoning', 'BldgType'] 
(len(set(continuous_features + ordinal_features + categorical_features)) ==
len(continuous_features + ordinal_features + categorical_features)==
len(data.columns)-2 )<count_values>
X_train,X_val,y_train,y_val = train_test_split(train_sent,y_target,test_size = 0.2,stratify = y_target )
print('incomplete features in data:
' +
"
".join([" {}: {} useful data points".format(col, data[col].dropna().count())
 for col in data.columns if data[col].dropna().count() < 1460]), '
')
 print('incomplete features in target:
' +
"
".join([" {}: {} useful data points".format(col, target[col].dropna().count())
 for col in target.columns if target[col].dropna().count() < 1459]))<feature_engineering>
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
slope, intercept, rvalue, pvalue, stderr = linregress(
 np.sqrt(data.loc[(data.LotFrontage.notna())&(data.LotArea < 20000), 'LotArea']),
data.loc[(data.LotFrontage.notna())&(data.LotArea < 20000), 'LotFrontage'])
 residual = data.LotFrontage -(slope*np.sqrt(data.LotArea)+intercept)
 print('mean residual:', np.nanmean(residual), '
median residual:',
np.nanmedian(residual), '
residual std:', np.nanstd(residual))
data.loc[data.LotFrontage.isna() , 'LotFrontage'] = np.sqrt(data.LotArea)*slope+intercept
target.loc[target.LotFrontage.isna() , 'LotFrontage'] = np.sqrt(target.LotArea)*slope+intercept<data_type_conversions>
vocabulary = len(list(unique_words))
oov = '<OOV>'
embedding_dim = 300
padding = 'post'
trunc = 'post'
for col in ['Alley', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1',
'BsmtFinType2', 'FireplaceQu', 'GarageType', 'GarageFinish', 'GarageQual',
'GarageCond', 'PoolQC', 'Fence', 'MiscFeature']:
data.loc[:, col].fillna('NA', inplace=True)
 target.loc[:, col].fillna('NA', inplace=True )<count_values>
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 )
for sample in [data, target]:
sample.fillna({col: sample.loc[:, col].median() for col in continuous_features}, inplace=True)
 sample.fillna({col: sample.loc[:, col].value_counts().index[0] for col in categorical_features + ordinal_features},
inplace=True )<count_values>
from keras.models import Sequential
from keras.layers import Dense,Bidirectional,Activation,Dropout,LSTM,Embedding
from keras.layers.embeddings import Embedding
print('incomplete features in data:',
len([" {}: {} useful data points".format(col, data[col].dropna().count())
 for col in data.columns if data[col].dropna().count() < 1460]), '
')
 print('incomplete features in target:',
len([" {}: {} useful data points".format(col, target[col].dropna().count())
 for col in target.columns if target[col].dropna().count() < 1459]))<define_variables>
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()
ordinal_transform = {'Street': {'Grvl': 1, 'Pave': 2},
'Alley': {'NA': 0, 'Grvl': 1, 'Pave': 2},
'LotShape': {'IR3': 1, 'IR2': 2, 'IR1': 3, 'Reg': 4},
'Utilities': {'ELO': 1, 'NoSeWa': 2, 'NoSewr': 3, 'AllPub': 4},
'LandSlope': {'Sev': 1, 'Mod': 2, 'Gtl': 3},
'LandContour': {'Low': 1, 'HLS': 1, 'Bnk': 2, 'Lvl': 3},
'ExterQual': {'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'ExterCond': {'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'BsmtQual': {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'BsmtCond': {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'BsmtExposure': {'NA': 0, 'No': 1, 'Mn': 2, 'Av': 3, 'Gd': 4},
'BsmtFinType1': {'NA': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5,
'GLQ': 6},
'BsmtFinType2': {'NA': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5,
'GLQ': 6},
'HeatingQC': {'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'CentralAir': {'N': 1, 'Y': 2},
'Electrical': {'Mix': 1, 'FuseP': 2, 'FuseF': 3, 'FuseA': 4, 'SBrkr': 5},
'KitchenQual': {'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'Functional': {'Sal': 1, 'Sev': 2, 'Maj2': 3, 'Maj1': 4, 'Mod': 5, 'Min2': 6,
'Min1': 7, 'Typ': 8},
'FireplaceQu': {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'GarageType': {'NA': 0, 'Detchd': 1, 'CarPort': 2, 'BuiltIn': 3,
'Basment': 4, 'Attchd': 5, '2Types': 6},
'GarageFinish': {'NA': 0, 'Unf': 1, 'RFn': 2, 'Fin': 3},
'GarageQual': {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'GarageCond': {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'PavedDrive': {'N': 1, 'P': 2, 'Y': 3},
'PoolQC': {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5},
'Fence': {'NA': 0, 'MnWw': 1, 'GdWo': 2, 'MnPrv': 3, 'GdPrv': 4},
}
for col in ordinal_features:
data.loc[:, col] = data.loc[:, col].map(ordinal_transform[col], na_action='ignore')
 target.loc[:, col] = target.loc[:, col].map(ordinal_transform[col], na_action='ignore')
 for col in ['BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath', 'BedroomAbvGr',
'KitchenAbvGr', 'TotRmsAbvGrd', 'Fireplaces', 'GarageCars', 'MoSold', 'YrSold']:
continuous_features.remove(col)
 ordinal_features.append(col)
 continuous_features.remove('MSSubClass')
 categorical_features.append('MSSubClass' )<categorify>
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'] )
enc = MultiLabelBinarizer()
enc.fit([data.Condition1, data.Condition2])
 binarized_columns = pd.DataFrame(enc.transform(list(zip(data.Condition1, data.Condition2))),
columns=enc.classes_, index=data.Id)
 data = pd.merge(data, binarized_columns, on='Id')
 data.drop(['Condition1', 'Condition2'], axis=1, inplace=True)
 binarized_columns = pd.DataFrame(enc.transform(list(zip(target.Condition1, target.Condition2))),
columns=enc.classes_, index=target.Id)
 target = pd.merge(target, binarized_columns, on='Id')
 target.drop(['Condition1', 'Condition2'], axis=1, inplace=True)
 categorical_features.remove('Condition1')
 categorical_features.remove('Condition2' )<categorify>
hist_model = model.fit(X_train,y_train,
validation_data =(X_val, y_val),
epochs = 4,
batch_size = 256,
verbose = 1 )
enc = LabelBinarizer()
ordinalized = []
binary_features = []
binarize = ['Neighborhood', 'MSSubClass']
for col in categorical_features:
if col not in binarize:
sorted_labels = data[
[col, 'logSalePrice']].groupby(col ).logSalePrice.median().sort_values()
data.loc[:, col] = data.loc[:, col].map(
 {sorted_labels.index.values[i]:i for i in range(len(sorted_labels)) })
 target.loc[:, col] = target.loc[:, col].map(
 {sorted_labels.index.values[i]:i for i in range(len(sorted_labels)) })
 ordinalized.append(col)
 else:
enc.fit(data[col])
 binarized_columns = pd.DataFrame(enc.transform(data[col]),
columns=enc.classes_, index=data.Id)
 data = pd.merge(data, binarized_columns, on='Id')
 data.drop(col, axis=1, inplace=True)
 binarized_columns = pd.DataFrame(enc.transform(target[col]),
columns=enc.classes_, index=target.Id)
 target = pd.merge(target, binarized_columns, on='Id')
 target.drop(col, axis=1, inplace=True)
 binary_features += list(enc.classes_)
 for col in binarize:
categorical_features.remove(col)
 for col in ordinalized:
categorical_features.remove(col)
 ordinal_features.append(col )<categorify>
test_id = test_data['PhraseId']
test_id
poly = PolynomialFeatures(degree=2, include_bias=True)
 poldata = poly.fit_transform(data[['squarefeet', 'kitchenbaths',
'totalquality']] ).transpose()
for colid in range(poldata.shape[0]):
data['poly_{}'.format(colid)] = poldata[colid]
poldata = poly.fit_transform(target[['squarefeet', 'kitchenbaths',
'totalquality']] ).transpose()
for colid in range(poldata.shape[0]):
target['poly_{}'.format(colid)] = poldata[colid]<train_on_grid>
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()