Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<create_dataframe>
start_time = time.time()
cols = dataset.columns
r,c = dataset.shape
data = pandas.DataFrame(index=numpy.arange(0, r),columns=['Wilderness_Area','Soil_Type','Cover_Type'])
 for i in range(0,r):
w=0;
s=0;
for j in range(10,14):
if(dataset.iloc[i,j] == 1):
w=j-9
break
for k in range(14,54):
if(dataset.iloc[i,k] == 1):
s=k-13
break
data.iloc[i]=[w,s,dataset.iloc[i,c-1]]
sns.countplot(x="Wilderness_Area", hue="Cover_Type", data=data)
 plt.show()
plt.rc("figure", figsize=(25, 10))
sns.countplot(x="Soil_Type", hue="Cover_Type", data=data)
 plt.show() 
<drop_column>
%matplotlib inline
rem = []
for c in dataset.columns:
if dataset[c].std() == 0:
rem.append(c)
 dataset.drop(rem,axis=1,inplace=True)
 print(rem)
<split>
!pip install tensorflow-text==2.0.0 --user
r, c = dataset.shape
cols = dataset.columns
i_cols = []
for i in range(0,c-1):
i_cols.append(i)
 ranks = []
array = dataset.values
X = array[:,0:(c-1)]
Y = array[:,(c-1)]
val_size = 0.1
seed = 0
X_train, X_val, Y_train, Y_val = cross_validation.train_test_split(X, Y, test_size=val_size, random_state=seed)
 X_all = []
X_all_add =[]
rem = []
i_rem = []
comb = []
comb.append("All+1.0")
 X_all.append(['Orig','All', X_train,X_val,1.0,cols[:c-1],rem,ranks,i_cols,i_rem])
 size=10
X_temp = StandardScaler().fit_transform(X_train[:,0:size])
 X_val_temp = StandardScaler().fit_transform(X_val[:,0:size])
 X_con = numpy.concatenate(( X_temp,X_train[:,size:]),axis=1)
 X_val_con = numpy.concatenate(( X_val_temp,X_val[:,size:]),axis=1)
 X_all.append(['StdSca','All', X_con,X_val_con,1.0,cols,rem,ranks,i_cols,i_rem])
 X_temp = MinMaxScaler().fit_transform(X_train[:,0:size])
 X_val_temp = MinMaxScaler().fit_transform(X_val[:,0:size])
 X_con = numpy.concatenate(( X_temp,X_train[:,size:]),axis=1)
 X_val_con = numpy.concatenate(( X_val_temp,X_val[:,size:]),axis=1)
 X_all.append(['MinMax', 'All', X_con,X_val_con,1.0,cols,rem,ranks,i_cols,i_rem])
 X_temp = Normalizer().fit_transform(X_train[:,0:size])
 X_val_temp = Normalizer().fit_transform(X_val[:,0:size])
 X_con = numpy.concatenate(( X_temp,X_train[:,size:]),axis=1)
 X_val_con = numpy.concatenate(( X_val_temp,X_val[:,size:]),axis=1)
 X_all.append(['Norm', 'All', X_con,X_val_con,1.0,cols,rem,ranks,i_cols,i_rem])
 trans_list = []
for trans,name,X,X_val,v,cols_list,rem_list,rank_list,i_cols_list,i_rem_list in X_all:
trans_list.append(trans )<define_variables>
import tensorflow as tf
import tensorflow_hub as hub
ratio_list = [0.75,0.50,0.25]<choose_model_class>
import tensorflow_text as text
feat = []
feat_list =[]
model = LogisticRegression(random_state=seed,n_jobs=-1)
 n = 'RFE'
feat_list.append(n)
 for val in ratio_list:
comb.append("%s+%s" %(n,val))
feat.append([n,val,RFE(model,val*(c-1)) ])
 for trans,s, X, X_val, d, cols, rem, ra, i_cols, i_rem in X_all:
for name,v, model in feat:
model.fit(X,Y_train)
 joined = []
