Kaggle competition: Natural Language Processing with Disaster Tweets
categoricalFeatureNames = ["season","holiday","workingday","weather","dayofweek","month","year","hour"]
numericalFeatureNames = ["temp","humidity","windspeed","atemp"]
dropFeatures = ['casual',"count","datetime","day","registered"]<data_type_conversions>
tweet = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 submission = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv' )
for var in categoricalFeatureNames:
train[var] = train[var].astype("category")
 test[var] = test[var].astype("category" )<prepare_x_and_y>
def create_corpus(target):
corpus=[]
for x in tweet[tweet['target']==target]['text'].str.split() :
for i in x:
corpus.append(i)
 return corpus
datetimecol = test["datetime"]
yLabels = train["count"]
yLablesRegistered = train["registered"]
yLablesCasual = train["casual"]<drop_column>
def create_corpus_df(tweet, target):
corpus=[]
for x in tweet[tweet['target']==target]['text'].str.split() :
for i in x:
corpus.append(i)
 return corpus
train = train.drop(dropFeatures,axis=1)
 test = test.drop(['datetime','day'],axis=1 )<compute_test_metric>
df = pd.concat([tweet,test])
 df.shape
def rmsle(y, y_,convertExp=True):
if convertExp:
y = np.exp(y),
y_ = np.exp(y_)
 log1 = np.nan_to_num(np.array([np.log(v + 1)for v in y]))
log2 = np.nan_to_num(np.array([np.log(v + 1)for v in y_]))
calc =(log1 - log2)** 2
return np.sqrt(np.mean(calc))<choose_model_class>
df['location'].replace({'United States':'USA',
'New York':'USA',
"London":'UK',
"Los Angeles, CA":'USA',
"Washington, D.C.":'USA',
"California":'USA',
"Chicago, IL":'USA',
"Chicago":'USA',
"New York, NY":'USA',
"California, USA":'USA',
"FLorida":'USA',
"Nigeria":'Africa',
"Kenya":'Africa',
"Everywhere":'Worldwide',
"San Francisco":'USA',
"Florida":'USA',
"United Kingdom":'UK',
"Los Angeles":'USA',
"Toronto":'Canada',
"San Francisco, CA":'USA',
"NYC":'USA',
"Seattle":'USA',
"Earth":'Worldwide',
"Ireland":'UK',
"London, England":'UK',
"New York City":'USA',
"Texas":'USA',
"London, UK":'UK',
"Atlanta, GA":'USA',
"Mumbai":"India"},inplace=True)
 sns.barplot(y = df['location'].value_counts() [:5].index, x = df['location'].value_counts() [:5],
palette='autumn', orient='h' )
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning)
 lModel = LinearRegression()
