<jupyter_start><jupyter_text>GPU Extreme gradient boosting trained on timestamp text data-set1. Same emotion dataset from [NLP-dataset](https://github.com/huseinzol05/NLP-Dataset)2. Same splitting 80% training, 20% testing, may vary depends on randomness3. Same regex substitution '[^\"\'A-Za-z0-9 ]+' ExampleBased on sorted dictionary positiontext: 'module into which all the refactored classes', matrix: [167, 143, 12, 3, 4, 90]<jupyter_code>import numpy as np
import sklearn.datasets
import re
import time
import xgboost as xgb
import pickle
from sklearn.cross_validation import train_test_split
import json
def clearstring(string):
    string = re.sub('[^\"\'A-Za-z0-9 ]+', '', string)
    string = string.split(' ')
    string = filter(None, string)
    string = [y.strip() for y in string]
    string = ' '.join(string)
    return string

# because of sklean.datasets read a document as a single element
# so we want to split based on new line
def separate_dataset(trainset):
    datastring = []
    datatarget = []
    for i in range(len(trainset.data)):
        data_ = trainset.data[i].split('\n')
        # python3, if python2, just remove list()
        data_ = list(filter(None, data_))
        for n in range(len(data_)):
            data_[n] = clearstring(data_[n])
        datastring += data_
        for n in range(len(data_)):
            datatarget.append(trainset.target[i])
    return datastring, datatarget
trainset_data = sklearn.datasets.load_files(container_path = 'data', encoding = 'UTF-8')
trainset_data.data, trainset_data.target = separate_dataset(trainset_data)
with open('dictionary_emotion.p', 'rb') as fopen:
    dict_emotion = pickle.load(fopen)
len_sentences = np.array([len(i.split()) for i in trainset_data.data])
maxlen = np.ceil(len_sentences.mean()).astype('int')
data_X = np.zeros((len(trainset_data.data), maxlen))
for i in range(data_X.shape[0]):
    tokens = trainset_data.data[i].split()[:maxlen]
    for no, text in enumerate(tokens[::-1]):
        try:
            data_X[i, -1 - no] = dict_emotion[text]
        except:
            continue
train_X, test_X, train_Y, test_Y = train_test_split(data_X, trainset_data.target, test_size = 0.2)
params_xgd = {
    'min_child_weight': 10.0,
    'objective': 'multi:softprob',
    'eval_metric': 'mlogloss',
    'num_class': len(trainset_data.target_names),
    'max_depth': 7,
    'max_delta_step': 1.8,
    'colsample_bytree': 0.4,
    'subsample': 0.8,
    'eta': 0.03,
    'gamma': 0.65,
    'num_boost_round' : 700,
    'gpu_id': 0,
    'tree_method': 'gpu_hist'
    }
d_train = xgb.DMatrix(train_X, train_Y)
d_valid = xgb.DMatrix(test_X, test_Y)
watchlist = [(d_train, 'train'), (d_valid, 'valid')]
#with open('clf.p', 'rb') as fopen:
#    clf = pickle.load(fopen)
t=time.time()
clf = xgb.train(params_xgd, d_train, 100000, watchlist, early_stopping_rounds=200, maximize=False, verbose_eval=100)
print(round(time.time()-t, 3), 'Seconds to train xgb')
np.mean(test_Y == np.argmax(clf.predict(xgb.DMatrix(test_X), ntree_limit=clf.best_ntree_limit), axis = 1))
clf.save_model('0001.model')
bst = xgb.Booster(params_xgd)
bst.load_model('0001.model')
with open('xgb-timestamp-param', 'w') as fopen:
    fopen.write(json.dumps(params_xgd))
np.mean(test_Y == np.argmax(bst.predict(xgb.DMatrix(test_X)), axis = 1))
from sklearn import metrics
print(metrics.classification_report(test_Y, np.argmax(bst.predict(xgb.DMatrix(test_X)), axis = 1), target_names = trainset_data.target_names))<jupyter_output>precision    recall  f1-score   support

      anger       0.48      0.22      0.31     11390
       fear       0.46      0.20      0.28      9759
        joy       0.48      0.72      0.58     27981
       love       0.26      0.08      0.12      6838
    sadness       0.49      0.58      0.53     24395
   surprise       0.21      0.07      0.11      2999

avg / total       0.45      0.47      0.44     83362
{'fraction_non_alphanumeric': 0.13480266529984622, 'fraction_numerical': 0.04433623782675551, 'mean_word_length': 3.303197353914002, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '355737', 'license': 'MIT', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1400, 'n_words': 323, 'path': 'classification-comparison/XGB/xgb-timestamp-avg.ipynb', 'repo_name': 'huseinzol05/Tensorflow-NLP-Models', 'size': '3902'}