Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<save_to_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import datetime
import calendar
test['selected_text'] = all
test[['textID','selected_text']].to_csv('submission.csv',index=False)
 pd.set_option('max_colwidth', 60)
 test.sample(25 )<load_from_csv>
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
test_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv' )<define_variables>
from keras.models import Sequential
from keras.layers.recurrent import LSTM, SimpleRNN
from keras.layers.core import Dense, Activation, Dropout
from keras.optimizers import RMSprop,Adam
os.listdir('/kaggle/input')
 model_path = '.. /input/tf-roberta/'
weights_path = '.. /input/tfrobertaweights/tf_roBERTa-weights/'<define_variables>
warnings.filterwarnings("ignore" )
weights_list = []
weights_list = list(os.listdir(weights_path)) 
<define_variables>
train_data = pd.read_csv(path_in + 'train.csv', parse_dates = ['datetime'],
index_col='datetime', infer_datetime_format=True)
 test_data = pd.read_csv(path_in + 'test.csv', parse_dates = ['datetime'],
index_col='datetime', infer_datetime_format=True)
 samp_subm = pd.read_csv(path_in+'sampleSubmission.csv', parse_dates = ['datetime'],
index_col='datetime', infer_datetime_format=True )
weights_path_latest = '.. /input/v18robertaweights/'
weights_path_latest_list = []
for root, dirs, files in os.walk(weights_path_latest):
for file in files:
if file.endswith(".h5"):
weights_path_latest_list.append(file )<create_dataframe>
def rmse(y_true, y_pred):

 return K.sqrt(K.mean(K.square(y_pred - y_true)) )
<import_modules>
num_months_per_year = 12
year_list = [2011, 2012]
print('TF version',tf.__version__ )<define_variables>
train_data_temp = pd.DataFrame(columns=train_data.columns)
 for year in year_list:
for month in range(num_months_per_year):
start_date = datetime.datetime(year, month+1, 1, 0, 0, 0)
 end_date = datetime.datetime(year, month+1, 19, 23, 0, 0)
 temp = train_data[start_date:end_date].resample('H' ).asfreq()
features_fill_zero = ['casual', 'registered', 'count']
temp[features_fill_zero] = temp[features_fill_zero].fillna(0)
 features_fill_bbfil = ['season', 'holiday', 'workingday', 'weather']
temp[features_fill_bbfil] = temp[features_fill_bbfil].fillna(method='bfill')
 features_fill_linear = ['temp', 'atemp', 'humidity', 'windspeed']
temp[features_fill_linear] = temp[features_fill_linear].interpolate(method='linear')
 train_data_temp = train_data_temp.append(temp)
 train_data = train_data_temp
MAX_LEN = 96
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=model_path+'vocab-roberta-base.json',
merges_file=model_path+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}<categorify>
test_data_temp = pd.DataFrame(columns=test_data.columns)
 for year in year_list:
for month in range(num_months_per_year):
start_date = datetime.datetime(year, month+1, 20, 0, 0, 0)
 last_day_of_month = calendar.monthrange(year,month+1)[1]
end_date = datetime.datetime(year, month+1, last_day_of_month, 23, 0, 0)
 temp = test_data[start_date:end_date].resample('H' ).asfreq()
features_fill_bbfil = ['season', 'holiday', 'workingday', 'weather']
temp[features_fill_bbfil] = temp[features_fill_bbfil].fillna(method='bfill')
 features_fill_linear = ['temp', 'atemp', 'humidity', 'windspeed']
temp[features_fill_linear] = temp[features_fill_linear].interpolate(method='linear')
 test_data_temp = test_data_temp.append(temp)
 test_data = test_data_temp
