Kaggle competition: Jigsaw Multilingual Toxic Comment Classification
<SOS> metric: AUC Kaggle data source: jigsaw-multilingual-toxic-comment-classification<count_values>
train = pd.read_csv('.. /input/jigsaw-train-translated-yandex-api/train_yandex.csv')
 val = pd.read_csv('.. /input/jigsaw-multilingual-toxic-comment-classification/validation.csv', usecols=['comment_text', 'toxic', 'lang'] )
tmp = app_train['CNT_CHILDREN'].value_counts().to_frame()
tmp['%'] =(app_train['CNT_CHILDREN'].value_counts() *100 / app_train.shape[0])
 tmp<data_type_conversions>
test = pd.read_csv('.. /input/jigsaw-multilingual-toxic-comment-classification/test.csv')
 test['comment_text'] = test['content']
test['toxic'] = 0.5
app_train['CNT_CHILDREN'] = app_train['CNT_CHILDREN'].astype('object')
 app_test['CNT_CHILDREN'] = app_test['CNT_CHILDREN'].astype('object' )<feature_engineering>
[train_v, val] = train_test_split(val, test_size = 0.05, random_state = 411 )
print('Default',app_train[app_train['AMT_INCOME_TOTAL'] >0.2e8]['AMT_INCOME_TOTAL'])
 app_train['AMT_INCOME_TOTAL'] = np.log(app_train['AMT_INCOME_TOTAL'])
 app_test['AMT_INCOME_TOTAL'] = np.log(app_test['AMT_INCOME_TOTAL'] )<feature_engineering>
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
 def tokenize(s):
return re_tok.sub(r' \1 ', s ).split()
app_train['AMT_CREDIT'] = np.sqrt(app_train['AMT_CREDIT'])
 app_test['AMT_CREDIT'] = np.sqrt(app_test['AMT_CREDIT'] )<feature_engineering>
COMMENT = 'comment_text'
LABEL = 'toxic'
app_train['AMT_ANNUITY'] = np.log(app_train['AMT_ANNUITY'])
 app_test['AMT_ANNUITY'] = np.log(app_test['AMT_ANNUITY'] )<data_type_conversions>
binary = False
app_train[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE', 'NAME_FAMILY_STATUS',
'NAME_HOUSING_TYPE']] = app_train[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE',
'NAME_FAMILY_STATUS', 'NAME_HOUSING_TYPE']].astype('object')
 app_test[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE', 'NAME_FAMILY_STATUS',
'NAME_HOUSING_TYPE']] = app_test[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE',
'NAME_FAMILY_STATUS', 'NAME_HOUSING_TYPE']].astype('object' )<filter>
vec_train = TfidfVectorizer(ngram_range=(1,1), tokenizer=tokenize,
min_df=2, max_df=0.9, strip_accents='unicode', use_idf=1, binary=binary,
smooth_idf=1, sublinear_tf=1)
 vec_train.fit(train[COMMENT])
 val_on_train = vec_train.transform(val[COMMENT])
 trn_on_train = vec_train.transform(train[COMMENT] )
((app_train['DAYS_EMPLOYED']/-365)[(app_train['DAYS_EMPLOYED']/-365)<0][:5],
app_train['DAYS_EMPLOYED'][app_train['DAYS_EMPLOYED']>0][:5],
app_test['DAYS_EMPLOYED'][app_test['DAYS_EMPLOYED']>0][:5] )<categorify>
vec_val = TfidfVectorizer(ngram_range=(1,1), tokenizer=tokenize,
min_df=2, max_df=0.9, strip_accents='unicode', use_idf=1, binary=binary,
smooth_idf=1, sublinear_tf=1)
 vec_val.fit(pd.concat([train_v[COMMENT], val[COMMENT]]))
trn_on_val = vec_val.transform(train_v[COMMENT])
 val_on_val = vec_val.transform(val[COMMENT] )
app_train['DAYS_EMPLOYED'].replace({365243:np.nan},inplace=True)
 app_test['DAYS_EMPLOYED'].replace({365243:np.nan},inplace=True )<data_type_conversions>
x_on_train = trn_on_train
val_x_on_train = val_on_train
x_on_val = trn_on_val
val_x_on_val = val_on_val
app_train['OWN_CAR_AGE'] = app_train['OWN_CAR_AGE'].astype('object')
 app_test['OWN_CAR_AGE'] = app_test['OWN_CAR_AGE'].astype('object' )<data_type_conversions>
y_val = val[LABEL].values
app_train_col_drop.append('FLAG_MOBIL')
 app_train_col_drop.append('FLAG_CONT_MOBILE')
 app_train_col_drop.append('FLAG_EMAIL')
 app_train[['FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']] = app_train[[
'FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']].astype('object')
 app_test[['FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']] = app_test[[
'FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']].astype('object' )<data_type_conversions>
def pr_train(y_i, y):
p = x_on_train[y==y_i].sum(0)
 return(p+3)/(( y==y_i ).sum() +3)
 def pr_val(y_i, y):
