Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<load_from_csv>
plt.style.use('ggplot')
 stop=set(stopwords.words('english'))
def scale_fields(fn_train='train_x.csv', fn_test='test_x.csv',
fn_out_train='scaled_train_X.csv' , fn_out_test='scaled_test_X.csv'):
train_X = pd.read_csv(os.path.join(OUTPUT_DIR, fn_train))
try:
train_X.drop(labels=['seg_id', 'seg_start', 'seg_end'], axis=1, inplace=True)
 except:
pass
test_X = pd.read_csv(os.path.join(OUTPUT_DIR, fn_test))
print('start scaler')
 scaler = StandardScaler()
scaler.fit(train_X)
 scaled_train_X = pd.DataFrame(scaler.transform(train_X), columns=train_X.columns)
 scaled_test_X = pd.DataFrame(scaler.transform(test_X), columns=test_X.columns)
 scaled_train_X.to_csv(os.path.join(OUTPUT_DIR, fn_out_train), index=False)
 scaled_test_X.to_csv(os.path.join(OUTPUT_DIR, fn_out_test), index=False )<split>
import os
split_raw_data()
build_rnd_idxs()
run_mp_build()
join_mp_build()
build_test_fields()
scale_fields()<feature_engineering>
tweet= pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test=pd.read_csv('.. /input/nlp-getting-started/test.csv')
 tweet.head(3 )
def create_features_pk_det(seg_id, seg, X, st, end):
X.loc[seg_id, 'seg_id'] = np.int32(seg_id)
 X.loc[seg_id, 'seg_start'] = np.int32(st)
 X.loc[seg_id, 'seg_end'] = np.int32(end)
 sig = pd.Series(seg['acoustic_data'].values)
 b, a = des_bw_filter_lp(cutoff=18000)
 sig = sg.lfilter(b, a, sig)
 peakind = []
noise_pct =.001
count = 0
while len(peakind)< 12 and count < 24:
peakind = sg.find_peaks_cwt(sig, np.arange(1, 16), noise_perc=noise_pct, min_snr=4.0)
 noise_pct *= 2.0
count += 1
if len(peakind)< 12:
print('Warning: Failed to find 12 peaks for %d' % seg_id)
 while len(peakind)< 12:
peakind.append(149999)
 df_pk = pd.DataFrame(data={'pk': sig[peakind], 'idx': peakind}, columns=['pk', 'idx'])
 df_pk.sort_values(by='pk', ascending=False, inplace=True)
 for i in range(0, 12):
X.loc[seg_id, 'pk_idx_%d' % i] = df_pk['idx'].iloc[i]
X.loc[seg_id, 'pk_val_%d' % i] = df_pk['pk'].iloc[i]
return X<load_from_csv>
def create_corpus(target):
corpus=[]
for x in tweet[tweet['target']==target]['text'].str.split() :
for i in x:
corpus.append(i)
 return corpus
df = pd.read_csv('test_x_8pk.csv')
 df_out = None
for pks in df.itertuples() :
data = {'pk_idxs': [pks.pk_idx_0, pks.pk_idx_1, pks.pk_idx_2, pks.pk_idx_3, pks.pk_idx_4, pks.pk_idx_5, pks.pk_idx_6, pks.pk_idx_7, pks.pk_idx_8, pks.pk_idx_9, pks.pk_idx_10, pks.pk_idx_11],
'pk_vals': [pks.pk_val_0, pks.pk_val_1, pks.pk_val_2, pks.pk_val_3, pks.pk_val_4, pks.pk_val_5, pks.pk_val_6, pks.pk_val_7, pks.pk_val_8, pks.pk_val_9, pks.pk_val_10, pks.pk_val_11]}
pdf = pd.DataFrame(data=data)
 pdf.sort_values(by='pk_idxs', axis=0, inplace=True)
 data = {'pk_idx_0': pdf['pk_idxs'].iloc[0], 'pk_val_0': pdf['pk_vals'].iloc[0],
'pk_idx_1': pdf['pk_idxs'].iloc[1], 'pk_val_1': pdf['pk_vals'].iloc[1],
'pk_idx_2': pdf['pk_idxs'].iloc[2], 'pk_val_2': pdf['pk_vals'].iloc[2],
'pk_idx_3': pdf['pk_idxs'].iloc[3], 'pk_val_3': pdf['pk_vals'].iloc[3],
'pk_idx_4': pdf['pk_idxs'].iloc[4], 'pk_val_4': pdf['pk_vals'].iloc[4],
'pk_idx_5': pdf['pk_idxs'].iloc[5], 'pk_val_5': pdf['pk_vals'].iloc[5],
'pk_idx_6': pdf['pk_idxs'].iloc[6], 'pk_val_6': pdf['pk_vals'].iloc[6],
'pk_idx_7': pdf['pk_idxs'].iloc[7], 'pk_val_7': pdf['pk_vals'].iloc[7],
'pk_idx_8': pdf['pk_idxs'].iloc[8], 'pk_val_8': pdf['pk_vals'].iloc[8],
'pk_idx_9': pdf['pk_idxs'].iloc[9], 'pk_val_9': pdf['pk_vals'].iloc[9],
'pk_idx_10': pdf['pk_idxs'].iloc[10], 'pk_val_10': pdf['pk_vals'].iloc[10],
'pk_idx_11': pdf['pk_idxs'].iloc[11], 'pk_val_11': pdf['pk_vals'].iloc[11]}
if df_out is None:
df_out = pd.DataFrame(data=data, index=[0])
 else:
temp = pd.DataFrame(data=data, index=[0])
 df_out = df_out.append(temp, ignore_index=True)
 df_out = df_out[['pk_idx_0', 'pk_val_0',
'pk_idx_1', 'pk_val_1',
'pk_idx_2', 'pk_val_2',
'pk_idx_3', 'pk_val_3',
'pk_idx_4', 'pk_val_4',
'pk_idx_5', 'pk_val_5',
'pk_idx_6', 'pk_val_6',
'pk_idx_7', 'pk_val_7',
'pk_idx_8', 'pk_val_8',
'pk_idx_9', 'pk_val_9',
'pk_idx_10', 'pk_val_10',
'pk_idx_11', 'pk_val_11']]
print(df_out.head())
 print(df_out.tail())
 df_out.to_csv('test_x_8pk_by_idx.csv' )<save_to_csv>
corpus=create_corpus(0)
 dic=defaultdict(int)
 for word in corpus:
if word in stop:
dic[word]+=1
top=sorted(dic.items() , key=lambda x:x[1],reverse=True)[:10]
pk_idx_base = 'pk_idx_'
pk_val_base = 'pk_val_'
print('do train')
 df = pd.read_csv(r'pk8/train_x_8pk.csv')
 slopes = np.zeros(( len(df.index), 12))
for i in df.index:
for j in range(12):
pk_idx = pk_idx_base + str(j)
 pk_val = pk_val_base + str(j)
 slopes[i, j] = df[pk_val].iloc[i] /(150000 - df[pk_idx].iloc[i])
 for j in range(12):
df['slope_' + str(j)] = slopes[:, j]
print(df.head())
 df.to_csv(r'pk8/train_x_8_slope.csv', index=False)
 df = pd.read_csv(r'pk8/test_x_8pk.csv')
 slopes = np.zeros(( len(df.index), 12))
print('do test')
 for i in df.index:
for j in range(12):
pk_idx = pk_idx_base + str(j)
 pk_val = pk_val_base + str(j)
 slopes[i, j] = df[pk_val].iloc[i] /(150000 - df[pk_idx].iloc[i])
 for j in range(12):
df['slope_' + str(j)] = slopes[:, j]
print(df.head())
 df.to_csv(r'pk8/test_x_8_slope.csv', index=False)
 print('!DONE!' )<load_pretrained>
counter=Counter(corpus)
 most=counter.most_common()
x=[]
y=[]
for word,count in most[:40]:
if(word not in stop):
x.append(word)
 y.append(count )
params = {'num_leaves': 21,
'min_data_in_leaf': 20,
'objective':'regression',
'learning_rate': 0.001,
'max_depth': 108,
"boosting": "gbdt",
"feature_fraction": 0.91,
"bagging_freq": 1,
"bagging_fraction": 0.91,
"bagging_seed": 42,
"metric": 'mae',
"lambda_l1": 0.1,
"verbosity": -1,
"random_state": 42}
def lgb_base_model() :
maes = []
rmses = []
submission = pd.read_csv(os.path.join(DATA_DIR, 'sample_submission.csv'), index_col='seg_id')
 scaled_train_X = pd.read_csv(r'train_8_and_9\scaled_train_X_8.csv')
 scaled_test_X = pd.read_csv(r'train_8_and_9\scaled_test_X_8.csv')
 train_y = pd.read_csv(r'train_8_and_9\train_y_8.csv')
 predictions = np.zeros(len(scaled_test_X))
n_fold = 8
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = scaled_train_X.columns
for fold_,(trn_idx, val_idx)in enumerate(folds.split(scaled_train_X, train_y.values)) :
print('working fold %d' % fold_)
 strLog = "fold {}".format(fold_)
 print(strLog)
 X_tr, X_val = scaled_train_X.iloc[trn_idx], scaled_train_X.iloc[val_idx]
y_tr, y_val = train_y.iloc[trn_idx], train_y.iloc[val_idx]
model = lgb.LGBMRegressor(**params, n_estimators=80000, n_jobs=-1)
 model.fit(X_tr, y_tr,
eval_set=[(X_tr, y_tr),(X_val, y_val)], eval_metric='mae',
verbose=1000, early_stopping_rounds=200)
 preds = model.predict(scaled_test_X, num_iteration=model.best_iteration_)
 predictions += preds / folds.n_splits
preds = model.predict(X_val, num_iteration=model.best_iteration_)
 mae = mean_absolute_error(y_val, preds)
 print('MAE: %.6f' % mae)
 maes.append(mae)
 rmse = mean_squared_error(y_val, preds)
 print('RMSE: %.6f' % rmse)
 rmses.append(rmse)
 fold_importance_df['importance_%d' % fold_] = model.feature_importances_[:len(scaled_train_X.columns)]
print('MAEs', maes)
 print('MAE mean: %.6f' % np.mean(maes))
print('RMSEs', rmses)
 print('RMSE mean: %.6f' % np.mean(rmses))
submission.time_to_failure = predictions
submission.to_csv('submission_lgb_8_80k_108dp.csv', index=False)
 fold_importance_df.to_csv('fold_imp_lgb_8_80k_108dp.csv' )<choose_model_class>
def get_top_tweet_bigrams(corpus, n=None):
vec = CountVectorizer(ngram_range=(2, 2)).fit(corpus)
 bag_of_words = vec.transform(corpus)
 sum_words = bag_of_words.sum(axis=0)
 words_freq = [(word, sum_words[0, idx])for word, idx in vec.vocabulary_.items() ]
words_freq =sorted(words_freq, key = lambda x: x[1], reverse=True)
 return words_freq[:n]
params = {'num_leaves': 21,
'min_data_in_leaf': 20,
'objective':'regression',
'max_depth': 108,
'learning_rate': 0.001,
"boosting": "gbdt",
"feature_fraction": 0.91,
"bagging_freq": 1,
"bagging_fraction": 0.91,
"bagging_seed": 42,
"metric": 'mae',
"lambda_l1": 0.1,
"verbosity": -1,
"random_state": 42}
def lgb_trimmed_model() :
maes = []
rmses = []
tr_maes = []
tr_rmses = []
submission = pd.read_csv(os.path.join(DATA_DIR, 'sample_submission.csv'), index_col='seg_id')
 scaled_train_X = pd.read_csv(r'pk8/scaled_train_X_8.csv')
 df = pd.read_csv(r'pk8/scaled_train_X_8_slope.csv')
 scaled_train_X = scaled_train_X.join(df)
 scaled_test_X = pd.read_csv(r'pk8/scaled_test_X_8.csv')
 df = pd.read_csv(r'pk8/scaled_test_X_8_slope.csv')
 scaled_test_X = scaled_test_X.join(df)
 pcol = []
pcor = []
pval = []
y = pd.read_csv(r'pk8/train_y_8.csv')['time_to_failure'].values
for col in scaled_train_X.columns:
pcol.append(col)
 pcor.append(abs(pearsonr(scaled_train_X[col], y)[0]))