ct = test_df.shape[0]
input_ids_t = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(test_df.shape[0]):
text1 = " "+" ".join(test_df.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[test_df.loc[k,'sentiment']]
input_ids_t[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask_t[k,:len(enc.ids)+5] = 1<load_pretrained>
train_data['weekday'] = train_data.index.weekday
train_data['hour'] = train_data.index.hour
test_data['weekday'] = test_data.index.weekday
test_data['hour'] = test_data.index.hour
def build_model() :
ids = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 att = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 tok = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 config = RobertaConfig.from_pretrained(model_path+'config-roberta-base.json')
 bert_model = TFRobertaModel.from_pretrained(model_path+'pretrained-roberta-base.h5',config=config)
 x = bert_model(ids,attention_mask=att,token_type_ids=tok)
 x1 = tf.keras.layers.Dropout(0.15 )(x[0])
 x1 = tf.keras.layers.Conv1D(512,2, padding='same' )(x1)
 x1 = tf.keras.layers.Conv1D(256, 2,padding='same' )(x1)
 x1 = tf.keras.layers.Dense(1 )(x1)
 x1 = tf.keras.layers.Flatten()(x1)
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.15 )(x[0])
 x2 = tf.keras.layers.Conv1D(512,2, padding='same' )(x2)
 x2 = tf.keras.layers.Conv1D(256, 2,padding='same' )(x2)
 x2 = tf.keras.layers.Dense(1 )(x2)
 x2 = tf.keras.layers.Flatten()(x2)
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 model = tf.keras.models.Model(inputs=[ids, att, tok], outputs=[x1,x2])
 optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer)
 return model<load_pretrained>
for year in year_list:
for month in range(num_months_per_year):
start_date = datetime.datetime(year, month+1, 1, 0, 0, 0)
 end_date = datetime.datetime(year, month+1, 19, 23, 0, 0)
 count_mean = train_data[start_date:end_date]['count'].mean()
train_data.loc[start_date:end_date, 'count_mean'] = count_mean
start_date = datetime.datetime(year, month+1, 20, 0, 0, 0)
 last_day_of_month = calendar.monthrange(year,month+1)[1]
end_date = datetime.datetime(year, month+1, last_day_of_month, 23, 0, 0)
 test_data.loc[start_date:end_date, 'count_mean'] = count_mean
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
for i, fl in enumerate(weights_path_latest_list):
K.clear_session()
model = build_model()
print('Loading model...')
 model.load_weights(weights_path_latest + weights_path_latest_list[i])
 print(f'Predicting Test...using weights...{weights_path_latest_list[i]}')
 preds = model.predict([input_ids_t,attention_mask_t,token_type_ids_t],verbose=1)
 preds_start += preds[0]/len(weights_path_latest_list)
 preds_end += preds[1]/len(weights_path_latest_list )<string_transform>
def hour_group(s):
if(( 0<=s)&(s<=6)) :
return 1
elif(( s==7)|(s==9)) :
return 2
elif(( s==8)|(s==16)|(s==19)) :
return 3
elif(( 10<=s)&(s<=15)) :
return 4
elif(( s==17)|(s==18)) :
return 5
elif(20<=s):
return 6
submission = []
for i in range(input_ids_t.shape[0]):
a = np.argmax(preds_start[i,])
 b = np.argmax(preds_end[i,])
 if a>b:
st = test_df.loc[i,'text']
else:
text1 = " "+" ".join(test_df.loc[i,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 submission.append(st)
<feature_engineering>
train_data['hour_group'] = train_data['hour'].apply(hour_group)
 test_data['hour_group'] = test_data['hour'].apply(hour_group )
test_df['selected_text'] = submission
pd.set_option('max_colwidth', 50)
<save_to_csv>
submission_df.to_csv('submission.csv',index=False)
 print('Submitted Successfully.....' )<set_options>
warnings.filterwarnings('ignore' )<set_options>
scale_features = ['temp', 'atemp', 'humidity', 'hour', 'windspeed']
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed = 42
seed_everything(seed )<feature_engineering>
feature_list = ['holiday', 'workingday', 'weather', 'temp', 'atemp',
'humidity', 'windspeed', 'hour_group',
'hour', 'weekday', 'count_mean']
no_features = ['casual', 'registered']
class TweetDataset(torch.utils.data.Dataset):
def __init__(self, df, max_len=196):
self.df = df
self.max_len = max_len
self.labeled = 'selected_text' in df
self.tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file='.. /input/roberta-base/vocab.json',
merges_file='.. /input/roberta-base/merges.txt',
lowercase=True,
add_prefix_space=True)
 def __getitem__(self, index):
data = {}
row = self.df.iloc[index]
ids, masks, tweet, offsets = self.get_input_data(row)
 data['ids'] = ids
data['masks'] = masks
data['tweet'] = tweet
data['offsets'] = offsets
if self.labeled:
start_idx, end_idx = self.get_target_idx(row, tweet, offsets)
 data['start_idx'] = start_idx
data['end_idx'] = end_idx
return data
def __len__(self):
return len(self.df)
 def get_input_data(self, row):
tweet = " " + " ".join(row.text.lower().split())
 encoding = self.tokenizer.encode(tweet)
 sentiment_id = self.tokenizer.encode(row.sentiment ).ids
ids = [0] + sentiment_id + [2, 2] + encoding.ids + [2]
offsets = [(0, 0)] * 4 + encoding.offsets + [(0, 0)]
pad_len = self.max_len - len(ids)
 if pad_len > 0:
ids += [1] * pad_len
offsets += [(0, 0)] * pad_len
ids = torch.tensor(ids)
 masks = torch.where(ids != 1, torch.tensor(1), torch.tensor(0))
offsets = torch.tensor(offsets)
 return ids, masks, tweet, offsets
def get_target_idx(self, row, tweet, offsets):
selected_text = " " + " ".join(row.selected_text.lower().split())
 len_st = len(selected_text)- 1
idx0 = None
idx1 = None
for ind in(i for i, e in enumerate(tweet)if e == selected_text[1]):
if " " + tweet[ind: ind+len_st] == selected_text:
idx0 = ind
idx1 = ind + len_st - 1
break
char_targets = [0] * len(tweet)
 if idx0 != None and idx1 != None:
for ct in range(idx0, idx1 + 1):
char_targets[ct] = 1
target_idx = []
for j,(offset1, offset2)in enumerate(offsets):
if sum(char_targets[offset1: offset2])> 0:
target_idx.append(j)
 start_idx = target_idx[0]
end_idx = target_idx[-1]
return start_idx, end_idx
def get_train_val_loaders(df, train_idx, val_idx, batch_size=8):
train_df = df.iloc[train_idx]
val_df = df.iloc[val_idx]
train_loader = torch.utils.data.DataLoader(
 TweetDataset(train_df),
batch_size=batch_size,
shuffle=True,
num_workers=2,
drop_last=True)
 val_loader = torch.utils.data.DataLoader(
 TweetDataset(val_df),
batch_size=batch_size,
shuffle=False,
num_workers=2)
 dataloaders_dict = {"train": train_loader, "val": val_loader}
return dataloaders_dict
def get_test_loader(df, batch_size=32):
loader = torch.utils.data.DataLoader(
 TweetDataset(df),
batch_size=batch_size,
shuffle=False,
num_workers=2)
 return loader<load_pretrained>
train_data['workingday'].value_counts()
class TweetModel(nn.Module):
def __init__(self):
super(TweetModel, self ).__init__()
config = RobertaConfig.from_pretrained(
 '.. /input/roberta-base/config.json', output_hidden_states=True)
 self.roberta = RobertaModel.from_pretrained(
 '.. /input/roberta-base/pytorch_model.bin', config=config)
 self.dropout = nn.Dropout(0.5)
 self.fc = nn.Linear(config.hidden_size, 2)
 nn.init.normal_(self.fc.weight, std=0.02)
 nn.init.normal_(self.fc.bias, 0)
 def forward(self, input_ids, attention_mask):
_, _, hs = self.roberta(input_ids, attention_mask)
 x = torch.stack([hs[-1], hs[-2], hs[-3]])
 x = torch.mean(x, 0)
 x = self.dropout(x)
 x = self.fc(x)
 start_logits, end_logits = x.split(1, dim=-1)
 start_logits = start_logits.squeeze(-1)
 end_logits = end_logits.squeeze(-1)
 return start_logits, end_logits<compute_test_metric>
lookback = 3
horizon = 2
month = 1
year = 2011
def loss_fn(start_logits, end_logits, start_positions, end_positions):
ce_loss = nn.CrossEntropyLoss()
start_loss = ce_loss(start_logits, start_positions)
 end_loss = ce_loss(end_logits, end_positions)
 total_loss = start_loss + end_loss
return total_loss<statistical_test>
def create_data(data, lookback, horizon, start, end, kind):
X, y = [], []
if kind == 'train':
start_shifted = start
else:
start_shifted = start - datetime.timedelta(hours=lookback)
 temp = data[start_shifted:end].copy()
temp.index = range(len(temp.index))
start_ix = lookback
n_samples = int(( len(temp.index)-lookback)/horizon)
 for i in range(n_samples):
end_ix = start_ix+horizon
seq_X = temp[(start_ix-lookback):start_ix][data.columns.difference(no_features)].values
seq_y = temp[start_ix:end_ix]['count'].values
X.append(seq_X)
 y.append(seq_y)
 start_ix = end_ix
return np.array(X), np.array(y )
def get_selected_text(text, start_idx, end_idx, offsets):
selected_text = ""
for ix in range(start_idx, end_idx + 1):
selected_text += text[offsets[ix][0]: offsets[ix][1]]
if(ix + 1)< len(offsets)and offsets[ix][1] < offsets[ix + 1][0]:
selected_text += " "
return selected_text
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))
def compute_jaccard_score(text, start_idx, end_idx, start_logits, end_logits, offsets):
start_pred = np.argmax(start_logits)
 end_pred = np.argmax(end_logits)
 if start_pred > end_pred:
