{"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":"import os\nfor dirname, _, filenames in os.walk('/kaggle/working'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:47:45.879040Z","iopub.execute_input":"2022-05-29T18:47:45.879441Z","iopub.status.idle":"2022-05-29T18:47:45.886492Z","shell.execute_reply.started":"2022-05-29T18:47:45.879411Z","shell.execute_reply":"2022-05-29T18:47:45.885257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install cudf","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport cudf\nimport time\nprint(f\"Pandas: {pd.__version__}, Cudf: {cudf.__version__}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:13:22.209729Z","iopub.execute_input":"2022-05-29T18:13:22.210609Z","iopub.status.idle":"2022-05-29T18:13:27.059701Z","shell.execute_reply.started":"2022-05-29T18:13:22.210537Z","shell.execute_reply":"2022-05-29T18:13:27.058548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#LOADING\nstarttime = time.time()\ndf_pandas = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntime_pandas = time.time() - starttime\nprint(f\"Time to load: {time_pandas:.2f}s\")\n\nprint(df_pandas.shape)\ndf_pandas.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:13:30.7998Z","iopub.execute_input":"2022-05-29T18:13:30.800514Z","iopub.status.idle":"2022-05-29T18:14:33.688515Z","shell.execute_reply.started":"2022-05-29T18:13:30.800481Z","shell.execute_reply":"2022-05-29T18:14:33.687156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#LOADING\nstarttime = time.time()\ndf_cudf = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntime_cudf = time.time() - starttime\nprint(f\"Time to load: {time_cudf:.2f}s\")\n\nprint(df_cudf.shape)\ndf_cudf.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:14:37.899706Z","iopub.execute_input":"2022-05-29T18:14:37.90045Z","iopub.status.idle":"2022-05-29T18:14:42.677535Z","shell.execute_reply.started":"2022-05-29T18:14:37.900414Z","shell.execute_reply":"2022-05-29T18:14:42.67589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GROUPBY AND AGGREGATION\nstarttime = time.time()\nagg_pandas = df_pandas.groupby('customer_id')[['price', 'sales_channel_id']].agg(['mean', 'min', 'max', 'std'])\ntime_pandas = time.time() - starttime\nprint(f\"Time to aggregate: {time_pandas:.2f}s\")","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:14:48.319792Z","iopub.execute_input":"2022-05-29T18:14:48.320606Z","iopub.status.idle":"2022-05-29T18:15:06.510671Z","shell.execute_reply.started":"2022-05-29T18:14:48.320568Z","shell.execute_reply":"2022-05-29T18:15:06.50955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GROUPBY AND AGGREGATION\nstarttime = time.time()\nagg_cudf = df_cudf.groupby('customer_id')[['price', 'sales_channel_id']].agg(['mean', 'min', 'max', 'std'])\ntime_cudf = time.time() - starttime\nprint(f\"Time to aggregate: {time_cudf:.2f}s\")","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:15:14.243218Z","iopub.execute_input":"2022-05-29T18:15:14.243653Z","iopub.status.idle":"2022-05-29T18:15:14.843335Z","shell.execute_reply.started":"2022-05-29T18:15:14.243623Z","shell.execute_reply":"2022-05-29T18:15:14.842122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#SORTING\nstarttime = time.time()\ndf_pandas = df_pandas.sort_values('t_dat', ignore_index=True)\ntime_pandas = time.time() - starttime\nprint(f\"Time to sort: {time_pandas:.2f}s\")","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:15:18.319548Z","iopub.execute_input":"2022-05-29T18:15:18.32028Z","iopub.status.idle":"2022-05-29T18:16:02.526109Z","shell.execute_reply.started":"2022-05-29T18:15:18.320242Z","shell.execute_reply":"2022-05-29T18:16:02.524897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#SORTING\nstarttime = time.time()\ndf_cudf = df_cudf.sort_values('t_dat', ignore_index=True)\ntime_cudf = time.time() - starttime\nprint(f\"Time to sort: {time_cudf:.2f}s\")","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:16:06.569605Z","iopub.execute_input":"2022-05-29T18:16:06.570353Z","iopub.status.idle":"2022-05-29T18:16:07.384654Z","shell.execute_reply.started":"2022-05-29T18:16:06.570311Z","shell.execute_reply":"2022-05-29T18:16:07.38352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GAUSSRANK\nimport numpy as np\nimport cupy as cp\nimport matplotlib.pyplot as plt\nfrom scipy.special import erfinv as sp_erfinv\nfrom cupyx.scipy.special import erfinv","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:28:20.459466Z","iopub.execute_input":"2022-05-29T18:28:20.46029Z","iopub.status.idle":"2022-05-29T18:28:20.466945Z","shell.execute_reply.started":"2022-05-29T18:28:20.460218Z","shell.execute_reply":"2022-05-29T18:28:20.464994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GAUSSRANK\ndef gaussrank_cpu(data, epsilon = 1e-6):\n    r_cpu = data.argsort().argsort()\n    r_cpu = (r_cpu/r_cpu.max()-0.5)*2 # scale to (-1,1)\n    r_cpu = np.clip(r_cpu,-1+epsilon,1-epsilon)\n    r_cpu = sp_erfinv(r_cpu)\n    return(r_cpu)\n\ndef gaussrank_gpu(data, epsilon = 1e-6):\n    r_gpu = data.argsort().argsort()\n    r_gpu = (r_gpu/r_gpu.max()-0.5)*2 # scale to (-1,1)\n    r_gpu = cp.clip(r_gpu,-1+epsilon,1-epsilon)\n    r_gpu = erfinv(r_gpu)\n    return(r_gpu)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:22:24.619421Z","iopub.execute_input":"2022-05-29T18:22:24.619803Z","iopub.status.idle":"2022-05-29T18:22:24.629282Z","shell.execute_reply.started":"2022-05-29T18:22:24.619772Z","shell.execute_reply":"2022-05-29T18:22:24.628173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GAUSSRANK\ndata_cpu = df_pandas['price']\ndata_gpu = df_cudf['price'].values","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:22:29.139495Z","iopub.execute_input":"2022-05-29T18:22:29.13989Z","iopub.status.idle":"2022-05-29T18:22:29.147616Z","shell.execute_reply.started":"2022-05-29T18:22:29.139828Z","shell.execute_reply":"2022-05-29T18:22:29.146146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\ngaussrank_cpu(data_cpu)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:22:41.221441Z","iopub.execute_input":"2022-05-29T18:22:41.221885Z","iopub.status.idle":"2022-05-29T18:22:52.010984Z","shell.execute_reply.started":"2022-05-29T18:22:41.221832Z","shell.execute_reply":"2022-05-29T18:22:52.009922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\ngaussrank_gpu(data_gpu)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T18:23:03.640223Z","iopub.execute_input":"2022-05-29T18:23:03.64062Z","iopub.status.idle":"2022-05-29T18:23:03.906367Z","shell.execute_reply.started":"2022-05-29T18:23:03.640591Z","shell.execute_reply":"2022-05-29T18:23:03.905163Z"},"trusted":true},"execution_count":null,"outputs":[]}]}