Kaggle competition: Natural Language Processing with Disaster Tweets
SEED = 1111
np.random.seed(SEED )<prepare_x_and_y>
target = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')['target']
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 ssub = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] != 0]
train.fillna(train.mean() ,inplace=True)
 train['action'] =(( train['resp'].values)> 0 ).astype(int)
 val = train.loc[train["date"]>=450].copy()
train = train.loc[train["date"]<450]
features = [c for c in train.columns if "feature" in c]
f_mean = np.mean(train[features[1:]].values,axis=0)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X_train = train.loc[:, train.columns.str.contains('feature')]
y_train = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
X_val = val.loc[:, val.columns.str.contains('feature')]
y_val = np.stack([(val[c] > 0 ).astype('int')for c in resp_cols] ).T<choose_model_class>
import pandas_profiling as pp
def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
inp = tf.keras.layers.Input(shape=(num_columns,))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(dropout_rates[0] )(x)
 for i in range(len(hidden_units)) :
x = tf.keras.layers.Dense(hidden_units[i] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(dropout_rates[i + 1] )(x)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer=tfa.optimizers.RectifiedAdam(learning_rate=learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model<train_model>
train['hashtags']=train['text'].apply(lambda x:re.findall('
tf.keras.backend.clear_session()
tf.random.set_seed(SEED)
 clf = create_mlp(
 len(features), 5, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=epochs, batch_size=batch_size, verbose=2)
 clf.save(f'model.h5')
<compute_train_metric>
labels=['Negative','Positive']
no_clusters=2
for c in range(2):
print('Target:-',labels[c])
 hts=list(train[train['target']==c]['hashtags'])
 hashes=[]
for ht in hts:
for h in ht:
hashes.append(h.strip())
 string_hash=' '.join(hashes)
 hash_values=pd.Series(hashes ).value_counts()
hval=hash_values.reset_index()
d = {}
for a, x in hval.values:
d[a] = x
wordcloud = WordCloud(max_font_size=40)
 wordcloud.generate_from_frequencies(frequencies=d)
 plt.figure(figsize=(70,70))
plt.imshow(wordcloud, interpolation="bilinear")
 plt.axis("off")
 plt.show()
preds = clf(X_train.values, training = False ).numpy()
preds = np.median(preds, axis=1)
 u_score = utility_score_numba(train.date.values, train.weight.values, train.resp.values,(preds>th ).astype(int))
u_score<compute_test_metric>
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/3")
 X_train_embeddings = embed(train.text.values)
 X_test_embeddings = embed(test.text.values )
preds = clf(X_val.values, training = False ).numpy()
preds = np.median(preds, axis=1)
 u_score = utility_score_numba(val.date.values, val.weight.values, val.resp.values,(preds>th ).astype(int))
u_score<set_options>
X_embedded = TSNE(n_components=2,perplexity=50 ).fit_transform(X_train_embeddings['outputs'])
 xy_df=pd.DataFrame(X_embedded)
 xy_df['tweets']=train.text.values
xy_df['Target']=target
xy_df.columns=['x', 'y', 'tweets','Target']
del train, val
gc.collect()<categorify>
output_notebook()
models = [clf]
f = np.median
env = janestreet.make_env()
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_mean
pred = np.mean([model(x_tt, training = False ).numpy() for model in models],axis=0)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<set_options>
vectorizer = TfidfVectorizer(max_features=30,stop_words='english')
 all_tweets=list(train['text'])+list(test['text'])
 X = vectorizer.fit_transform(all_tweets)
 tweet_array=X.toarray()
tf_train=tweet_array[0:len(train)]
tf_test=tweet_array[len(train):len(tweet_array)]
train_df=np.concatenate([X_train_embeddings['outputs'],tf_train],axis=1)
 test_df=np.concatenate([X_test_embeddings['outputs'],tf_test],axis=1 )
tf.random.set_seed(42)
<categorify>
text_clf = lgb.LGBMClassifier(n_estimators=3000, learning_rate=0.05)
 text_clf.fit(train_df, target )
<load_from_csv><EOS> 
pred=text_clf.predict(test_df)
 ssub["target"] = pred
ssub.to_csv("submission.csv",index=False )