{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\nfrom scipy.optimize import curve_fit","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-04T12:40:30.561228Z","iopub.execute_input":"2022-03-04T12:40:30.562144Z","iopub.status.idle":"2022-03-04T12:40:30.588824Z","shell.execute_reply.started":"2022-03-04T12:40:30.562059Z","shell.execute_reply":"2022-03-04T12:40:30.588227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\n    '../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv',\n    usecols=['t_dat', 'customer_id', 'article_id'])\n\ndf['t_dat'] = pd.to_datetime(df['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-03-04T12:40:30.590397Z","iopub.execute_input":"2022-03-04T12:40:30.590768Z","iopub.status.idle":"2022-03-04T12:41:32.857408Z","shell.execute_reply.started":"2022-03-04T12:40:30.590739Z","shell.execute_reply":"2022-03-04T12:41:32.856472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gb = df.groupby(['customer_id', 'article_id'])['t_dat'].nunique()\ninx = gb[gb>1].index","metadata":{"execution":{"iopub.status.busy":"2022-03-04T12:41:32.858478Z","iopub.execute_input":"2022-03-04T12:41:32.858656Z","iopub.status.idle":"2022-03-04T12:41:55.11125Z","shell.execute_reply.started":"2022-03-04T12:41:32.858634Z","shell.execute_reply":"2022-03-04T12:41:55.110542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.set_index(['customer_id', 'article_id'])\ndf = df.loc[inx].copy().sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-03-04T12:41:55.11223Z","iopub.execute_input":"2022-03-04T12:41:55.112871Z","iopub.status.idle":"2022-03-04T12:42:33.001273Z","shell.execute_reply.started":"2022-03-04T12:41:55.112838Z","shell.execute_reply":"2022-03-04T12:42:33.000183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dist = []\n\nfor i in tqdm(inx):\n    t_prev = None\n    for t_dat in df.loc[i, 't_dat']:\n        if t_prev is not None:\n            dist.append((t_dat - t_prev).days)\n        t_prev = t_dat","metadata":{"execution":{"iopub.status.busy":"2022-03-04T12:42:33.003648Z","iopub.execute_input":"2022-03-04T12:42:33.003943Z","iopub.status.idle":"2022-03-04T12:42:53.005718Z","shell.execute_reply.started":"2022-03-04T12:42:33.00391Z","shell.execute_reply":"2022-03-04T12:42:53.004849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc = pd.Series(dist).value_counts()\nvc = vc[1:]","metadata":{"execution":{"iopub.status.busy":"2022-03-04T12:42:53.00666Z","iopub.status.idle":"2022-03-04T12:42:53.00701Z","shell.execute_reply.started":"2022-03-04T12:42:53.006874Z","shell.execute_reply":"2022-03-04T12:42:53.006889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def func(x, a, b, c, d):\n    return a / np.sqrt(x) + b * np.exp(-c*x) + d\n\npopt, pcov = curve_fit(func, vc.index, vc.values)\n\nplt.plot(vc.index[:100], vc.values[:100], 'b-', label='data')\nplt.plot(vc.index[:100], func(vc.index[:100], *popt), 'r-',\n         label='fit: a=%5.3f, b=%5.3f, c=%5.3f, d=%5.3f' % tuple(popt))\n\nplt.xlabel('x')\nplt.ylabel('y')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-04T12:42:53.007628Z","iopub.status.idle":"2022-03-04T12:42:53.008508Z","shell.execute_reply.started":"2022-03-04T12:42:53.008289Z","shell.execute_reply":"2022-03-04T12:42:53.008356Z"},"trusted":true},"execution_count":null,"outputs":[]}]}