pred = text
else:
pred = get_selected_text(text, start_pred, end_pred, offsets)
 true = get_selected_text(text, start_idx, end_idx, offsets)
 return jaccard(true, pred )<train_model>
lookback = 24
horizon = 1
month = 12
year = 2012
start_train = datetime.datetime(year, month, 1, 0, 0, 0)
 end_train = datetime.datetime(year, month, 19, 23, 0, 0)
 start_test = datetime.datetime(year, month, 20, 0, 0, 0)
 last_day_of_month = calendar.monthrange(year,month)[1]
end_test = datetime.datetime(year, month, last_day_of_month, 23, 0, 0)
 data = pd.concat([train_data[start_train:end_train],
test_data[start_test:end_test]])
 features_no_scale = ['season', 'holiday', 'workingday', 'count_mean']
data_scaled = data.copy()
data_mean = data_scaled.mean(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] -= data_mean
data_std = data_scaled.std(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] /= data_std
X_train, y_train = create_data(data_scaled, lookback, horizon, start_train, end_train, 'train')
 print(X_train[0])
 n_steps = X_train.shape[1]
n_features = X_train.shape[2]
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=(n_steps, n_features)))
 model.add(SimpleRNN(64, return_sequences=True, input_shape=(n_steps, n_features)))
 model.add(LSTM(32))
model.add(Dense(horizon))
optimizer = Adam(lr=1e-4)
 model.compile(optimizer=optimizer, loss='mse', metrics = ['mse'])
 history = model.fit(X_train, y_train,
epochs=1, verbose=1)
 start_test_temp = start_test
n_days = last_day_of_month-19
for i in range(n_days*24+1):
end_test_temp = start_test+datetime.timedelta(hours=i)
 X_test, y_test = create_data(data_scaled, lookback, horizon, start_test_temp, end_test_temp, 'test')
 if X_test.size != 0:
y_test = model.predict(X_test, verbose=0)
 data_scaled.loc[start_test_temp:(end_test_temp-datetime.timedelta(hours=1)) , 'count'] = y_test[0]
