Kaggle competition: Jigsaw Multilingual Toxic Comment Classification
<SOS> metric: AUC Kaggle data source: jigsaw-multilingual-toxic-comment-classification<split>
import numpy as np
import pandas as pd
import os
train_index, test_index = train_test_split(df.index.values, test_size=0.1 )<train_model>
LANG = "ru"
DIR = f".. /input/{LANG}-changed-subs/"
WEIGHT = 1
def evaluate_model(df, target, train_index, test_index, params):
model = LGBMClassifier(**params)
 model.fit(df.iloc[train_index], target.iloc[train_index])
 return log_loss(target.iloc[test_index], model.predict_proba(df.iloc[test_index]))<define_search_space>
submission = pd.read_csv(".. /input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv")
 test = pd.read_csv(".. /input/jigsaw-multilingual-toxic-comment-classification/test.csv")
 sub_best = pd.read_csv(os.path.join(DIR, "sub-LB-9549.csv"))
param_grid = {
'learning_rate': [0.05, 0.02, 0.01],
'num_leaves': [25, 38, 63],
'n_estimators': [100, 200, 400],
'min_child_samples': [5, 10, 20, 40, 100],
'colsample_bytree': [0.8, 0.9, 1],
'subsample': [0.8, 0.9, 1],
'reg_alpha': [0.1, 0.2, 0.4, 0.6, 0.8],
'reg_lambda': [0.1, 0.2, 0.4, 0.6, 0.8],
}
best_eval_score = 0
for i in range(50):
params = {k: np.random.choice(v)for k, v in param_grid.items() }
score = evaluate_model(df, bin_target, train_index, test_index, params)
 if score < best_eval_score or best_eval_score == 0:
best_eval_score = score
best_params = params
print(best_eval_score)
 print("Best evaluation logloss", best_eval_score )<train_model>
files_sub = os.listdir(DIR)
 files_sub = sorted(files_sub)
 print(len(files_sub))
files_sub
clf = LGBMClassifier(**best_params)
 clf.fit(df, bin_target )<feature_engineering>
for file in files_sub:
test[file.replace(".csv", "")] = pd.read_csv(os.path.join(DIR, file)) ["toxic"]
test_df_columns = ['time', 'assetCode', 'assetName', 'volume', 'close', 'open',
'returnsClosePrevRaw1', 'returnsOpenPrevRaw1',
'returnsClosePrevMktres1', 'returnsOpenPrevMktres1',
'returnsClosePrevRaw10', 'returnsOpenPrevRaw10',
'returnsClosePrevMktres10', 'returnsOpenPrevMktres10']
base_df = market_train_df[market_train_df['time'] >= '2016-10-01']
base_df = base_df[test_df_columns]
base_df['id'] = -1
base_df.shape<categorify>
test = test.loc[test["lang"]==LANG].reset_index(drop=True)
 test.head(1 )
def write_submission(model, env):
days = env.get_prediction_days()
day_id = 0
market_obs_df_append = None
for(market_obs_df, news_obs_df, predictions_template_df)in days:
news_obs_df = preprocess_news(news_obs_df)
 index_df = unstack_asset_codes(news_obs_df)
 news_unstack = merge_news_on_index(news_obs_df, index_df)
 news_obs_agg = group_news(news_unstack)
 market_obs_df['id'] = day_id
if market_obs_df_append is None:
market_obs_df_append = base_df
market_obs_df_append = pd.concat([market_obs_df_append,market_obs_df],
ignore_index=True,
sort=False)
 market_obs_process = process_date(market_obs_df_append)
 market_obs_process = process_ma(market_obs_process)
 market_obs_df = market_obs_process[market_obs_process['id']==day_id]
obs_df = market_obs_df.merge(news_obs_agg, how='left', on=['assetCode', 'date'])
 del market_obs_df, news_obs_agg, news_obs_df, news_unstack, index_df
gc.collect()
obs_df = obs_df[obs_df.assetCode.isin(predictions_template_df.assetCode)]
feats = [c for c in obs_df.columns if c not in ['date', 'assetCode', 'assetName', 'time', 'id']]
preds = model.predict_proba(obs_df[feats])[:, 1] * 2 - 1
sub = pd.DataFrame({'assetCode': obs_df['assetCode'], 'confidence': preds})
 predictions_template_df = predictions_template_df.merge(sub, how='left' ).drop(
 'confidenceValue', axis=1 ).fillna(0 ).rename(columns={'confidence':'confidenceValue'})
 env.predict(predictions_template_df)
 if day_id == 59:
market_obs_df_append.drop(
 market_obs_df_append.index[market_obs_df_append['id']==-1],
inplace=True)
 elif day_id >= 60:
market_obs_df_append.drop(
 market_obs_df_append.index[market_obs_df_append['id']==day_id-60],
inplace=True)
 day_id += 1
del obs_df, predictions_template_df, preds, sub
gc.collect()
env.write_submission_file()
print('day_count',day_id )<save_to_csv>
test["diff_good1"] = test[f"{LANG}-9397"] - test[f"{LANG}-9373"]
test["diff_good2"] = test[f"{LANG}-9476"] - test[f"{LANG}-9475"]
test["diff_good3"] = test[f"{LANG}-9529"] - test[f"{LANG}-9510"]
test["diff_good4"] = test[f"{LANG}-9544"] - test[f"{LANG}-9543"]
test["diff_bad1"] = test[f"{LANG}-9545"] - test[f"{LANG}-9543-from-9545"]
write_submission(clf, env )<set_options>
test["sub_best"] = test["sub-LB-9549"]
col_comment = ["id", "content", "sub_best"]
col_diff = [column for column in test.columns if "diff" in column]
test_diff = test[col_comment + col_diff].reset_index(drop=True)
 test_diff["diff_avg"] = test_diff[col_diff].mean(axis=1 )
warnings.filterwarnings("ignore",category=DeprecationWarning)
 env = twosigmanews.make_env() 
(market_train, news_train)= env.get_training_data()<set_options>
test_diff["sub_new"] = test_diff.apply(lambda x:(1+WEIGHT*x["diff_avg"])*x["sub_best"] if x["diff_avg"]<0 else(1-WEIGHT*x["diff_avg"])*x["sub_best"] + WEIGHT*x["diff_avg"] , axis=1 )
def show_mem_usage() :
ipython_vars = ['In', 'Out', 'exit', 'quit', 'get_ipython', 'ipython_vars']
list_objects=[]
mem = 0
for x in globals() :
if x.startswith('_'): continue
if x in sys.modules: continue
if x in ipython_vars: continue
if isinstance(globals().get(x), pd.DataFrame):
mem = sys.getsizeof(globals().get(x)) /1e+6
if mem > 1:
list_objects.append([x, mem ])
 else:
for o in dir(globals().get(x)) :
if o.startswith('__'): continue
mem = sys.getsizeof(getattr(globals().get(x), o)) /1e+6
if mem > 1:
list_objects.append(['.'.join([x,o]), mem ])
 return sorted(list_objects, key=lambda x: x[1], reverse=True )<categorify>
submission["toxic"] = sub_best["toxic"]
submission.loc[test["id"], "toxic"] = test_diff["sub_new"].values
submission.to_csv("submission.csv", index=False )