{"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":"## This notebook shows the digraph traced user behavior. It may be helpful to understand the characteristic of data.","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-03T17:39:29.938549Z","iopub.execute_input":"2022-11-03T17:39:29.939261Z","iopub.status.idle":"2022-11-03T17:39:29.984964Z","shell.execute_reply.started":"2022-11-03T17:39:29.939129Z","shell.execute_reply":"2022-11-03T17:39:29.982967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import graphviz\nfrom tqdm import tqdm\n\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:39:29.988050Z","iopub.execute_input":"2022-11-03T17:39:29.989073Z","iopub.status.idle":"2022-11-03T17:39:30.021037Z","shell.execute_reply.started":"2022-11-03T17:39:29.989016Z","shell.execute_reply":"2022-11-03T17:39:30.019961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_pickle(\"../input/otto-mors-pickled-data/train.pkl\")\ndf = df.drop(\"ts\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:43:02.184101Z","iopub.execute_input":"2022-11-03T17:43:02.184612Z","iopub.status.idle":"2022-11-03T17:43:58.451197Z","shell.execute_reply.started":"2022-11-03T17:43:02.184571Z","shell.execute_reply":"2022-11-03T17:43:58.446608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NODE_LIST = [\"No_action\", \"Click\", \"Cart\", \"Order\"]\n\nEDGE_LIST = [\n    [\"No_action\", \"Click\", 0],\n    [\"No_action\", \"Cart\", 0],\n    [\"No_action\", \"Order\", 0],\n    [\"Click\", \"Click\", 0],\n    [\"Click\", \"Cart\", 0],\n    [\"Click\", \"Order\", 0],\n    [\"Cart\", \"Click\", 0],\n    [\"Cart\", \"Cart\", 0],\n    [\"Cart\", \"Order\", 0],\n    [\"Order\", \"Click\", 0],\n    [\"Order\", \"Cart\", 0],\n    [\"Order\", \"Order\", 0]\n]","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:43:58.456768Z","iopub.execute_input":"2022-11-03T17:43:58.457385Z","iopub.status.idle":"2022-11-03T17:43:58.469993Z","shell.execute_reply.started":"2022-11-03T17:43:58.457323Z","shell.execute_reply":"2022-11-03T17:43:58.468924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef get_index(start, end):\n    return int(start * 3 + end)\n\ndef calc_edgelist(x):\n    edge_list = [0 for i in range(12)]\n    end = -1\n\n    for i,tp in x.iteritems():\n        start = end + 1\n        end = tp\n        edge_list[get_index(start, end)]+=1\n    return edge_list","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:43:58.471448Z","iopub.execute_input":"2022-11-03T17:43:58.472344Z","iopub.status.idle":"2022-11-03T17:43:58.487089Z","shell.execute_reply.started":"2022-11-03T17:43:58.472298Z","shell.execute_reply":"2022-11-03T17:43:58.485699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CHUNK_SIZE=1e4\nsession_length = len(df[\"session\"].unique())-1\n\nedge_list = [0 for i in range(12)]\n\nprint(\"SESSION NUM: %d\"%session_length)\nfor before, chunk in tqdm([(int(i*CHUNK_SIZE), min(session_length, int((i+1)*CHUNK_SIZE))) for i in range(int((session_length/CHUNK_SIZE)+1))]):\n    chunk_df = df[(df[\"session\"]>=before)&(df[\"session\"]<chunk)]\n    result = chunk_df.groupby([\"session\",\"aid\"]).agg(calc_edgelist)\n    edge_list+=np.array(result[\"type\"].to_list()).sum(axis=0)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:46:12.743471Z","iopub.execute_input":"2022-11-03T17:46:12.744135Z","iopub.status.idle":"2022-11-03T17:46:23.031338Z","shell.execute_reply.started":"2022-11-03T17:46:12.744050Z","shell.execute_reply":"2022-11-03T17:46:23.029461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, val in enumerate(edge_list):\n    EDGE_LIST[i][2] = val","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:45:29.158613Z","iopub.execute_input":"2022-11-03T17:45:29.159025Z","iopub.status.idle":"2022-11-03T17:45:29.164900Z","shell.execute_reply.started":"2022-11-03T17:45:29.158990Z","shell.execute_reply":"2022-11-03T17:45:29.163621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = graphviz.Digraph(format='svg')\ng.attr('node', shape='circle')\ng.attr('node', style ='filled')\ng.attr('node', fillcolor='orange')\ng.attr('node', color='orange')\n\nfor node in NODE_LIST:\n    g.node(node, node)\n\nfor row in EDGE_LIST:\n    g.attr('edge', weight=str(row[2]))\n    g.edge(row[0], row[1], label=str(row[2]))","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:45:29.166372Z","iopub.execute_input":"2022-11-03T17:45:29.166768Z","iopub.status.idle":"2022-11-03T17:45:29.178741Z","shell.execute_reply.started":"2022-11-03T17:45:29.166733Z","shell.execute_reply":"2022-11-03T17:45:29.177580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g","metadata":{"execution":{"iopub.status.busy":"2022-11-03T17:45:29.180304Z","iopub.execute_input":"2022-11-03T17:45:29.181663Z","iopub.status.idle":"2022-11-03T17:45:29.328204Z","shell.execute_reply.started":"2022-11-03T17:45:29.181595Z","shell.execute_reply":"2022-11-03T17:45:29.326633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}