{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Previous version: V1, XGBoost using 4M trainset with uid_cnt \n\nCurrent: XGBoost using 4M trainset with uid_cnt  - Ensemble","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \n\nimport os\nfrom shutil import copyfile\nimport pickle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-23T04:02:52.489432Z","iopub.execute_input":"2023-02-23T04:02:52.489975Z","iopub.status.idle":"2023-02-23T04:02:52.518422Z","shell.execute_reply.started":"2023-02-23T04:02:52.489874Z","shell.execute_reply":"2023-02-23T04:02:52.517256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load helpers\nloading_dir = '/kaggle/input/avazu-ctr-prediction-helpers'\npreprocess_path = loading_dir + '/preprocess.py'\ncopyfile(src = preprocess_path, dst = \"../working/preprocess.py\") \n\nfrom preprocess import *","metadata":{"execution":{"iopub.status.busy":"2023-02-23T04:02:52.520617Z","iopub.execute_input":"2023-02-23T04:02:52.520984Z","iopub.status.idle":"2023-02-23T04:02:53.151377Z","shell.execute_reply.started":"2023-02-23T04:02:52.520950Z","shell.execute_reply":"2023-02-23T04:02:53.150172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load counters\nuid_counter = loading_dir + '/uid_counter.pkl'\n\nwith open(uid_counter, 'rb') as f:\n    uid_counter = pickle.load(f)\n\nprint(uid_counter.most_common(3))","metadata":{"execution":{"iopub.status.busy":"2023-02-23T04:02:53.153191Z","iopub.execute_input":"2023-02-23T04:02:53.153954Z","iopub.status.idle":"2023-02-23T04:03:07.046969Z","shell.execute_reply.started":"2023-02-23T04:02:53.153910Z","shell.execute_reply":"2023-02-23T04:03:07.045689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Debugging and Prediction mode\nDEBUG = False\nif DEBUG:\n    print('Debugging: using reduced test dataset')\n    \npred_mode = 'ensemble' #base or ensemble\nprint('Prediction mode: ', pred_mode)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T04:03:23.687596Z","iopub.execute_input":"2023-02-23T04:03:23.688056Z","iopub.status.idle":"2023-02-23T04:03:23.695463Z","shell.execute_reply.started":"2023-02-23T04:03:23.688021Z","shell.execute_reply":"2023-02-23T04:03:23.693841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare testset\ntest_path = '/kaggle/input/avazu-ctr-prediction/test.gz'\nif DEBUG:\n    chunksize = 2000\n    for c in pd.read_csv(test_path, chunksize=chunksize):\n        test = c\n        break\n        \nelse: #read the whole test set\n    test = pd.read_csv(test_path)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T04:03:07.057773Z","iopub.execute_input":"2023-02-23T04:03:07.058171Z","iopub.status.idle":"2023-02-23T04:03:07.192790Z","shell.execute_reply.started":"2023-02-23T04:03:07.058136Z","shell.execute_reply":"2023-02-23T04:03:07.191574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if pred_mode == 'base':\n    # load clf\n    clf = loading_dir + '/clfs/clf_0.pkl'\n    with open(clf, 'rb') as file:\n        clf = pickle.load(file)\n    # Retrieve clf, encoder\n    encoder = clf['encoder']\n    clf = clf['clf']\n    \n    # encoding\n    test,_,_ = encoding(test, uid_counter, encoder=encoder)\n\n    # prediction\n    preds = clf.predict_proba(test)[:,1]\n    \n    \nelse: #ensemble\n    print('Ensemble predictions')\n    clfs_path = loading_dir + '/clfs' #dir path TBD\n    for r,dirs,files in os.walk(clfs_path):\n        break\n    clfs = [clfs_path + '/' + f for f in files]\n    ls_preds = list()\n    for i in range(len(clfs)):\n        print('- inferencing using clf number: ', i)\n        # Retrieve clf, encoder\n        clf = clfs[i]\n        with open(clf, 'rb') as file:\n            clf = pickle.load(file)\n        encoder = clf['encoder']\n        clf = clf['clf']\n        \n         # encoding\n        test_i,_,_ = encoding(test, uid_counter, encoder=encoder)\n\n        # prediction\n        ls_preds.append(clf.predict_proba(test_i)[:,1])\n        print(' ')\n    # taking average as the final prediction\n    print('Taking average as the final prediction')\n    preds = np.mean(np.array(ls_preds).T, axis=1) #final prediction","metadata":{"execution":{"iopub.status.busy":"2023-02-23T04:03:07.195130Z","iopub.execute_input":"2023-02-23T04:03:07.195452Z","iopub.status.idle":"2023-02-23T04:03:07.752386Z","shell.execute_reply.started":"2023-02-23T04:03:07.195423Z","shell.execute_reply":"2023-02-23T04:03:07.751463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load sub\nprint('Making submission')\nsub = '/kaggle/input/avazu-ctr-prediction/sampleSubmission.gz'\nsub = pd.read_csv(sub)\nsub['click'] = preds\nsub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T04:03:07.753738Z","iopub.execute_input":"2023-02-23T04:03:07.754404Z","iopub.status.idle":"2023-02-23T04:03:10.313010Z","shell.execute_reply.started":"2023-02-23T04:03:07.754367Z","shell.execute_reply":"2023-02-23T04:03:10.311312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}