{"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":"# Overview\nThis is a very quick exploration of the problem end-to-end. \n\nIn this work, we have reviewed the datasets and tested our baseline model on the subset of the data. \n\n* Initial thoughts:\n\n    1. Need to handle large amount of input data - try using chunks like we do use batches in deep learning\n    2. Need to handle datatypes of the data - handle categorical columns\n    3. Need to handle the missing values. - Understand is missing leads to any signal. \n    4. Train various models and evaluate on validation dataset.- Ensembels , Fastai \n    5. Finally make predictions on test set to submit\n    \n    \n* Update on 2022-11-27:\n\n    1. Fastai - Tabular Implementation is tested \n    2. test_dl method is used to apply all preprocessing done on training set to test set automatically\n    3. Still need to understand, what is amex_metric and how to use it.\n    4. I am still exploring on loading large datasets, found few good articles talking about frameworks like dask, datatable, and rapids and file formats like feather, parquet, etc. \n    5. Is the current way of doing could help us, as of now we are performing batch wise training and predictions. \n    6. For inderencing, how can I make batched prediction, and create a huge submission.csv wth all predictions for final submission, is there a way similar to read in chunks from a file, so that we can write in chunks to a file. I am assuming, with file append does the job. \n    ","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-27T10:42:50.741974Z","iopub.execute_input":"2022-11-27T10:42:50.742490Z","iopub.status.idle":"2022-11-27T10:42:50.779205Z","shell.execute_reply.started":"2022-11-27T10:42:50.742383Z","shell.execute_reply":"2022-11-27T10:42:50.777774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai import *\nfrom fastai.tabular.all import *","metadata":{"execution":{"iopub.status.busy":"2022-11-27T10:42:51.218501Z","iopub.execute_input":"2022-11-27T10:42:51.218958Z","iopub.status.idle":"2022-11-27T10:42:53.922674Z","shell.execute_reply.started":"2022-11-27T10:42:51.218920Z","shell.execute_reply":"2022-11-27T10:42:53.921252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub_path = \"/kaggle/input/amex-default-prediction/sample_submission.csv\"\ndf_train_path = \"/kaggle/input/amex-default-prediction/train_data.csv\"\ndf_test_path = \"/kaggle/input/amex-default-prediction/test_data.csv\"\ndf_labels_path = \"/kaggle/input/amex-default-prediction/train_labels.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:01:46.594565Z","iopub.execute_input":"2022-11-27T11:01:46.595023Z","iopub.status.idle":"2022-11-27T11:01:46.600934Z","shell.execute_reply.started":"2022-11-27T11:01:46.594985Z","shell.execute_reply":"2022-11-27T11:01:46.599611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_size = 10000\nbatch_size = 200\n\ntrain_reader = pd.read_csv(df_train_path, dtype=str, chunksize=batch_size)\nlabels_reader = pd.read_csv(df_labels_path, dtype=str, chunksize=batch_size)\ntest_reader = pd.read_csv(df_test_path, dtype=str, chunksize=batch_size)\n\n# df_train = pd.read_csv(df_train_path).sample(n_samples, random_state=44)\n# df_labels = pd.read_csv(df_labels_path).sample(n_samples, random_state=44)\n# df_test = pd.read_csv(df_test_path).sample(n_samples, random_state=44)\n# df_sub = pd.read_csv(df_sub_path).sample(n_samples, random_state=44)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:18.885571Z","iopub.execute_input":"2022-11-27T11:04:18.886547Z","iopub.status.idle":"2022-11-27T11:04:18.904239Z","shell.execute_reply.started":"2022-11-27T11:04:18.886505Z","shell.execute_reply":"2022-11-27T11:04:18.902990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = train_reader.get_chunk(sample_size)\ndf_labels = labels_reader.get_chunk(sample_size)\ndf_test = test_reader.get_chunk(sample_size)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:19.108375Z","iopub.execute_input":"2022-11-27T11:04:19.108781Z","iopub.status.idle":"2022-11-27T11:04:21.106703Z","shell.execute_reply.started":"2022-11-27T11:04:19.108747Z","shell.execute_reply":"2022-11-27T11:04:21.105366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:21.108665Z","iopub.execute_input":"2022-11-27T11:04:21.109353Z","iopub.status.idle":"2022-11-27T11:04:21.135586Z","shell.execute_reply.started":"2022-11-27T11:04:21.109312Z","shell.execute_reply":"2022-11-27T11:04:21.134424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:21.136928Z","iopub.execute_input":"2022-11-27T11:04:21.137376Z","iopub.status.idle":"2022-11-27T11:04:21.153228Z","shell.execute_reply.started":"2022-11-27T11:04:21.137338Z","shell.execute_reply":"2022-11-27T11:04:21.152353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:21.155808Z","iopub.execute_input":"2022-11-27T11:04:21.156862Z","iopub.status.idle":"2022-11-27T11:04:21.185853Z","shell.execute_reply.started":"2022-11-27T11:04:21.156782Z","shell.execute_reply":"2022-11-27T11:04:21.184905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy = pd.merge(df_train, df_labels, on='customer_ID')","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:21.187292Z","iopub.execute_input":"2022-11-27T11:04:21.188283Z","iopub.status.idle":"2022-11-27T11:04:21.243423Z","shell.execute_reply.started":"2022-11-27T11:04:21.188231Z","shell.execute_reply":"2022-11-27T11:04:21.242306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy = df_train_Xy.drop(columns=['S_2', 'customer_ID'])","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:21.244707Z","iopub.execute_input":"2022-11-27T11:04:21.245120Z","iopub.status.idle":"2022-11-27T11:04:21.400488Z","shell.execute_reply.started":"2022-11-27T11:04:21.245058Z","shell.execute_reply":"2022-11-27T11:04:21.399018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:21.403931Z","iopub.execute_input":"2022-11-27T11:04:21.404685Z","iopub.status.idle":"2022-11-27T11:04:21.414479Z","shell.execute_reply.started":"2022-11-27T11:04:21.404630Z","shell.execute_reply":"2022-11-27T11:04:21.413305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68','target']","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:22.323463Z","iopub.execute_input":"2022-11-27T11:04:22.324133Z","iopub.status.idle":"2022-11-27T11:04:22.329349Z","shell.execute_reply.started":"2022-11-27T11:04:22.324094Z","shell.execute_reply":"2022-11-27T11:04:22.328087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_cols = list(df_train_Xy.columns)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:22.910460Z","iopub.execute_input":"2022-11-27T11:04:22.910874Z","iopub.status.idle":"2022-11-27T11:04:22.916476Z","shell.execute_reply.started":"2022-11-27T11:04:22.910841Z","shell.execute_reply":"2022-11-27T11:04:22.915121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cont_cols = list(set(all_cols)-set(categorical_cols))","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:24.100480Z","iopub.execute_input":"2022-11-27T11:04:24.100902Z","iopub.status.idle":"2022-11-27T11:04:24.105904Z","shell.execute_reply.started":"2022-11-27T11:04:24.100864Z","shell.execute_reply":"2022-11-27T11:04:24.104755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cont_cols, categorical_cols, target_col = cont_cols[:10], categorical_cols[:10], categorical_cols[-1:]\nuse_cols = cont_cols +categorical_cols+ target_col","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:24.448175Z","iopub.execute_input":"2022-11-27T11:04:24.448606Z","iopub.status.idle":"2022-11-27T11:04:24.455031Z","shell.execute_reply.started":"2022-11-27T11:04:24.448555Z","shell.execute_reply":"2022-11-27T11:04:24.453590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_col","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:25.350324Z","iopub.execute_input":"2022-11-27T11:04:25.350716Z","iopub.status.idle":"2022-11-27T11:04:25.358664Z","shell.execute_reply.started":"2022-11-27T11:04:25.350684Z","shell.execute_reply":"2022-11-27T11:04:25.357317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy = df_train_Xy[use_cols]","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:26.117765Z","iopub.execute_input":"2022-11-27T11:04:26.118201Z","iopub.status.idle":"2022-11-27T11:04:26.140753Z","shell.execute_reply.started":"2022-11-27T11:04:26.118162Z","shell.execute_reply":"2022-11-27T11:04:26.139793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:27.237877Z","iopub.execute_input":"2022-11-27T11:04:27.238286Z","iopub.status.idle":"2022-11-27T11:04:27.246732Z","shell.execute_reply.started":"2022-11-27T11:04:27.238250Z","shell.execute_reply":"2022-11-27T11:04:27.245784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy[cont_cols] = df_train_Xy[cont_cols].apply(pd.to_numeric, errors='coerce', axis=1).copy()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:32.802809Z","iopub.execute_input":"2022-11-27T11:04:32.803460Z","iopub.status.idle":"2022-11-27T11:04:34.409017Z","shell.execute_reply.started":"2022-11-27T11:04:32.803426Z","shell.execute_reply":"2022-11-27T11:04:34.408132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:34.410746Z","iopub.execute_input":"2022-11-27T11:04:34.411361Z","iopub.status.idle":"2022-11-27T11:04:34.419168Z","shell.execute_reply.started":"2022-11-27T11:04:34.411326Z","shell.execute_reply":"2022-11-27T11:04:34.417916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy['target'] = df_train_Xy['target'].astype('int8').copy()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:35.388580Z","iopub.execute_input":"2022-11-27T11:04:35.388971Z","iopub.status.idle":"2022-11-27T11:04:35.396689Z","shell.execute_reply.started":"2022-11-27T11:04:35.388938Z","shell.execute_reply":"2022-11-27T11:04:35.395512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def coerce_df_columns_to_numeric(df, cont_cols):\n    df[cont_cols] = df[cont_cols].apply(pd.to_numeric, errors='coerce')","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:36.703268Z","iopub.execute_input":"2022-11-27T11:04:36.704335Z","iopub.status.idle":"2022-11-27T11:04:36.709685Z","shell.execute_reply.started":"2022-11-27T11:04:36.704295Z","shell.execute_reply":"2022-11-27T11:04:36.708322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:37.094028Z","iopub.execute_input":"2022-11-27T11:04:37.094437Z","iopub.status.idle":"2022-11-27T11:04:37.103322Z","shell.execute_reply.started":"2022-11-27T11:04:37.094402Z","shell.execute_reply":"2022-11-27T11:04:37.101897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"splits = RandomSplitter(valid_pct=0.2)(range_of(df_train_Xy))","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:42.360185Z","iopub.execute_input":"2022-11-27T11:04:42.360941Z","iopub.status.idle":"2022-11-27T11:04:42.368347Z","shell.execute_reply.started":"2022-11-27T11:04:42.360892Z","shell.execute_reply":"2022-11-27T11:04:42.367121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to = TabularPandas(df_train_Xy, procs=[Categorify, FillMissing,Normalize],\n                   cat_names = categorical_cols,\n                   cont_names = cont_cols,\n                   y_names='target',\n                   y_block = CategoryBlock,\n                   splits=splits)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:42.653432Z","iopub.execute_input":"2022-11-27T11:04:42.654075Z","iopub.status.idle":"2022-11-27T11:04:42.760480Z","shell.execute_reply.started":"2022-11-27T11:04:42.654036Z","shell.execute_reply":"2022-11-27T11:04:42.759307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to.ys.iloc[:2]","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:44.089160Z","iopub.execute_input":"2022-11-27T11:04:44.090032Z","iopub.status.idle":"2022-11-27T11:04:44.102261Z","shell.execute_reply.started":"2022-11-27T11:04:44.089970Z","shell.execute_reply":"2022-11-27T11:04:44.100906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(use_cols)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:44.888483Z","iopub.execute_input":"2022-11-27T11:04:44.889663Z","iopub.status.idle":"2022-11-27T11:04:44.899863Z","shell.execute_reply.started":"2022-11-27T11:04:44.889610Z","shell.execute_reply":"2022-11-27T11:04:44.898319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(to.train.xs[categorical_cols])","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:45.728653Z","iopub.execute_input":"2022-11-27T11:04:45.729089Z","iopub.status.idle":"2022-11-27T11:04:45.751520Z","shell.execute_reply.started":"2022-11-27T11:04:45.729050Z","shell.execute_reply":"2022-11-27T11:04:45.750307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = to.dataloaders(bs=64)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:46.527336Z","iopub.execute_input":"2022-11-27T11:04:46.528274Z","iopub.status.idle":"2022-11-27T11:04:46.542559Z","shell.execute_reply.started":"2022-11-27T11:04:46.528223Z","shell.execute_reply":"2022-11-27T11:04:46.541322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:04:47.809869Z","iopub.execute_input":"2022-11-27T11:04:47.810270Z","iopub.status.idle":"2022-11-27T11:04:47.864242Z","shell.execute_reply.started":"2022-11-27T11:04:47.810236Z","shell.execute_reply":"2022-11-27T11:04:47.863163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = tabular_learner(dls, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:05:03.213894Z","iopub.execute_input":"2022-11-27T11:05:03.214276Z","iopub.status.idle":"2022-11-27T11:05:03.229325Z","shell.execute_reply.started":"2022-11-27T11:05:03.214243Z","shell.execute_reply":"2022-11-27T11:05:03.228327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(30)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:54:23.412859Z","iopub.execute_input":"2022-11-27T11:54:23.413247Z","iopub.status.idle":"2022-11-27T11:55:10.978387Z","shell.execute_reply.started":"2022-11-27T11:54:23.413213Z","shell.execute_reply":"2022-11-27T11:55:10.977421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:24.532861Z","iopub.execute_input":"2022-11-27T11:55:24.534213Z","iopub.status.idle":"2022-11-27T11:55:24.587732Z","shell.execute_reply.started":"2022-11-27T11:55:24.534169Z","shell.execute_reply":"2022-11-27T11:55:24.586612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = to.train.xs, to.train.ys.values.ravel()\nX_test, y_test = to.valid.xs, to.valid.ys.values.ravel()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:07:56.555454Z","iopub.execute_input":"2022-11-27T11:07:56.556091Z","iopub.status.idle":"2022-11-27T11:07:56.569246Z","shell.execute_reply.started":"2022-11-27T11:07:56.556056Z","shell.execute_reply":"2022-11-27T11:07:56.567851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:12:24.357480Z","iopub.execute_input":"2022-11-27T11:12:24.357882Z","iopub.status.idle":"2022-11-27T11:12:24.585624Z","shell.execute_reply.started":"2022-11-27T11:12:24.357849Z","shell.execute_reply":"2022-11-27T11:12:24.584461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = RandomForestClassifier(n_estimators=100, max_depth=8, random_state=0)\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:36.340560Z","iopub.execute_input":"2022-11-27T11:55:36.340983Z","iopub.status.idle":"2022-11-27T11:55:37.525204Z","shell.execute_reply.started":"2022-11-27T11:55:36.340946Z","shell.execute_reply":"2022-11-27T11:55:37.524035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:38.733635Z","iopub.execute_input":"2022-11-27T11:55:38.734696Z","iopub.status.idle":"2022-11-27T11:55:38.787384Z","shell.execute_reply.started":"2022-11-27T11:55:38.734643Z","shell.execute_reply":"2022-11-27T11:55:38.785922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"use_cols","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:40.193918Z","iopub.execute_input":"2022-11-27T11:55:40.194315Z","iopub.status.idle":"2022-11-27T11:55:40.201535Z","shell.execute_reply.started":"2022-11-27T11:55:40.194282Z","shell.execute_reply":"2022-11-27T11:55:40.200521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = df_test[use_cols[:-1]].copy()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:24:25.218524Z","iopub.execute_input":"2022-11-27T11:24:25.218967Z","iopub.status.idle":"2022-11-27T11:24:25.230615Z","shell.execute_reply.started":"2022-11-27T11:24:25.218929Z","shell.execute_reply":"2022-11-27T11:24:25.229313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[cont_cols] = df_test[cont_cols].apply(pd.to_numeric, errors='coerce', axis=1).copy()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:25:28.975456Z","iopub.execute_input":"2022-11-27T11:25:28.976483Z","iopub.status.idle":"2022-11-27T11:25:30.258496Z","shell.execute_reply.started":"2022-11-27T11:25:28.976436Z","shell.execute_reply":"2022-11-27T11:25:30.257351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dls = learn.dls.test_dl(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:46.728094Z","iopub.execute_input":"2022-11-27T11:55:46.728543Z","iopub.status.idle":"2022-11-27T11:55:46.803172Z","shell.execute_reply.started":"2022-11-27T11:55:46.728508Z","shell.execute_reply":"2022-11-27T11:55:46.801888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = learn.get_preds(dl=test_dls)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:47.932203Z","iopub.execute_input":"2022-11-27T11:55:47.932595Z","iopub.status.idle":"2022-11-27T11:55:48.670504Z","shell.execute_reply.started":"2022-11-27T11:55:47.932563Z","shell.execute_reply":"2022-11-27T11:55:48.669097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_preds[0])","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:51.398969Z","iopub.execute_input":"2022-11-27T11:55:51.399414Z","iopub.status.idle":"2022-11-27T11:55:51.409233Z","shell.execute_reply.started":"2022-11-27T11:55:51.399378Z","shell.execute_reply":"2022-11-27T11:55:51.407643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.argmax(test_preds[0], axis=1).sum()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:55:51.629250Z","iopub.execute_input":"2022-11-27T11:55:51.629645Z","iopub.status.idle":"2022-11-27T11:55:51.638689Z","shell.execute_reply.started":"2022-11-27T11:55:51.629611Z","shell.execute_reply":"2022-11-27T11:55:51.637597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def amex_metric(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n\n    def top_four_percent_captured(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n        df = (pd.concat([y_true, y_pred], axis='columns')\n              .sort_values('prediction', ascending=False))\n        df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1)\n        four_pct_cutoff = int(0.04 * df['weight'].sum())\n        df['weight_cumsum'] = df['weight'].cumsum()\n        df_cutoff = df.loc[df['weight_cumsum'] <= four_pct_cutoff]\n        return (df_cutoff['target'] == 1).sum() / (df['target'] == 1).sum()\n        \n    def weighted_gini(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n        df = (pd.concat([y_true, y_pred], axis='columns')\n              .sort_values('prediction', ascending=False))\n        df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1)\n        df['random'] = (df['weight'] / df['weight'].sum()).cumsum()\n        total_pos = (df['target'] * df['weight']).sum()\n        df['cum_pos_found'] = (df['target'] * df['weight']).cumsum()\n        df['lorentz'] = df['cum_pos_found'] / total_pos\n        df['gini'] = (df['lorentz'] - df['random']) * df['weight']\n        return df['gini'].sum()\n\n    def normalized_weighted_gini(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n        y_true_pred = y_true.rename(columns={'target': 'prediction'})\n        return weighted_gini(y_true, y_pred) / weighted_gini(y_true, y_true_pred)\n\n    g = normalized_weighted_gini(y_true, y_pred)\n    d = top_four_percent_captured(y_true, y_pred)\n\n    return 0.5 * (g + d)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T11:57:14.315997Z","iopub.execute_input":"2022-11-27T11:57:14.316387Z","iopub.status.idle":"2022-11-27T11:57:14.330350Z","shell.execute_reply.started":"2022-11-27T11:57:14.316354Z","shell.execute_reply":"2022-11-27T11:57:14.329458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}