for i, pred in enumerate(list(model.ranking_)) :
joined.append([i,cols[i],pred])
 joined_sorted = sorted(joined, key=lambda x: x[2])
 rem_start = int(( v*(c-1)))
 cols_list = []
i_cols_list = []
rank_list =[]
rem_list = []
i_rem_list = []
for i, col, j in joined_sorted:
rank_list.append([i,j-1])
 if(( j-1)< rem_start):
cols_list.append(col)
 i_cols_list.append(i)
 else:
rem_list.append(col)
 i_rem_list.append(i)
 X_all_add.append([trans,name,X,X_val,v,cols_list,rem_list,[x[1] for x in sorted(rank_list,key=lambda x:x[0])],i_cols_list,i_rem_list])
 plt.rc("figure", figsize=(25, 10))
for f_name in feat_list:
leg=[]
fig, ax = plt.subplots()
for trans,name,X,X_val,v,cols_list,rem_list,rank_list,i_cols_list,i_rem_list in X_all_add:
if(name==f_name):
plt.plot(rank_list)
 leg.append(trans+"+"+name+"+%s"% v)
 ax.set_xticks(range(c-1))
ax.set_xticklabels(cols[:c-1],rotation='vertical')
 plt.legend(leg,loc='best')
 plt.show()<train_model>
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )
feat = []
feat_list =[]
n = 'SelK'
feat_list.append(n)
 for val in ratio_list:
comb.append("%s+%s" %(n,val))
feat.append([n,val,SelectPercentile(score_func=f_classif,percentile=val*100)])
 for trans,s, X, X_val, d, cols, rem, ra, i_cols, i_rem in X_all:
for name,v, model in feat:
model.fit(X,Y_train)
 joined = []
for i, pred in enumerate(list(model.scores_)) :
joined.append([i,cols[i],pred])
 joined_sorted = sorted(joined, key=lambda x: -x[2])
 rem_start = int(( v*(c-1)))
 cols_list = []
i_cols_list = []
rank_list =[]
rem_list = []
i_rem_list = []
for j,(i, col, x)in enumerate(list(joined_sorted)) :
rank_list.append([i,j])
 if(j < rem_start):
cols_list.append(col)
 i_cols_list.append(i)
 else:
rem_list.append(col)
 i_rem_list.append(i)
 X_all_add.append([trans,name,X,X_val,v,cols_list,rem_list,[x[1] for x in sorted(rank_list,key=lambda x:x[0])],i_cols_list,i_rem_list])
 plt.rc("figure", figsize=(25, 10))
for f_name in feat_list:
leg=[]
fig, ax = plt.subplots()
for trans,name,X,X_val,v,cols_list,rem_list,rank_list,i_cols_list,i_rem_list in X_all_add:
if(name==f_name):
plt.plot(rank_list)
 leg.append(trans+"+"+name+"+%s"% v)
 ax.set_xticks(range(c-1))
ax.set_xticklabels(cols[:c-1],rotation='vertical')
 plt.legend(leg,loc='best')
 plt.show()<create_dataframe>