yLabelsLog = np.log1p(yLabels)
 lModel.fit(X = train,y = yLabelsLog)
 preds = lModel.predict(X= train)
 print("RMSLE Value For Linear Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))<train_on_grid>
def clean_text(text):

 text = text.lower()
text = re.sub('\[.*?\]', '', text)
 text = re.sub('https?://\S+|www\.\S+', '', text)
 text = re.sub('<.*?>+', '', text)
 text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
 text = re.sub('
', '', text)
 text = re.sub('\w*\d\w*', '', text)
 return text
df['text'] = df['text'].apply(lambda x: clean_text(x))
df['text'].head()
ridge_m_ = Ridge()
ridge_params_ = { 'max_iter':[3000],'alpha':[0.1, 1, 2, 3, 4, 10, 30,100,200,300,400,800,900,1000]}
rmsle_scorer = metrics.make_scorer(rmsle, greater_is_better=False)
 grid_ridge_m = GridSearchCV(ridge_m_,
ridge_params_,
scoring = rmsle_scorer,
cv=5)
 yLabelsLog = np.log1p(yLabels)
 grid_ridge_m.fit(train, yLabelsLog)
 preds = grid_ridge_m.predict(X= train)
 print(grid_ridge_m.best_params_)
 print("RMSLE Value For Ridge Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(12,5)
 df = pd.DataFrame(grid_ridge_m.cv_results_)
 df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sns.pointplot(data=df,x="alpha",y="rmsle",ax=ax )<train_on_grid>
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 df['text']=df['text'].apply(lambda x: remove_emoji(x))
lasso_m_ = Lasso()
alpha = 1/np.array([0.1, 1, 2, 3, 4, 10, 30,100,200,300,400,800,900,1000])
 lasso_params_ = { 'max_iter':[3000],'alpha':alpha}
grid_lasso_m = GridSearchCV(lasso_m_,lasso_params_,scoring = rmsle_scorer,cv=5)
 yLabelsLog = np.log1p(yLabels)
 grid_lasso_m.fit(train, yLabelsLog)
 preds = grid_lasso_m.predict(X= train)
 print(grid_lasso_m.best_params_)
 print("RMSLE Value For Lasso Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(12,5)
 df = pd.DataFrame(grid_lasso_m.cv_results_)
 df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sns.pointplot(data=df,x="alpha",y="rmsle",ax=ax)
<train_model>
tweet_1 = tweet.text.values
test_1 = test.text.values
sentiments = tweet.target.values
rfModel = RandomForestRegressor(n_estimators=100)
 yLabelsLog = np.log1p(yLabels)
 rfModel.fit(train,yLabelsLog)
 preds = rfModel.predict(X= train)
 print("RMSLE Value For Random Forest: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))<train_model>
word_tokenizer = Tokenizer()
word_tokenizer.fit_on_texts(tweet_1)
 vocab_length = len(word_tokenizer.word_index)+ 1
gbm = GradientBoostingRegressor(n_estimators=4000,alpha=0.01)
 yLabelsLog = np.log1p(yLabels)
 gbm.fit(train,yLabelsLog)
 preds = gbm.predict(X= train)
 print("RMSLE Value For Gradient Boost: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))<data_type_conversions>
def metrics(pred_tag, y_test):
print("F1-score: ", f1_score(pred_tag, y_test))
print("Precision: ", precision_score(pred_tag, y_test))
print("Recall: ", recall_score(pred_tag, y_test))
print("Acuracy: ", accuracy_score(pred_tag, y_test))
print("-"*50)
 print(classification_report(pred_tag, y_test))
def embed(corpus):
return word_tokenizer.texts_to_sequences(corpus )
for var in categoricalFeatureNames:
train[var] = train[var].astype("float")
 test[var] = test[var].astype("float" )<train_model>
longest_train = max(tweet_1, key=lambda sentence: len(word_tokenize(sentence)))
 length_long_sentence = len(word_tokenize(longest_train))
padded_sentences = pad_sequences(embed(tweet_1), length_long_sentence, padding='post')
 test_sentences = pad_sequences(
 embed(test_1),
length_long_sentence,
padding='post'
)
xgb = XGBRegressor(n_estimators = 500, learning_rate = 0.1, max_depth = 4)
 yLabelsLog = np.log1p(yLabels)
 xgb.fit(train,yLabelsLog)
 preds = xgb.predict(train)
 print("RMSLE Value For XGBboost: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))<save_to_csv>
embeddings_dictionary = dict()
embedding_dim = 100
glove_file = open('.. /input/glove-file/glove.6B.100d.txt')
 for line in glove_file:
records = line.split()
word = records[0]
vector_dimensions = np.asarray(records[1:], dtype='float32')
 embeddings_dictionary [word] = vector_dimensions
glove_file.close()
submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_gbm_separate_without_fe.csv', index=False )<feature_engineering>
embedding_matrix = np.zeros(( vocab_length, embedding_dim))
for word, index in word_tokenizer.word_index.items() :
embedding_vector = embeddings_dictionary.get(word)
 if embedding_vector is not None:
embedding_matrix[index] = embedding_vector
train = pd.read_csv(".. /input/train.csv", parse_dates = ["datetime"])
 test = pd.read_csv(".. /input/test.csv", parse_dates = ["datetime"])
 train["year"] = train["datetime"].dt.year
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
test["year"] = test["datetime"].dt.year
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek
y_casual = np.log1p(train.casual)
 y_registered = np.log1p(train.registered)
 train.drop(["datetime", "windspeed", "casual", "registered", "count"], 1, inplace=True)
 test.drop(["datetime", "windspeed", ], 1, inplace=True)
 hyperparameters = { 'colsample_bytree': 0.725, 'learning_rate': 0.013,
'num_leaves': 56, 'reg_alpha': 0.754, 'reg_lambda': 0.071,
'subsample': 0.523, 'n_estimators': 1093}
model = lgb.LGBMRegressor(**hyperparameters)
 model.fit(train, y_casual)
 preds1 = model.predict(test)
 hyperparameters = { 'colsample_bytree': 0.639, 'learning_rate': 0.011,
'num_leaves': 30, 'reg_alpha': 0.351, 'reg_lambda': 0.587,
'subsample': 0.916, 'n_estimators': 2166}
model = lgb.LGBMRegressor(**hyperparameters,)
 model.fit(train, y_registered)
 preds2 = model.predict(test)
 submission=pd.read_csv(".. /input/sampleSubmission.csv")
 submission["count"] = np.expm1(preds1)+ np.expm1(preds2)
<feature_engineering>
X_train, X_test, y_train, y_test = train_test_split(
 padded_sentences,
sentiments,
test_size=0.25
)
pd.options.display.max_rows = 200
submission["holiday"] = test["holiday"]
submission.loc[(submission["holiday"]==1)]<prepare_x_and_y>
def BLSTM() :
model = Sequential()
model.add(Embedding(input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights = [embedding_matrix],
input_length=length_long_sentence))
model.add(Bidirectional(LSTM(length_long_sentence, return_sequences = True, recurrent_dropout=0.2)))
 model.add(GlobalMaxPool1D())
 model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
 return model
submission.iloc[1258:1269, 1]= submission.iloc[1258:1269, 1]*0.5
submission.iloc[4492:4515, 1]= submission.iloc[4492:4515, 1]*0.5
submission.iloc[6308:6330, 1]= submission.iloc[6308:6330, 1]*0.5
submission.iloc[3041:3063, 1]= submission.iloc[3041:3063, 1]*0.5
submission.iloc[6332:6354, 1]= submission.iloc[6332:6354, 1]*0.5
submission.iloc[3065:3087, 1]= submission.iloc[3065:3087, 1]*0.5
submission.iloc[5992:6015, 1]= submission.iloc[5992:6015, 1]*0.5
submission.iloc[2771:2794, 1]= submission.iloc[2771:2794, 1]*0.5<save_to_csv>
model = BLSTM()
checkpoint = ModelCheckpoint(
 'model.h5',
monitor = 'val_loss',
verbose = 1,
save_best_only = True 
)
 reduce_lr = ReduceLROnPlateau(
 monitor = 'val_loss',
factor = 0.2,
verbose = 1,
patience = 5,
min_lr = 0.001 
)
 history = model.fit(
 X_train,
y_train,
epochs = 7,
batch_size = 32,
validation_data = [X_test, y_test],
verbose = 1,
callbacks = [reduce_lr, checkpoint]
)
submission.drop("holiday",1,inplace=True)
 submission.to_csv("allrf2.csv", index=False )<load_from_csv>
loss, accuracy = model.evaluate(X_test, y_test)
 print('Loss:', loss)
 print('Accuracy:', accuracy )
dfs = {}
for name in ['train','test']:
df = pd.read_csv('/kaggle/input/bike-sharing-demand/%s.csv' % name)
 df['_data'] = name
dfs[name] = df<concatenate>
preds = model.predict_classes(X_test)
 metrics(preds, y_test )
df = dfs['train'].append(dfs['test'])
 df.columns = map(str.lower, df.columns )<feature_engineering>
model.load_weights('model.h5')
 preds = model.predict_classes(X_test)
 metrics(preds, y_test )
<feature_engineering><EOS> 
submission.target = model.predict_classes(test_sentences)
 submission.to_csv("submission.csv", index=False)
 submission.target.value_counts().plot.bar() ;