p = x_on_val[y==y_i].sum(0)
 return(p+0.2)/(( y==y_i ).sum() +0.2)
 y_train = train[LABEL].values
y_train_v = train_v[LABEL].values
r_train = np.log(pr_train(1,y_train)/ pr_train(0,y_train))
r_val = np.log(pr_val(1,y_train_v)/ pr_val(0,y_train_v))
app_train['CNT_FAM_MEMBERS'] = app_train['CNT_FAM_MEMBERS'].astype('object')
 app_test['CNT_FAM_MEMBERS'] = app_test['CNT_FAM_MEMBERS'].astype('object' )<data_type_conversions>
x_nb_on_train = x_on_train.multiply(r_train)
 C_PARAMETERS = [1.5, 2, 4]
models_train = [LogisticRegression(C=c, dual=True, solver='liblinear')for c in C_PARAMETERS]
preds_val_on_train = []
for model in models_train:
model.fit(x_nb_on_train, y_train)
 p = model.predict_proba(val_x_on_train.multiply(r_train)) [:,1]
print(roc_auc_score(y_val, p))
preds_val_on_train.append(p)
 preds_ensemble_val_on_train = 2**(( np.log2(preds_val_on_train[1])+ np.log2(preds_val_on_train[2])) /2)
 print(roc_auc_score(y_val, preds_ensemble_val_on_train))
app_train[['REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']] = app_train[[
'REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']].astype('object')
 app_test[['REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']] = app_test[[
'REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']].astype('object' )<data_type_conversions>
x_nb_on_val = x_on_val.multiply(r_val)
 models_train_v = [LogisticRegression(C=c, dual=True, solver='liblinear')for c in C_PARAMETERS]
preds_val_on_val = []
for model in models_train_v:
model.fit(x_nb_on_val, y_train_v)
 p = model.predict_proba(val_x_on_val.multiply(r_val)) [:,1]
print(roc_auc_score(y_val, p))
preds_val_on_val.append(p)
 preds_ensemble_val_on_val = 2**(( np.log2(preds_val_on_val[1])+ np.log2(preds_val_on_val[2])) /2)
 print(roc_auc_score(y_val, preds_ensemble_val_on_val))
app_train['WEEKDAY_APPR_PROCESS_START'] = app_train['WEEKDAY_APPR_PROCESS_START'].astype('object')
 app_test['WEEKDAY_APPR_PROCESS_START'] = app_test['WEEKDAY_APPR_PROCESS_START'].astype('object' )<data_type_conversions>
preds_val = [
models_train[1].predict_proba(val_x_on_train.multiply(r_train)) [:,1],
models_train[2].predict_proba(val_x_on_train.multiply(r_train)) [:,1],
models_train_v[1].predict_proba(val_x_on_val.multiply(r_val)) [:,1],
models_train_v[2].predict_proba(val_x_on_val.multiply(r_val)) [:,1],
]
app_train['HOUR_APPR_PROCESS_START'] = app_train['HOUR_APPR_PROCESS_START'].astype('object')
 app_test['HOUR_APPR_PROCESS_START'] = app_test['HOUR_APPR_PROCESS_START'].astype('object' )<data_type_conversions>
preds_val_ens = 2**np.mean([np.log2(p)for p in preds_val], axis = 0)
 print(roc_auc_score(y_val, preds_val_ens))
app_train_col_drop.append('REG_REGION_NOT_LIVE_REGION')
 app_train[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']] = app_train[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']].astype('object')
 app_test[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']] = app_test[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']].astype('object' )<data_type_conversions>
is_val_lang = test.lang.isin(['tr', 'es', 'it'] )
app_train['ORGANIZATION_TYPE'].replace('XNA',np.nan,inplace=True)
 app_test['ORGANIZATION_TYPE'].replace('XNA',np.nan,inplace=True)
 app_train['ORGANIZATION_TYPE'] = app_train['ORGANIZATION_TYPE'].astype('object')
 app_test['ORGANIZATION_TYPE'] = app_test['ORGANIZATION_TYPE'].astype('object' )<data_type_conversions>
np.sum(is_val_lang.values )
app_train[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE', 'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']] = app_train[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE',
'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']].astype('object')
 app_test[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE', 'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']] = app_test[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE',
'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']].astype('object' )<data_type_conversions>
test_val_lang = test.loc[is_val_lang, COMMENT]
test_non_val_lang = test.loc[~is_val_lang, COMMENT]
app_train[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]] = app_train[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]].astype('object')
 app_test[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]] = app_test[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]].astype('object' )<drop_column>
test_val_lang_on_train = vec_train.transform(test_val_lang)
 test_nonval_lang_on_train = vec_train.transform(test_non_val_lang)
 test_val_lang_on_val = vec_val.transform(test_val_lang )
