<jupyter_start><jupyter_text>Quora preprocessing **(~1:30h gpu run time)**<jupyter_code>import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split
from imblearn.over_sampling import RandomOverSampler<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>df_raw = pd.read_csv("../data/raw/train.csv", low_memory=False)
df_raw.head()
df_raw.shape
sampled_df = df_raw[df_raw.target == 0].sample(n=480_000, random_state=42)

df = pd.concat(
    [sampled_df, df_raw[df_raw.target == 1]]
)
df.head()
# Target samples
df.shape[0] - 200_000<jupyter_output><empty_output><jupyter_text>Preprocessing<jupyter_code># Train/Test
X, y = df.drop('target', axis=1), df.target

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.10, random_state=42
)

train_df = X_train
train_df['target'] = y_train

test_df = X_test
test_df['target'] = y_test<jupyter_output><empty_output><jupyter_text>Random Oversampling<jupyter_code># Oversampling
ros = RandomOverSampler(random_state=42)
X_resampled, y_resampled = ros.fit_resample(X_train, y_train)

resampled_df = X_resampled
resampled_df['target'] = y_resampled<jupyter_output><empty_output><jupyter_text>nlpaugUsing:  * KeyboardAug  * ContextualWordEmbsAug  * SynonymAug  * BackTranslationAug  * SpellingAug80_000 new examples for each augment<jupyter_code>import nlpaug.augmenter.char as nac
import nlpaug.augmenter.word as naw
import nlpaug.augmenter.sentence as nas

df_1 = train_df[train_df.target==1].copy()

texts = list(df_1.question_text)
texts[:5]
key_board_aug = nac.KeyboardAug(aug_char_max=2, aug_word_max=2)
key_board_texts = key_board_aug.augment(texts)
key_board_texts[:5]
synonym_aug = naw.SynonymAug(aug_max=2)
synonym_texts = synonym_aug.augment(texts)
synonym_texts[:5]
spelling_aug = naw.SpellingAug(aug_max=1)
spelling_texts = spelling_aug.augment(texts)
spelling_texts[:5]
# Load contextual words
contextual_words_aug = naw.ContextualWordEmbsAug(model_path='distilbert-base-uncased', aug_max=4, device='cuda')
contextual_words_aug
contextual_words = []

for i in range(0, len(texts), 64):
    if i%1024==0:
        print(f"{i}/{len(texts)}")
    contextual_words += contextual_words_aug.augment(texts[i: i + 64])

contextual_words[:5]
new_texts = key_board_texts + synonym_texts + contextual_words + spelling_texts
new_texts[:5]
df_nlpaug = pd.DataFrame({'question_text': new_texts, 'target': np.ones(len(new_texts))})
df_nlpaug.head()
train_nlpaug = pd.concat([train_df, df_nlpaug])
train_nlpaug.head()
train_nlpaug.shape
train_nlpaug.target.value_counts()
import pandas as pd
t = pd.read_csv("../data/nlpaug/train.csv")
t.shape
x1 = t[t.target==1]
x2 = t[t.target==0]
x2 = x2.sample(n=364145)

df = pd.concat([x1, x2])
df.shape
df.target.value_counts()<jupyter_output><empty_output><jupyter_text>Save<jupyter_code># train datasets
resampled_df.to_csv("../data/ros/train.csv")
train_nlpaug.to_csv("../data/nlpaug/train.csv")

# nlpaug
test_df.to_csv("../data/processed/test.csv")<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup