Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<drop_column>
nlp = spacy.load("en_core_web_lg" )
X_train.reset_index(drop=True, inplace=True)
 X_test.reset_index(drop=True, inplace=True )<concatenate>
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn import metrics, svm
n_comp = 600
data = pd.concat([X_train[GENES], X_test[GENES]])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:X_train.shape[0]]
test2 = data2[-X_test.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 X_train = pd.concat(( X_train, train2), axis=1)
 X_test = pd.concat(( X_test, test2), axis=1 )<create_dataframe>
full_train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 full_submission_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 full_train_data.head()
n_comp = 50
data = pd.concat([X_train[CELLS], X_test[CELLS]])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:X_train.shape[0]]
test2 = data2[-X_test.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 X_train = pd.concat(( X_train, train2), axis=1)
 X_test = pd.concat(( X_test, test2), axis=1 )<normalization>
train_data_shape = full_train_data.shape[0]
var_thresh = VarianceThreshold(0.8)
 data = X_train.append(X_test)
 data_transformed = var_thresh.fit_transform(data)
 X_train = data_transformed[ : X_train.shape[0]]
X_test = data_transformed[-X_test.shape[0] : ]<compute_test_metric>
df = pd.concat([full_train_data, full_submission_data])
 df.shape
def logloss(y_true, y_pred):
y_pred = tf.clip_by_value(y_pred,p_min,p_max)
 return -backend.mean(y_true*backend.log(y_pred)+(1-y_true)*backend.log(1-y_pred))<define_variables>
def clean_text(text):
url = re.compile(r'https?://\S+|www\.\S+')
 text = url.sub(r'', text)
 html = re.compile(r'<.*?>')
 text = html.sub(r'', 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)
 text = emoji_pattern.sub(r'', text)
 table = str.maketrans('', '', string.punctuation)
 text = text.translate(table)
 return text
df['text'] = df['text'].apply(lambda x : clean_text(x))
n_train = X_train.shape[0]
n_test = X_test.shape[0]
n_features = X_train.shape[1]
n_labels = y_train.shape[1]
epsilon = 1E-3
p_min = epsilon
p_max = 1-epsilon<choose_model_class>
train_data = df.copy().iloc[:train_data_shape]
submission_data = df.copy().iloc[train_data_shape:]
sgd = tf.keras.optimizers.SGD()
adamw = tfa.optimizers.AdamW(weight_decay = 0.0001)
 adam = tf.keras.optimizers.Adam()
radam = tfa.optimizers.RectifiedAdam()
lookahead_radam = tfa.optimizers.Lookahead(radam)
 lookahead_adamw = tfa.optimizers.Lookahead(adamw)
 def create_model(n_features, opt):
model = Sequential([
layers.Input(n_features),
layers.BatchNormalization() ,
layers.Dropout(0.466),
tfa.layers.WeightNormalization(layers.Dense(1024)) ,
layers.LeakyReLU() ,
layers.Dropout(0.466),
layers.BatchNormalization() ,
tfa.layers.WeightNormalization(layers.Dense(2048)) ,
layers.LeakyReLU() ,
layers.Dropout(0.466),
layers.BatchNormalization() ,
tfa.layers.WeightNormalization(layers.Dense(2048)) ,
layers.LeakyReLU() ,
layers.Dropout(0.466),
layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(n_labels, activation="sigmoid"))
])
 model.compile(optimizer= opt,
loss=losses.BinaryCrossentropy(label_smoothing=1E-6), metrics=logloss)
 return model 
<split>
X = train_data.copy().drop(['target'], axis=1)
 y = train_data.copy() ['target']
X_sub = submission_data.copy().drop(['target'], axis=1 )
n_folds = 5
seed = 42
test_pred = np.zeros(( n_test, n_labels))
oof_pred = np.zeros(( n_train, n_labels))
mskf = MultilabelStratifiedKFold(n_splits = n_folds, random_state = seed, shuffle = True)
 for n,(tr, te)in enumerate(mskf.split(X_train, y_train)) :
print(f'Starting fold: {n}')
 model = create_model(n_features, 'adam')
 reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=5, verbose=1, mode='min',min_lr=1E-5)
 early_stopping = callbacks.EarlyStopping(monitor='val_logloss', min_delta=1E-4, patience=10, mode='min',restore_best_weights=True)
 model.fit(
 X_train[tr],
y_train[tr],
validation_data=(X_train[te], y_train[te]),
epochs=80,
batch_size=128,
callbacks = [reduce_lr,early_stopping] 
)
 test_pred += model.predict(X_test)/n_folds
oof_pred[te,:] += model.predict(X_train[te] )<compute_test_metric>
class SpacyVectorTransformer(BaseEstimator, TransformerMixin):
def __init__(self, nlp):
self.nlp = nlp
self.dim = 300
def fit(self, X, y):
return self
def transform(self, X):
return np.array([self.nlp(text ).vector for text in X] )
print(f'OOF log loss: {log_loss(np.ravel(y_train), np.ravel(np.clip(oof_pred, p_min, p_max)))}' )<compute_test_metric>
column_preprocessor = ColumnTransformer(
 [ 
('text_glove', SpacyVectorTransformer(nlp), 'text'),
],
remainder='drop',
n_jobs=1
)
print(f'OOF log loss: {log_loss(np.ravel(y_train), np.ravel(oof_pred)) }' )<prepare_output>
pipeline = Pipeline([ 
('column_preprocessor', column_preprocessor), 
('svm', svm.SVC(kernel='rbf', C=1.2, gamma=0.2))
] )
predictions = np.clip(test_pred.copy() , p_min,p_max)
 for pos in add_index:
predictions = np.insert(predictions, pos, values=np.zeros(206), axis=0 )<save_to_csv>
print("start pipeline fit")
 pipeline.fit(X, y )
ss = pd.DataFrame(predictions, columns=train_target.columns[1:])
 ss.insert(0,'sig_id', test_set['sig_id'].values)
 ss.to_csv('submission.csv',index = False)
 ss.describe()<load_from_csv>
predictions = pipeline.predict(X_sub)
 predictions = [int(i)for i in predictions]
train_features = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 cp_type_train = train_features['cp_type']
non_ctl_idx = train_features.loc[train_features['cp_type']!='ctl_vehicle'].index.to_list()
train_features = train_features.drop(['sig_id','cp_type','cp_dose','cp_time'],axis=1)
 train_targets_scored = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 train_targets_scored = train_targets_scored.drop('sig_id',axis=1)
 labels_train = train_targets_scored.values
test_features = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_features = test_features.drop(['sig_id','cp_dose','cp_time'],axis=1 )<filter>
results = pd.Series(predictions, name="target")
 tweet_ids = full_submission_data['id']
submission = pd.concat([tweet_ids, results], axis=1)
 print(submission.head())
 submission.to_csv("submission.csv",index=False )