app_train[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]] = app_train[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]].astype('object')
 app_test[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]] = app_test[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]].astype('object')
 app_train_col_drop.extend(['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_9',
'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11', 'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14',
'FLAG_DOCUMENT_15', 'FLAG_DOCUMENT_16', 'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18',
'FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21'] )<data_type_conversions>
test_val_lang_on_train, test_nonval_lang_on_train, test_val_lang_on_val
app_train[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]] = app_train[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]].astype('object')
 app_test[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]] = app_test[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]].astype('object')
<set_options>
preds1_val_lang_on_train = models_train[0].predict_proba(test_val_lang_on_train.multiply(r_train)) [:,1]
preds2_val_lang_on_train = models_train[1].predict_proba(test_val_lang_on_train.multiply(r_train)) [:,1]
preds4_val_lang_on_train = models_train[2].predict_proba(test_val_lang_on_train.multiply(r_train)) [:,1]
preds1_nonval_lang_on_train = models_train[0].predict_proba(test_nonval_lang_on_train.multiply(r_train)) [:,1]
preds2_nonval_lang_on_train = models_train[1].predict_proba(test_nonval_lang_on_train.multiply(r_train)) [:,1]
preds4_nonval_lang_on_train = models_train[2].predict_proba(test_nonval_lang_on_train.multiply(r_train)) [:,1]
gc.collect()<drop_column>
preds1_val_lang_on_val = models_train_v[0].predict_proba(test_val_lang_on_val.multiply(r_val)) [:,1]
preds2_val_lang_on_val = models_train_v[1].predict_proba(test_val_lang_on_val.multiply(r_val)) [:,1]
preds4_val_lang_on_val = models_train_v[2].predict_proba(test_val_lang_on_val.multiply(r_val)) [:,1]
train_test = train_test.drop(list(set(app_train_col_drop)) , axis=1)
 categorical_col = train_test.select_dtypes('object' ).columns
numeric_col = train_test.select_dtypes('number' ).columns
numeric_col = numeric_col.drop('TARGET')
 gc.collect()<categorify>
preds_val = 2**(( np.log2(preds2_val_lang_on_train)+ np.log2(preds4_val_lang_on_train)+np.log2(preds2_val_lang_on_val)+ np.log2(preds4_val_lang_on_val)) / 4)
 preds_nonval = 2**(( np.log2(preds2_nonval_lang_on_train)+ np.log2(preds4_nonval_lang_on_train)) / 2 )
train_test = replace_XNA_XAP(train_test)
 train_test,_ = binary_encoding(train_test, categorical_col)
 train_test,_ = one_hot_encoding(train_test,categorical_col,nan_as_category=True)
 descriptive_stat_feat(train_test,numeric_col)
 del app_train,app_test
reduce_memory_usage(train_test)
 gc.collect()<categorify>
test.loc[is_val_lang, 'toxic'] = preds_val
test.loc[~is_val_lang, 'toxic'] = preds_nonval
bureau_balance = replace_XNA_XAP(bureau_balance)
 bb_bin =[]
bureau_balance, bb_cat = one_hot_encoding(bureau_balance,['STATUS'],nan_as_category=False)
 bb_aggregations = {'MONTHS_BALANCE':['min','max','mean']}
for col in bb_cat+bb_bin:
bb_aggregations[col] =['sum','mean']
bb_agg = bureau_balance.groupby('SK_ID_BUREAU' ).agg(bb_aggregations)
 bb_agg.columns = [e[0]+ "_" +e[1].upper() for e in bb_agg.columns.tolist() ]
bb_agg.head(2 )<data_type_conversions>
submission1 = test[['id', 'toxic', 'lang']]
submission1.to_csv('submission1.csv', index=False )
bureau[['CREDIT_TYPE','CNT_CREDIT_PROLONG']] = bureau[['CREDIT_TYPE','CNT_CREDIT_PROLONG']].astype('object')
<feature_engineering>
submission2 = pd.read_csv('.. /input/tpu-inference-super-fast-xlmroberta/submission.csv' )
bureau['AMT_CREDIT_SUM'] = bureau['AMT_CREDIT_SUM'].apply(np.log1p )<feature_engineering>
submission1['toxic'] = submission1['toxic'] * 0.04 + submission2['toxic'] * 0.96
bureau['AMT_CREDIT_SUM_DEBT'] = np.log1p(bureau['AMT_CREDIT_SUM_DEBT'] )<feature_engineering>
submission1.loc[submission1["lang"] == "es", "toxic"] *= 1.06
submission1.loc[submission1["lang"] == "fr", "toxic"] *= 1.04
submission1.loc[submission1["lang"] == "it", "toxic"] *= 0.97
submission1.loc[submission1["lang"] == "pt", "toxic"] *= 0.96
submission1.loc[submission1["lang"] == "tr", "toxic"] *= 0.98
<categorify><EOS> 
submission1[['id', 'toxic']].to_csv('submission.csv', index=False )