def clean(text):
text = re.sub(r"http\S+", " ", text)
 text = re.sub(r"RT ", " ", text)
 text = re.sub(r"[^a-zA-Z
text = re.sub(r"[0-9]", " ", text)
 text = re.sub(r"\s+", " ", text)
 text = text.strip()
return text
rank_df = pandas.DataFrame(data=[x[7] for x in X_all_add],columns=cols[:c-1])
 _ = rank_df.boxplot(rot=90)
<save_to_csv>
train.text = train.text.apply(clean)
 test.text = test.text.apply(clean )
rank_df = pandas.DataFrame(data=[x[7] for x in X_all_add],columns=cols[:c-1])
 med = rank_df.median()
print(med)
 with open("median.csv", "w")as subfile:
subfile.write("Column,Median
")
 subfile.write(med.to_string() )<import_modules>
use = hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3" )
import numpy as np
import pandas as pd
import os<load_from_csv>
X_train = []
for r in tqdm(train.text.values):
emb = use(r)
 review_emb = tf.reshape(emb, [-1] ).numpy()
X_train.append(review_emb)
 X_train = np.array(X_train)
 y_train = train.target.values
X_test = []
for r in tqdm(test.text.values):
emb = use(r)
 review_emb = tf.reshape(emb, [-1] ).numpy()
X_test.append(review_emb)
 X_test = np.array(X_test )
app_train = pd.read_csv('.. /input/home-credit-simple-featuers/simple_features_train.csv')
 app_test = pd.read_csv('.. /input/home-credit-simple-featuers/simple_features_test.csv' )<load_from_csv>
train_arrays, test_arrays, train_labels, test_labels = train_test_split(X_train,
y_train,
random_state =42,
test_size=0.05 )
sk_id = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv' )<drop_column>
import xgboost as xgb
from xgboost import XGBClassifier
train = app_train.drop(columns = ['TARGET'])
 train_labels = app_train['TARGET']<split>
def xgboost_param_selection(X, y, nfolds):
depth_m=[2,3,5,7,9]
base_learners=[2,5,50,70,100]
parameters=dict(n_estimators=base_learners , max_depth=depth_m)
 clf=RandomizedSearchCV(XGBClassifier(n_jobs=-1, class_weight='balanced'),parameters, scoring='roc_auc', refit=True, cv=3)
 clf.fit(X, y)
 clf.best_params_
return clf
X_train, X_test, y_train, y_test = train_test_split(
 train, train_labels, test_size=0.2 )<choose_model_class>
def svc_param_selection(X, y, nfolds):
Cs = [1.07]
gammas = [2.075]
param_grid = {'C': Cs, 'gamma' : gammas}
grid_search = GridSearchCV(SVC(kernel='rbf'), param_grid, cv=nfolds, n_jobs=8)
 grid_search.fit(X, y)
 grid_search.best_params_
return grid_search
clf = LGBMClassifier(boosting_type = 'goss', n_estimators = 10000, learning_rate= 0.005134, num_leaves= 54, max_depth= 10, subsample_for_bin= 240000, reg_alpha= 0.436193, reg_lambda= 0.479169, colsample_bytree=0.508716, min_split_gain= 0.024766, subsample= 1, is_unbalance= False, silent=-1, verbose=-1)
 clf.fit(X_train, y_train, eval_set=[(X_train, y_train),(X_test, y_test)],
eval_metric= 'auc', verbose= 100, early_stopping_rounds= 200)
<predict_on_test>
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(input_dim,)))
 model.add(Dense(128, activation='relu'))
model.add(Dense(output_dim, input_dim=input_dim, activation='softmax'))
model.summary()
<save_to_csv>
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 nb_epoch = 10
history = model.fit(train_arrays,train_labels, batch_size=batch_size, epochs=nb_epoch, verbose=1 )
y_pred = clf.predict_proba(app_test, num_iteration=clf.best_iteration_)[:, 1]
submit = sk_id[['SK_ID_CURR']]
submit['TARGET'] = y_pred
submit.to_csv('sub1.csv', index = False)
<load_from_csv>
pred = model.predict(test_arrays )
df = dtable.fread('/kaggle/input/jane-street-market-prediction/train.csv' ).to_pandas()
df = df.query('date > 85' ).reset_index(drop = True)
 df = df[df.weight > 0]
df.reset_index(inplace=True, drop=True)
 df = df.astype({c: np.float32 for c in df.select_dtypes(include='float64' ).columns})
 df_labels = df[['date', 'weight', 'resp_1', 'resp_2', 'resp_3', 'resp_4', 'resp']]
features = [c for c in df.columns if 'feature' in c]
df = df.drop(df_labels.columns, axis=1)
 df.fillna(df.mean() , inplace=True)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp_4', 'resp']