pval.append(abs(pearsonr(scaled_train_X[col], y)[1]))
df = pd.DataFrame(data={'col': pcol, 'cor': pcor, 'pval': pval}, index=range(len(pcol)))
 df.sort_values(by=['cor', 'pval'], inplace=True)
 df.dropna(inplace=True)
 df = df.loc[df['pval'] <= 0.05]
drop_cols = []
for col in scaled_train_X.columns:
if col not in df['col'].tolist() :
drop_cols.append(col)
 scaled_train_X.drop(labels=drop_cols, axis=1, inplace=True)
 scaled_test_X.drop(labels=drop_cols, axis=1, inplace=True)
 train_y = pd.read_csv(r'pk8/train_y_8.csv')
 predictions = np.zeros(len(scaled_test_X))
preds_train = np.zeros(len(scaled_train_X))
print('shapes of train and test:', scaled_train_X.shape, scaled_test_X.shape)
 n_fold = 6
folds = KFold(n_splits=n_fold, shuffle=False, random_state=42)
 for fold_,(trn_idx, val_idx)in enumerate(folds.split(scaled_train_X, train_y.values)) :
print('working fold %d' % fold_)
 strLog = "fold {}".format(fold_)
 print(strLog)
 X_tr, X_val = scaled_train_X.iloc[trn_idx], scaled_train_X.iloc[val_idx]
y_tr, y_val = train_y.iloc[trn_idx], train_y.iloc[val_idx]
model = lgb.LGBMRegressor(**params, n_estimators=60000, n_jobs=-1)
 model.fit(X_tr, y_tr,
eval_set=[(X_tr, y_tr),(X_val, y_val)], eval_metric='mae',
verbose=1000, early_stopping_rounds=200)
 preds = model.predict(scaled_test_X)
 predictions += preds / folds.n_splits
preds = model.predict(scaled_train_X)
 preds_train += preds / folds.n_splits
preds = model.predict(X_val)
 mae = mean_absolute_error(y_val, preds)
 print('MAE: %.6f' % mae)
 maes.append(mae)
 rmse = mean_squared_error(y_val, preds)
 print('RMSE: %.6f' % rmse)
 rmses.append(rmse)
 preds = model.predict(X_tr)
 mae = mean_absolute_error(y_tr, preds)
 print('Tr MAE: %.6f' % mae)
 tr_maes.append(mae)
 rmse = mean_squared_error(y_tr, preds)
 print('Tr RMSE: %.6f' % rmse)
 tr_rmses.append(rmse)
 print('MAEs', maes)
 print('MAE mean: %.6f' % np.mean(maes))
print('RMSEs', rmses)
 print('RMSE mean: %.6f' % np.mean(rmses))
print('Tr MAEs', tr_maes)
 print('Tr MAE mean: %.6f' % np.mean(tr_maes))
print('Tr RMSEs', rmses)
 print('Tr RMSE mean: %.6f' % np.mean(tr_rmses))
submission.time_to_failure = predictions
submission.to_csv('submission_xgb_slope_pearson_6fold.csv')
 pr_tr = pd.DataFrame(data=preds_train, columns=['time_to_failure'], index=range(0, preds_train.shape[0]))
pr_tr.to_csv(r'preds_tr_xgb_slope_pearson_6fold.csv', index=False)
 print('Train shape: {}, Test shape: {}, Y shape: {}'.format(scaled_train_X.shape, scaled_test_X.shape, train_y.shape))<import_modules>
df=pd.concat([tweet,test])
 df.shape
print(sys.version)
 print('pandas:', pd.__version__)
 print('numpy:', np.__version__)
 print('scipy:', scipy.__version__)
 print('sklearn:', sklearn.__version__)
 print('light gbm:', lgb.__version__)
 print('xgboost:', xgb.__version__ )<import_modules>
example="New competition launched :https://www.kaggle.com/c/nlp-getting-started"
warnings.filterwarnings("ignore")
 PandasDataFrame = TypeVar('pandas.core.frame.DataFrame')
<load_from_csv>
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 remove_URL(example )
IS_LOCAL = False
if(IS_LOCAL):
PATH=".. /input/LANL/"
else:
PATH=".. /input/"
os.listdir(PATH)
 train_X = pd.read_csv(os.path.join(PATH,'.. /input/all-features/train_X.csv'))
