{"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":"code","source":"!pip install giotto-tda","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-06-13T19:47:51.505260Z","iopub.execute_input":"2022-06-13T19:47:51.505696Z","iopub.status.idle":"2022-06-13T19:48:07.165676Z","shell.execute_reply.started":"2022-06-13T19:47:51.505617Z","shell.execute_reply":"2022-06-13T19:48:07.164733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np, pandas as pd, matplotlib.pyplot as plt, seaborn as sns\nimport os, gc\nimport sklearn\nfrom gtda.time_series import SingleTakensEmbedding, PearsonDissimilarity\nfrom gtda.homology import VietorisRipsPersistence\nfrom gtda.plotting import plot_point_cloud\nfrom gtda.diagrams import Amplitude, PersistenceEntropy\nfrom gtda.plotting import plot_diagram","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-13T19:48:07.167145Z","iopub.execute_input":"2022-06-13T19:48:07.167493Z","iopub.status.idle":"2022-06-13T19:48:08.016424Z","shell.execute_reply.started":"2022-06-13T19:48:07.167422Z","shell.execute_reply":"2022-06-13T19:48:08.015433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain = pd.read_feather('../input/amexfeather/train_data.ftr')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:48:08.017422Z","iopub.execute_input":"2022-06-13T19:48:08.017696Z","iopub.status.idle":"2022-06-13T19:48:26.489386Z","shell.execute_reply.started":"2022-06-13T19:48:08.017670Z","shell.execute_reply":"2022-06-13T19:48:26.488403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\ntrain[\"target\"].hist()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:48:26.492326Z","iopub.execute_input":"2022-06-13T19:48:26.492760Z","iopub.status.idle":"2022-06-13T19:48:26.862967Z","shell.execute_reply.started":"2022-06-13T19:48:26.492716Z","shell.execute_reply":"2022-06-13T19:48:26.861582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def_customer = train[train[\"target\"] == 1][\"customer_ID\"].unique()\nndef_customer = train[train[\"target\"] == 0][\"customer_ID\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:48:26.863926Z","iopub.execute_input":"2022-06-13T19:48:26.864358Z","iopub.status.idle":"2022-06-13T19:48:34.955960Z","shell.execute_reply.started":"2022-06-13T19:48:26.864322Z","shell.execute_reply":"2022-06-13T19:48:34.955120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train[\"customer_ID\"] == def_customer[1]]","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:48:34.957236Z","iopub.execute_input":"2022-06-13T19:48:34.957457Z","iopub.status.idle":"2022-06-13T19:48:35.812955Z","shell.execute_reply.started":"2022-06-13T19:48:34.957437Z","shell.execute_reply":"2022-06-13T19:48:35.812255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_features = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\nbin_features = ['B_31', 'D_87']\ncont_features = sorted([f for f in train.columns if f not in cat_features + bin_features + ['customer_ID', 'target', 'S_2']])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:48:35.814181Z","iopub.execute_input":"2022-06-13T19:48:35.814477Z","iopub.status.idle":"2022-06-13T19:48:35.820370Z","shell.execute_reply.started":"2022-06-13T19:48:35.814447Z","shell.execute_reply":"2022-06-13T19:48:35.819587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Due to the size of the dataset, we use $200$ customers for illustration.","metadata":{}},{"cell_type":"code","source":"sample_customers = []\nncf = len(cont_features)\nfor i, cust_id in enumerate(np.concatenate([def_customer[0:100], ndef_customer[0:100]])):\n    temp = np.zeros((13, ncf))\n    curr_cust = train[train[\"customer_ID\"] == cust_id]\n    curr_cust_days = curr_cust.shape[0]\n    temp[0:curr_cust_days, 0:ncf] = curr_cust[cont_features].fillna(-1).values\n    sample_customers.append(temp)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:54:56.369876Z","iopub.execute_input":"2022-06-13T19:54:56.370198Z","iopub.status.idle":"2022-06-13T19:56:14.480543Z","shell.execute_reply.started":"2022-06-13T19:54:56.370174Z","shell.execute_reply":"2022-06-13T19:56:14.479462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VR = VietorisRipsPersistence(homology_dimensions=[0, 1, 2])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:49:40.533778Z","iopub.execute_input":"2022-06-13T19:49:40.534271Z","iopub.status.idle":"2022-06-13T19:49:40.538143Z","shell.execute_reply.started":"2022-06-13T19:49:40.534233Z","shell.execute_reply":"2022-06-13T19:49:40.537583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Vietoris-Rips Persistence Homology\ncalculate persistence diagrams for customers.","metadata":{}},{"cell_type":"code","source":"diagrams = VR.fit_transform(sample_customers)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:57:31.389280Z","iopub.execute_input":"2022-06-13T19:57:31.389665Z","iopub.status.idle":"2022-06-13T19:58:05.433397Z","shell.execute_reply.started":"2022-06-13T19:57:31.389636Z","shell.execute_reply":"2022-06-13T19:58:05.432582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagrams.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:58:05.434660Z","iopub.execute_input":"2022-06-13T19:58:05.434924Z","iopub.status.idle":"2022-06-13T19:58:05.441083Z","shell.execute_reply.started":"2022-06-13T19:58:05.434901Z","shell.execute_reply":"2022-06-13T19:58:05.440011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_diagram(diagrams[0])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:58:05.442110Z","iopub.execute_input":"2022-06-13T19:58:05.442382Z","iopub.status.idle":"2022-06-13T19:58:05.480808Z","shell.execute_reply.started":"2022-06-13T19:58:05.442355Z","shell.execute_reply":"2022-06-13T19:58:05.480052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_diagram(diagrams[1])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:58:12.835215Z","iopub.execute_input":"2022-06-13T19:58:12.835516Z","iopub.status.idle":"2022-06-13T19:58:12.860423Z","shell.execute_reply.started":"2022-06-13T19:58:12.835495Z","shell.execute_reply":"2022-06-13T19:58:12.859474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_diagram(diagrams[2])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:58:15.885561Z","iopub.execute_input":"2022-06-13T19:58:15.885875Z","iopub.status.idle":"2022-06-13T19:58:15.912145Z","shell.execute_reply.started":"2022-06-13T19:58:15.885853Z","shell.execute_reply":"2022-06-13T19:58:15.911359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Persistence Entropy\nextract scalar features from the persistence diagrams","metadata":{}},{"cell_type":"code","source":"PE = PersistenceEntropy()\nfeatures = PE.fit_transform(diagrams)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:58:19.701157Z","iopub.execute_input":"2022-06-13T19:58:19.701483Z","iopub.status.idle":"2022-06-13T19:58:19.709451Z","shell.execute_reply.started":"2022-06-13T19:58:19.701459Z","shell.execute_reply":"2022-06-13T19:58:19.708519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(features)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:58:21.068236Z","iopub.execute_input":"2022-06-13T19:58:21.068550Z","iopub.status.idle":"2022-06-13T19:58:21.074576Z","shell.execute_reply.started":"2022-06-13T19:58:21.068510Z","shell.execute_reply":"2022-06-13T19:58:21.073986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features","metadata":{"execution":{"iopub.status.busy":"2022-06-13T19:58:22.217649Z","iopub.execute_input":"2022-06-13T19:58:22.218097Z","iopub.status.idle":"2022-06-13T19:58:22.230821Z","shell.execute_reply.started":"2022-06-13T19:58:22.218064Z","shell.execute_reply":"2022-06-13T19:58:22.230317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reference\n1. https://arxiv.org/pdf/2001.07588.pdf\n2. https://arxiv.org/pdf/1701.07857.pdf","metadata":{}}]}