start_test_temp = end_test_temp
samp_subm.loc[start_test:end_test, 'count'] = data_scaled.loc[samp_subm[start_test:end_test].index]['count']*data_std['count']+data_mean['count']
samp_subm['count'] = np.where(samp_subm['count']<0, 0, samp_subm['count'])
 samp_subm['count'] = samp_subm['count'].interpolate()
def train_model(model, dataloaders_dict, criterion, optimizer, num_epochs, filename):
model.cuda()
best_jaccard = 0.0
for epoch in range(num_epochs):
for phase in ['train', 'val']:
if phase == 'train':
model.train()
else:
model.eval()
epoch_loss = 0.0
epoch_jaccard = 0.0
for data in(dataloaders_dict[phase]):
ids = data['ids'].cuda()
masks = data['masks'].cuda()
tweet = data['tweet']
offsets = data['offsets'].numpy()
start_idx = data['start_idx'].cuda()
end_idx = data['end_idx'].cuda()
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
start_logits, end_logits = model(ids, masks)
 loss = criterion(start_logits, end_logits, start_idx, end_idx)
 if phase == 'train':
loss.backward()
optimizer.step()
epoch_loss += loss.item() * len(ids)
 start_idx = start_idx.cpu().detach().numpy()
end_idx = end_idx.cpu().detach().numpy()
start_logits = torch.softmax(start_logits, dim=1 ).cpu().detach().numpy()
end_logits = torch.softmax(end_logits, dim=1 ).cpu().detach().numpy()
for i in range(len(ids)) :
jaccard_score = compute_jaccard_score(
 tweet[i],
start_idx[i],
end_idx[i],
start_logits[i],
end_logits[i],
offsets[i])
 epoch_jaccard += jaccard_score
epoch_loss = epoch_loss / len(dataloaders_dict[phase].dataset)
 epoch_jaccard = epoch_jaccard / len(dataloaders_dict[phase].dataset)
 print('Epoch {}/{} | {:^5} | Loss: {:.4f} | Jaccard: {:.4f}'.format(
 epoch + 1, num_epochs, phase, epoch_loss, epoch_jaccard))
if(phase == 'val' and epoch_jaccard > best_jaccard):
print('Jaccard score improved! Saving model...')
 torch.save(model.state_dict() , filename)
 best_jaccard = epoch_jaccard<choose_model_class>
lookback = 24
horizon = 1
n_months = 12
year_list = [2011, 2012]
num_epochs = 1
batch_size = 32
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed )<load_from_csv>
n_samples = 0
n_features = len(train_data[train_data.columns.difference(no_features)].columns)
 print('features used: ', train_data[train_data.columns.difference(no_features)].columns)
 X_train, y_train = np.empty(shape=(n_samples, lookback, n_features)) , np.empty(shape=(n_samples, horizon))
for year in year_list:
for month in range(1,n_months+1):
start_train = datetime.datetime(year, month, 1, 0, 0, 0)
 end_train = datetime.datetime(year, month, 19, 23, 0, 0)
 data = train_data[start_train:end_train]
features_no_scale = ['season', 'holiday', 'workingday', 'count_mean']
data_scaled = data.copy()
data_mean = data_scaled.mean(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] -= data_mean
data_std = data_scaled.std(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] /= data_std
X_temp, y_temp = create_data(data_scaled, lookback, horizon, start_train, end_train, 'train')
 X_train = np.vstack(( X_train, X_temp))
y_train = np.vstack(( y_train, y_temp))
<load_from_csv>
n_steps = X_train.shape[1]
n_features = X_train.shape[2]
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=(n_steps, n_features), dropout=0.0, recurrent_dropout=0.2,))
model.add(LSTM(64, return_sequences=True, input_shape=(n_steps, n_features), dropout=0.0, recurrent_dropout=0.2,))
model.add(LSTM(32))
model.add(Dense(horizon))
optimizer = Adam(lr=1e-4)
 model.compile(optimizer=optimizer, loss='mse', metrics = ['mse'])
 history = model.fit(X_train, y_train,
validation_split=0.1,
epochs=150, verbose=1 )
%%time
test_df = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 test_df['text'] = test_df['text'].astype(str)
 test_loader = get_test_loader(test_df)
 predictions = []
models = []
for fold in range(skf.n_splits):
model = TweetModel()
model.cuda()
model.load_state_dict(torch.load(f'/kaggle/input/tweet-sentiment-submission-file/roberta_fold{fold+1}.pth'))
model.eval()
models.append(model)
 for data in test_loader:
ids = data['ids'].cuda()
masks = data['masks'].cuda()
tweet = data['tweet']
offsets = data['offsets'].numpy()
start_logits = []
end_logits = []
for model in models:
with torch.no_grad() :
output = model(ids, masks)
 start_logits.append(torch.softmax(output[0], dim=1 ).cpu().detach().numpy())
 end_logits.append(torch.softmax(output[1], dim=1 ).cpu().detach().numpy())
 start_logits = np.mean(start_logits, axis=0)
 end_logits = np.mean(end_logits, axis=0)
 for i in range(len(ids)) :
start_pred = np.argmax(start_logits[i])
 end_pred = np.argmax(end_logits[i])
 if start_pred > end_pred:
pred = tweet[i]
else:
pred = get_selected_text(tweet[i], start_pred, end_pred, offsets[i])
 predictions.append(pred )<save_to_csv>
for year in year_list:
for month in range(1,n_months+1):
start_train = datetime.datetime(year, month, 1, 0, 0, 0)
 end_train = datetime.datetime(year, month, 19, 23, 0, 0)
 start_test = datetime.datetime(year, month, 20, 0, 0, 0)
 last_day_of_month = calendar.monthrange(year,month)[1]
end_test = datetime.datetime(year, month, last_day_of_month, 23, 0, 0)
 data = pd.concat([train_data[start_train:end_train],
test_data[start_test:end_test]])
 features_no_scale = ['season', 'holiday', 'workingday', 'count_mean']
data_scaled = data.copy()
data_mean = data_scaled.mean(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] -= data_mean
data_std = data_scaled.std(axis=0)
 data_scaled[data.columns.difference(features_no_scale)] /= data_std
start_test_temp = start_test
n_days = last_day_of_month-19
for i in range(n_days*24+1):
end_test_temp = start_test+datetime.timedelta(hours=i)
 X_test, y_test = create_data(data_scaled, lookback, horizon, start_test_temp, end_test_temp, 'test')
 if X_test.size != 0:
y_test = model.predict(X_test, verbose=0)
 data_scaled.loc[start_test_temp:(end_test_temp-datetime.timedelta(hours=1)) , 'count'] = y_test[0]
start_test_temp = end_test_temp
samp_subm.loc[start_test:end_test, 'count'] = data_scaled.loc[samp_subm[start_test:end_test].index]['count']*data_std['count']+data_mean['count']
samp_subm['count'] = np.where(samp_subm['count']<0, 0, samp_subm['count'])
 samp_subm['count'] = samp_subm['count'].interpolate()
sub_df = pd.read_csv('.. /input/tweet-sentiment-extraction/sample_submission.csv')
 sub_df['selected_text'] = predictions
sub_df['selected_text'] = sub_df['selected_text'].apply(lambda x: x.replace('!!!!', '!')if len(x.split())==1 else x)
 sub_df['selected_text'] = sub_df['selected_text'].apply(lambda x: x.replace('.. ', '.')if len(x.split())==1 else x)
 sub_df['selected_text'] = sub_df['selected_text'].apply(lambda x: x.replace('...', '.')if len(x.split())==1 else x)
 sub_df.to_csv('submission.csv', index=False)
 sub_df.head()<save_to_csv>
output = pd.DataFrame({'datetime': samp_subm.index,
'count': samp_subm['count']})
 output.to_csv('submission.csv', index=False )