train_y = pd.read_csv(os.path.join(PATH,'.. /input/all-features/train_y.csv'))
train_X.shape, train_y.shape<load_from_csv>
df['text']=df['text'].apply(lambda x : remove_URL(x))
submission = pd.read_csv(os.path.join(PATH,'LANL-Earthquake-Prediction/sample_submission.csv'), index_col='seg_id')
 test_X = pd.read_csv(os.path.join(PATH,'.. /input/all-features/test_X.csv'))
submission.shape, test_X.shape<split>
def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 print(remove_html(example))
def fold_generator(x, y, groups=None, num_folds=10, shuffle=True, seed=2019):
folds = KFold(num_folds, shuffle=shuffle, random_state=seed)
 for train_index, test_index in folds.split(x, y, groups):
yield train_index, test_index<train_on_grid>
df['text']=df['text'].apply(lambda x : remove_html(x))
def search_cv(x, y, model, grid, max_iter=None, num_folds=10, shuffle=True):
t0 = time.time()
cv = fold_generator(x, y, num_folds=num_folds)
 if max_iter is None:
search = GridSearchCV(model, grid, cv=cv,
scoring='neg_mean_absolute_error')
 else:
search = RandomizedSearchCV(model, grid, n_iter=max_iter, cv=cv,
scoring='neg_mean_absolute_error')
 search.fit(x, y, silent=True)
 t0 = time.time() - t0
print("Best CV score: {:.4f}, time: {:.1f}s".format(-search.best_score_, t0))
print(search.best_params_)
 return search.best_estimator_<predict_on_test>
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)
 remove_emoji("Omg another Earthquake 😔😔" )
def make_predictions(x, y, model, num_folds=10, shuffle=True, test=None, plot=True):
if test is not None:
sub_prediction = np.zeros(test.shape[0])
 oof_prediction = np.zeros(x.shape[0])
 for tr_idx, val_idx in fold_generator(x, y, num_folds=num_folds):
model.fit(x.iloc[tr_idx], y.iloc[tr_idx])
 oof_prediction[val_idx] = model.predict(x.iloc[val_idx])
 if test is not None:
sub_prediction += model.predict(test)/ num_folds
if plot:
plot_predictions(y, oof_prediction)
 if test is None:
return oof_prediction
else:
return oof_prediction, sub_prediction<normalization>
df['text']=df['text'].apply(lambda x: remove_emoji(x))
scaler = StandardScaler()
scaler.fit(train_X)
 scaled_train_X = pd.DataFrame(scaler.transform(train_X), columns=train_X.columns)
 scaled_test_X = pd.DataFrame(scaler.transform(test_X), columns=test_X.columns)
 scaled_test_X.shape<train_on_grid>