X = df[features]
y = np.stack([(df_labels[c] > 0 ).astype('int')for c in resp_cols] ).T
f_mean = np.mean(df[features[1:]].values, axis=0)
 device = 'cuda' if t.cuda.is_available() else 'cpu'<set_options>
test_pred = model.predict(X_test)
 submission['target'] = test_pred.round().astype(int)
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
cha_1 = 64
cha_2 = 128
cha_3 = 128
cha_1_reshape = int(hidden_size / cha_1)
 cha_po_1 = int(hidden_size / cha_1 / 2)
 cha_po_2 = int(hidden_size / cha_1 / 2 / 2)* cha_3
self.cha_1_reshape = cha_1_reshape
self.cha_1 = cha_1
self.cha_2 = cha_2
self.cha_3 = cha_3
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dropout1 = nn.Dropout(0.1)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm_c1 = nn.BatchNorm1d(cha_1)
 self.dropout_c1 = nn.Dropout(0.1)
 self.conv1 = nn.utils.weight_norm(nn.Conv1d(cha_1, cha_2, kernel_size=5, stride=1, padding=2, bias=False), dim=None)
 self.ave_po_c1 = nn.AdaptiveAvgPool1d(output_size=cha_po_1)
 self.batch_norm_c2 = nn.BatchNorm1d(cha_2)
 self.dropout_c2 = nn.Dropout(0.1)
 self.conv2 = nn.utils.weight_norm(nn.Conv1d(cha_2, cha_2, kernel_size=3, stride=1, padding=1, bias=True), dim=None)
 self.batch_norm_c2_1 = nn.BatchNorm1d(cha_2)
 self.dropout_c2_1 = nn.Dropout(0.3)
 self.conv2_1 = nn.utils.weight_norm(nn.Conv1d(cha_2, cha_2, kernel_size=3, stride=1, padding=1, bias=True), dim=None)
 self.batch_norm_c2_2 = nn.BatchNorm1d(cha_2)
 self.dropout_c2_2 = nn.Dropout(0.2)
 self.conv2_2 = nn.utils.weight_norm(nn.Conv1d(cha_2, cha_3, kernel_size=5, stride=1, padding=2, bias=True), dim=None)
 self.max_po_c2 = nn.MaxPool1d(kernel_size=4, stride=2, padding=1)
 self.flt = nn.Flatten()
self.batch_norm3 = nn.BatchNorm1d(cha_po_2)
 self.dropout3 = nn.Dropout(0.2)
 self.dense3 = nn.utils.weight_norm(nn.Linear(cha_po_2, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = self.dropout1(x)
 x = F.celu(self.dense1(x), alpha=0.06)
 x = x.reshape(x.shape[0], self.cha_1, self.cha_1_reshape)
 x = self.batch_norm_c1(x)
 x = self.dropout_c1(x)
 x = F.relu(self.conv1(x))
x = self.ave_po_c1(x)
 x = self.batch_norm_c2(x)
 x = self.dropout_c2(x)
 x = F.relu(self.conv2(x))
x_s = x
x = self.batch_norm_c2_1(x)
 x = self.dropout_c2_1(x)
 x = F.relu(self.conv2_1(x))
x = self.batch_norm_c2_2(x)
 x = self.dropout_c2_2(x)
 x = F.relu(self.conv2_2(x))
x = x * x_s
x = self.max_po_c2(x)
 x = self.flt(x)
 x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x<train_model>
count = 2
prob_disaster = 0.9
keyword_list_disaster = list(ag[(ag['Count']>count)&(ag['Disaster Probability']>=prob_disaster)].index)
 keyword_list_disaster
model_nn = Model(num_features=130, num_targets=5, hidden_size=512)
 model_nn = model_nn.to(device )<load_pretrained>
ids_disaster = test['id'][test.keyword.isin(keyword_list_disaster)].values
submission['target'][submission['id'].isin(ids_disaster)] = 1
<categorify><EOS> 
submission.to_csv("submission.csv", index=False)
 submission.head(10 )