Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<choose_model_class>
import pandas as pd, numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import(train_test_split, RandomizedSearchCV)
 from nltk.corpus import stopwords
import nltk
import re
gbk = GradientBoostingRegressor()
gbk.fit(df_train, y_train)
<predict_on_test>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 subm = pd.read_csv('.. /input/sample_submission.csv')
 SPLIT_TRAINING = False
if SPLIT_TRAINING:
del test
train, test = train_test_split(train, test_size=.8)
 train = train.reset_index()
test = test.reset_index()
preds = gbk.predict(df_test )<save_to_csv>
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
train['none'] = 1-train[label_cols].max(axis=1 )
submmision = pd.read_csv(".. /input/sample_submission.csv.zip")
 submmision["y"] = preds
submmision.to_csv("benz_test.csv", index=False )<set_options>
COMMENT = 'comment_text'
train[COMMENT].fillna("unknown", inplace=True)
 test[COMMENT].fillna("unknown", inplace=True )
print(h2o.__version__)
 h2o.init(max_mem_size='16G' )<load_from_csv>
stop_words_en = set(stopwords.words("english"))
word_pattern = "\w[a-z-_]+\w"
def normalize(s):

 s = s.lower()
s = re.sub(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', '', s)
 s = re.sub(r'(['"\.\(\)\!\?\-\\\/\,])', r' \1 ', s)
 s = re.sub(r'([\;\:\|•«
])', ' ', s)
 s = s.replace('&', ' and ')
 s = s.replace('@', ' at ')
 s = s.replace('0', ' zero ')
 s = s.replace('1', ' one ')
 s = s.replace('2', ' two ')
 s = s.replace('3', ' three ')
 s = s.replace('4', ' four ')
 s = s.replace('5', ' five ')
 s = s.replace('6', ' six ')
 s = s.replace('7', ' seven ')
 s = s.replace('8', ' eight ')
 s = s.replace('9', ' nine ')
 s = s.replace("!", " exclamationmark ")
 s = s.replace("?", " qestionmark ")
 return s
train[COMMENT] = train[COMMENT].map(normalize)
 test[COMMENT] = test[COMMENT].map(normalize )
%%time
train = h2o.import_file(".. /input/mercedes-benz-greener-manufacturing/train.csv")
 test = h2o.import_file(".. /input/mercedes-benz-greener-manufacturing/test.csv")
<drop_column>
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
 def tokenize(s): return re_tok.sub(r' \1 ', s ).split()
x = train.columns
y = 'y'
x.remove(y)
<train_model>
n = train.shape[0]
vec = TfidfVectorizer(tokenizer=tokenize,
min_df=2,
max_df=.625,
strip_accents='unicode',
use_idf=1,
smooth_idf=True,
sublinear_tf=True)
 trn_term_doc = vec.fit_transform(train[COMMENT])
 test_term_doc = vec.transform(test[COMMENT] )
aml = H2OAutoML(max_runtime_secs = 3500, seed = 1, project_name = "lb_frame")
 aml.train(x = x, y = y, training_frame = train )<find_best_params>
def pr(y_i, y):
p = x[y==y_i].sum(0)
 return(p+1)/(( y==y_i ).sum() +1 )
aml.leader<predict_on_test>
x = trn_term_doc
test_x = test_term_doc
pred = aml.predict(test)
 pred.head()<save_model>
class random_log_uniform:
def __init__(self, power_low, power_high):
self.power_low = power_low
self.power_high = power_high
def rvs(self, random_state=0):
rnd_power = np.random.rand() *(self.power_high - self.power_low)+ self.power_low
return 10**rnd_power
def get_mdl_training(y, parameters):
y = y.values
r = np.log(pr(1,y)/ pr(0,y))
model = LogisticRegression(dual=True)
 params = { "C": random_log_uniform(np.log10(0.01), np.log10(10)) }
x_nb = x.multiply(r)
 model_grid = RandomizedSearchCV(model, params, cv=5, n_jobs=1, n_iter=50, scoring='roc_auc')
 model_grid.fit(x_nb,y)
 m = model_grid.best_estimator_
parameters.append(model_grid.best_params_["C"])
 return m.fit(x_nb, y), r
def get_mdl(y,C):
y = y.values
r = np.log(pr(1,y)/ pr(0,y))
m = LogisticRegression(C=C, dual=True)
 x_nb = x.multiply(r)
 return m.fit(x_nb, y), r
h2o.save_model(aml.leader, path = "./product_backorders_model_bin" )<load_from_csv>
preds = np.zeros(( len(test), len(label_cols)))
 Cvalues = [1.391737250242211, 0.17361472592588692, 0.45345054479480851,
0.58227213530514976, 0.59166916356898391, 0.34278082413675537]
TRAINING_HYPERPARAMETERS = False
if TRAINING_HYPERPARAMETERS:
Cvalues = []
for i, j in enumerate(label_cols):
print('fitting %s.. ' %j)
 if TRAINING_HYPERPARAMETERS:
m,r = get_mdl_training(train[j], Cvalues)
 else:
m,r = get_mdl(train[j], Cvalues[i])
 preds[:,i] = m.predict_proba(test_x.multiply(r)) [:,1]
if TRAINING_HYPERPARAMETERS:
print("\tfitted coefficients: ", Cvalues )
sample_submission = pd.read_csv('.. /input/mercedes-benz-greener-manufacturing/sample_submission.csv')
 sample_submission.shape<save_to_csv>
submid = pd.DataFrame({'id': subm["id"]})
 submission = pd.concat([submid, pd.DataFrame(preds, columns = label_cols)], axis=1)
 submission.to_csv('submission.csv', index=False )
<save_model><EOS> 
if SPLIT_TRAINING:
for index, _ in enumerate(label_cols):
print("%.5f" % roc_auc_score(test[label_cols].values[:,index], preds[:,index]), end=" ")
 print("
%.5f" %roc_auc_score(test[label_cols].values, preds))