Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<train_model>
!pip install simpletransformers
vectorizer = TfidfVectorizer(max_features=5000,
ngram_range=(1, 3),
stop_words = stopwords.words('english'),
preprocessor = prep_remove_html,
strip_accents='unicode')
 vectorizer.fit(list(xtrain)+ list(xvalid))<normalization>
import os, re, string
import random
import numpy as np
import pandas as pd
import sklearn
import torch
from simpletransformers.classification import ClassificationModel
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
xtrain_features = vectorizer.transform(xtrain)
 xvalid_features = vectorizer.transform(xvalid)
<train_model>
seed = 1337
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
clf_lr = LogisticRegression()
clf_lr.fit(xtrain_features, ytrain)
 clf_lr.score(xvalid_features, yvalid)
<train_model>
train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 train_data = train_data[['text', 'target']]
clf_nb = BernoulliNB()
clf_nb.fit(xtrain_features, ytrain)
 clf_nb.score(xvalid_features, yvalid)
<train_model>
bert_uncased = ClassificationModel('bert', 'bert-large-uncased')
 bert_uncased.args
clf_rf = RandomForestClassifier(n_estimators = 100)
 clf_rf.fit(xtrain_features, ytrain)
 clf_rf.score(xvalid_features, yvalid )<save_to_csv>
custom_args = {'fp16': False,
'train_batch_size': 4,
'gradient_accumulation_steps': 2,
'do_lower_case': True,
'learning_rate': 1e-05,
'overwrite_output_dir': True,
'num_train_epochs': 2}
test = pd.read_csv("/kaggle/input/word2vec-nlp-tutorial/testData.tsv", header=0, delimiter="\t", quoting=3)
 test_features = vectorizer.transform(test.review.values)
 predictions = clf_lr.predict(test_features)
 output = pd.DataFrame(data={"id":test["id"], "sentiment":predictions})
 output.to_csv("tfidf-logreg.csv", index=False, quoting=3 )<load_from_csv>
n=5
kf = KFold(n_splits=n, random_state=seed, shuffle=True)
 results = []
for train_index, val_index in kf.split(train_data):
train_df = train_data.iloc[train_index]
val_df = train_data.iloc[val_index]
model = ClassificationModel('bert', 'bert-base-uncased', args=custom_args)
 model.train_model(train_df)
 result, model_outputs, wrong_predictions = model.eval_model(val_df, acc=sklearn.metrics.accuracy_score)
 print(result['acc'])
 results.append(result['acc'] )
train = pd.read_csv('.. /input/labeledTrainData.tsv', delimiter="\t")
 test = pd.read_csv('.. /input/testData.tsv', delimiter="\t")
 print(train.head())
 print(train.shape)
 print(test.shape )<normalization>
for i, result in enumerate(results, 1):
print(f"Fold-{i}: {result}")
 print(f"{n}-fold CV accuracy result: Mean: {np.mean(results)} Standard deviation:{np.std(results)}" )
tfidf = TfidfVectorizer(
 ngram_range=(1, 3),
use_idf=1,
smooth_idf=1,
sublinear_tf = 1,
stop_words ='english')
 train_data_count = tfidf.fit_transform(train_data)
 test_data_count = tfidf.transform(test_data)
 print(train_data_count.shape)
 print(test_data_count.shape )<choose_model_class>
model = ClassificationModel('bert', 'bert-base-uncased', args=custom_args)
 model.train_model(train_data )
<save_to_csv><EOS> 
test_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 predictions, raw_outputs = model.predict(test_data['text'])
 sample_submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 sample_submission["target"] = predictions
sample_submission.to_csv("submission.csv", index=False )