def remove_punct(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 example="I am a
print(remove_punct(example))
grid = {
'depth': [4, 6, 8],
'random_seed' : [400, 100, 200],
'learning_rate' : [0.01, 0.03, 0.1]
}
cat_model = CatBoostRegressor(loss_function='MAE', task_type = "GPU")
 cat_model = search_cv(scaled_train_X, train_y, cat_model, grid, max_iter=3 )<predict_on_test>
df['text']=df['text'].apply(lambda x : remove_punct(x))
oof = make_predictions(scaled_train_X, train_y, cat_model )<predict_on_test>
!pip install pyspellchecker
oof, sub = make_predictions(scaled_train_X, train_y, cat_model,
test=scaled_test_X, plot=False )<save_to_csv>
spell = SpellChecker()
def correct_spellings(text):
corrected_text = []
misspelled_words = spell.unknown(text.split())
 for word in text.split() :
if word in misspelled_words:
corrected_text.append(spell.correction(word))
else:
corrected_text.append(word)
 return " ".join(corrected_text)
 text = "corect me plese"
correct_spellings(text )
submission.time_to_failure = sub
submission.to_csv('submission.csv',index=True )<set_options>
def create_corpus(df):
corpus=[]
for tweet in tqdm(df['text']):
words=[word.lower() for word in word_tokenize(tweet)if(( word.isalpha() ==1)&(word not in stop)) ]
corpus.append(words)
 return corpus
%matplotlib inline
sns.set()
warnings.filterwarnings("ignore" )<load_from_csv>
corpus=create_corpus(df )
train_features_ = pd.read_csv('.. /input/lanl-earthquake-nonmagic-features/train_X.csv')
 train_y_ = pd.read_csv('.. /input/lanl-earthquake-nonmagic-features/train_y.csv')
 train_y_extra_ = pd.read_csv('.. /input/lanl-earthquake-nonmagic-features/train_y_extra.csv')
 test_features_ = pd.read_csv('.. /input/lanl-earthquake-nonmagic-features/test_X.csv' )<create_dataframe>
embedding_dict={}
with open('.. /input/glove-global-vectors-for-word-representation/glove.6B.100d.txt','r')as f:
for line in f:
values=line.split()
word=values[0]
vectors=np.asarray(values[1:],'float32')
 embedding_dict[word]=vectors
f.close()
X_train = train_features_.copy()
X_test = test_features_.copy()
y_train = train_y_<prepare_x_and_y>
MAX_LEN=50
tokenizer_obj=Tokenizer()
tokenizer_obj.fit_on_texts(corpus)
 sequences=tokenizer_obj.texts_to_sequences(corpus)
 tweet_pad=pad_sequences(sequences,maxlen=MAX_LEN,truncating='post',padding='post' )
rows = 150_000
y_train_new = pd.DataFrame(columns=['taf'], dtype=np.float64, index = y_train.index)
 y_train_new['taf'] = train_y_extra_['time_after_failure']<sort_values>
word_index=tokenizer_obj.word_index
print('Number of unique words:',len(word_index))
cols = list(np.abs(X_train.corrwith(y_train_new['taf'])).sort_values(ascending=False ).head(50 ).index)
 cols[::5]<compute_train_metric>
num_words=len(word_index)+1
embedding_matrix=np.zeros(( num_words,100))
for word,i in tqdm(word_index.items()):
if i > num_words:
continue
emb_vec=embedding_dict.get(word)
 if emb_vec is not None:
embedding_matrix[i]=emb_vec
coef = [0.35, 0.5, 0.05, 0.1]
def custom_objective(y_true, y_pred):
c = 0.5
residual = y_pred - y_true
grad = c * residual /(np.abs(residual)+ c)
 hess = c ** 2 /(np.abs(residual)+ c)** 2
h = 1.2
scale = 1 +(residual / h)** 2
scale_sqrt = np.sqrt(scale)
 grad_huber = residual / scale_sqrt
hess_huber = 1 / scale / scale_sqrt
grad_rmse = residual
hess_rmse = 1.0
grad_mae = np.array(residual)
 grad_mae[grad_mae > 0] = 1.
 grad_mae[grad_mae <= 0] = -1.
 hess_mae = 1.0
return coef[0] * grad + coef[1] * grad_huber + coef[2] * grad_rmse + coef[3] * grad_mae, \
coef[0] * hess + coef[1] * hess_huber + coef[2] * hess_rmse + coef[3] * hess_mae
def logcosh_objective(y_true, y_pred):
d = y_pred - y_true
grad = np.tanh(d)/y_true
hess =(1.0 - grad*grad)/y_true
return grad, hess
def huber_objective(y_true, y_pred):
d = y_pred - y_true
h = 1.2
scale = 1 +(d / h)** 2
scale_sqrt = np.sqrt(scale)
 grad = d / scale_sqrt
hess = 1 / scale / scale_sqrt
return grad, hess<init_hyperparams>
model=Sequential()
embedding=Embedding(num_words,100,embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN,trainable=False)
 model.add(embedding)
 model.add(SpatialDropout1D(0.2))
model.add(LSTM(64, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=1e-5)
 model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['accuracy'])
params = {'bagging_fraction': 0.71,
'boosting': 'gbdt',
'feature_fraction': 0.94,
'lambda_l1': 3.131216244016188,
'lambda_l2': 2.4124061313905836,
'learning_rate': 0.049886848207269734,
'max_bin': 193,
'max_depth': 15,
'metric': 'MAE',
'min_data_in_bin': 167,
'min_data_in_leaf': 62,
'min_gain_to_split': 2.07,
'num_leaves': 38,
'objective': custom_objective,
'subsample': 0.9133120405819966}<define_variables>
train=tweet_pad[:tweet.shape[0]]
test=tweet_pad[tweet.shape[0]:]
features_select = ['abs_min',
'abs_q01',
'abs_q05',
'abs_trend',
'autocorrelation_10',
'autocorrelation_1000',
'autocorrelation_500',
'av_change_abs_roll_mean_10',
'av_change_abs_roll_std_10',
'av_change_abs_roll_std_100',
'av_change_abs_roll_std_1000',
'avg_first_10000',
'avg_last_10000',
'avg_last_5000',
'c3_5',
'c3_500',
'classic_sta_lta1_mean',
'classic_sta_lta4_mean',
'classic_sta_lta5_mean',
'count_big_50000_less_threshold_5',
'energy_spectra_2640hz',
'energy_spectra_9306hz',
'energy_spectra_norm_109306hz_denoised',
'energy_spectra_norm_129306hz',
'energy_spectra_norm_135973hz_denoised',
'energy_spectra_norm_142640hz_denoised',
'energy_spectra_norm_149306hz_denoised',
'energy_spectra_norm_15973hz_denoised',
'energy_spectra_norm_169306hz_denoised',
'energy_spectra_norm_22640hz_denoised',
'energy_spectra_norm_29306hz_denoised',
'energy_spectra_norm_35973hz_denoised',
'energy_spectra_norm_42640hz_denoised',
'energy_spectra_norm_49306hz_denoised',
'energy_spectra_norm_55973hz_denoised',
'energy_spectra_norm_62640hz_denoised',
'energy_spectra_norm_89306hz_denoised',
'energy_spectra_norm_9306hz_denoised',
'energy_spectra_norm_95973hz_denoised',
'fft_1000_roll_std_20',
'fft_1000_roll_std_25',
'fft_1000_roll_std_70',
'fft_100_roll_std_1',
'fft_100_roll_std_20',
'fft_100_roll_std_70',
'fft_100_roll_std_75',
'fft_10_roll_std_75',
'fft_mean_change_rate',
'fft_min',
'fft_min_roll_mean_100',
'fft_min_roll_std_100',
'fft_skew',
'fft_skew_first_50000',
'fft_spkt_welch_density_100',
'fft_spkt_welch_density_5',
'fft_spkt_welch_density_50',
'fft_time_rev_asym_stat_10',
'fft_time_rev_asym_stat_100',
'iqr',
'kstat_3',
'mad',
'max_first_5000',
'max_last_10000',
'max_roll_mean_1000',
'max_to_min',
'mean_change_abs',
'med',
'min_last_10000',
'min_roll_std_100',
'num_crossings',
'num_peaks_10',
'num_peaks_5',
'q01_roll_mean_100',
'q01_roll_std_10',
'q01_roll_std_1000',
'q05_roll_mean_100',
'q05_roll_std_1000',
'q95',
'q95_roll_mean_10',
'q95_roll_mean_100',
'q95_roll_std_1000',
'q99_roll_mean_1000',
'skew',
'std_0_to_10',
'std_first_5000',
'std_neg_10_to_0',
'std_neg_2_to_2',
'std_neg_5_to_5',
'std_roll_mean_1000']<create_dataframe>
X_train,X_test,y_train,y_test=train_test_split(train,tweet['target'].values,test_size=0.15)
 print('Shape of train',X_train.shape)
 print("Shape of Validation ",X_test.shape )
X_train_select = X_train[features_select].copy()
X_test_select = X_test[features_select].copy()<compute_test_metric>
history=model.fit(X_train,y_train,batch_size=4,epochs=15,validation_data=(X_test,y_test),verbose=2 )
ttf_pred_orig, ttf_test_orig, ttf_pred_oof, model1= get_prediction(X_train_select, y_train,
X_test = X_test_select)
 print("Training MAE for time_to_failure is {:.7f}"\ 
.format(np.abs(y_train['time_to_failure'] - ttf_pred_orig ).mean()))
print("OOF MAE for time_to_failure is {:.7f}"\ 
.format(np.abs(y_train['time_to_failure'] - ttf_pred_oof ).mean()))<predict_on_test>
sample_sub=pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv' )
<train_model><EOS> 
y_pre=model.predict(test)
 y_pre=np.round(y_pre ).astype(int ).reshape(3263)
 sub=pd.DataFrame({'id':sample_sub['id'].values.tolist() ,'target':y_pre})
 sub.to_csv('submission.csv',index=False)