Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<define_variables>
nlp = spacy.load('en_core_web_sm')
 pd.options.display.max_colwidth = 400
AUTO = tf.data.experimental.AUTOTUNE
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 2
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
MAX_LEN = 192
MODEL = 'jplu/tf-xlm-roberta-large'<load_pretrained>
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 sample_submission = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 print('Train shape: {}'.format(train.shape))
print('Test shape: {}'.format(test.shape))
print('Sample submission shape: {}'.format(sample_submission.shape))
train.head()
tokenizer = AutoTokenizer.from_pretrained(MODEL )<load_from_csv>
word2vec_pipe = Pipeline([('estimator', LogisticRegression())])
 print('F1 score: {:.3f}'.format(np.mean(cross_val_score(word2vec_pipe, X_train, y_train, scoring = 'f1'))))
word2vec_pipe.fit(X_train, y_train)
 pred = word2vec_pipe.predict(X_test)
 sample_submission.target = pred
sample_submission.to_csv('word2vec_baseline.csv', index = False )
train1 = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-toxic-comment-train.csv")
 train2 = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-unintended-bias-train.csv")
 train2.toxic = train2.toxic.round().astype(int)
 valid = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/validation.csv')
 test = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/test.csv')
 sub = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv')
 train = pd.concat([
train1[['comment_text', 'toxic']],
train2[['comment_text', 'toxic']].query('toxic==1'),
train2[['comment_text', 'toxic']].query('toxic==0' ).sample(n=100000, random_state=0)
 ])
 x_train = regular_encode(train.comment_text.values, tokenizer, maxlen=MAX_LEN)
 x_valid = regular_encode(valid.comment_text.values, tokenizer, maxlen=MAX_LEN)
 x_test = regular_encode(test.content.values, tokenizer, maxlen=MAX_LEN)
 y_train = train.toxic.values
y_valid = valid.toxic.values<define_variables>
param_grid = [{'estimator' : [LogisticRegression() ],
'estimator__C' : np.logspace(-3, 3, 7)},
{'estimator' : [SVC() ],
'estimator__C' : np.logspace(-1, 1, 3),
'estimator__gamma' : np.logspace(-2, 2, 5)/ X_train.shape[0]}]
word2vec_grid_search = GridSearchCV(
 estimator = word2vec_pipe,
param_grid = param_grid,
scoring = 'f1',
n_jobs = -1,
refit = True,
verbose = 1,
return_train_score = True 
)
 word2vec_grid_search.fit(X_train, y_train)
 cols = ['param_estimator',
'param_estimator__C',
'param_estimator__gamma',
'mean_test_score',
'mean_train_score']
pd.options.display.max_colwidth = 50
word2vec_grid_search_results = pd.DataFrame(word2vec_grid_search.cv_results_ ).sort_values(by = 'mean_test_score',
ascending = False)
 word2vec_grid_search_results[cols].head(10 )
<load_pretrained><EOS> 
pred = word2vec_grid_search.predict(X_test)
 sample_submission.target = pred
sample_submission.to_csv('word2vec_tuned.csv', index = False )