{"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 matplotlib.pyplot as plt\nimport pandas as pd\nimport json\nimport numpy as np\nimport warnings\nwith warnings.catch_warnings():\n    warnings.simplefilter('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:31.546257Z","iopub.execute_input":"2022-12-01T12:48:31.546953Z","iopub.status.idle":"2022-12-01T12:48:31.559243Z","shell.execute_reply.started":"2022-12-01T12:48:31.546884Z","shell.execute_reply":"2022-12-01T12:48:31.557903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/otto-recommender-system/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:31.565169Z","iopub.execute_input":"2022-12-01T12:48:31.565468Z","iopub.status.idle":"2022-12-01T12:48:38.010568Z","shell.execute_reply.started":"2022-12-01T12:48:31.565440Z","shell.execute_reply":"2022-12-01T12:48:38.009428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install jsonlines","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:38.012095Z","iopub.execute_input":"2022-12-01T12:48:38.012441Z","iopub.status.idle":"2022-12-01T12:48:50.032036Z","shell.execute_reply.started":"2022-12-01T12:48:38.012408Z","shell.execute_reply":"2022-12-01T12:48:50.030813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create empty Test DataFrame","metadata":{}},{"cell_type":"code","source":"df_test = pd.DataFrame()\nprint(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:50.035239Z","iopub.execute_input":"2022-12-01T12:48:50.035645Z","iopub.status.idle":"2022-12-01T12:48:50.052549Z","shell.execute_reply.started":"2022-12-01T12:48:50.035605Z","shell.execute_reply":"2022-12-01T12:48:50.051142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert test.jsonl into a list of nested Dictionary","metadata":{}},{"cell_type":"code","source":"with open(\"../input/otto-recommender-system/test.jsonl\",\"r\") as json_file1 :\n    json_list1 = list(json_file1)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:50.054132Z","iopub.execute_input":"2022-12-01T12:48:50.054465Z","iopub.status.idle":"2022-12-01T12:48:54.626226Z","shell.execute_reply.started":"2022-12-01T12:48:50.054434Z","shell.execute_reply":"2022-12-01T12:48:54.624953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Converting the Test list to a dataframe","metadata":{}},{"cell_type":"code","source":"n = 1\nfor json_str in json_list1 :\n    result = json.loads(json_str)\n    df2 = pd.DataFrame(result)\n    df_test= df_test.append(df2)    \n    n = n +1\n    if( n > 100) :\n        break","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:54.627873Z","iopub.execute_input":"2022-12-01T12:48:54.630701Z","iopub.status.idle":"2022-12-01T12:48:54.811022Z","shell.execute_reply.started":"2022-12-01T12:48:54.630614Z","shell.execute_reply":"2022-12-01T12:48:54.809939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:54.812204Z","iopub.execute_input":"2022-12-01T12:48:54.812507Z","iopub.status.idle":"2022-12-01T12:48:54.838440Z","shell.execute_reply.started":"2022-12-01T12:48:54.812480Z","shell.execute_reply":"2022-12-01T12:48:54.836650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### From the above output we find that the Dataframe has a third dimentsion in the Events Column which is again a Dictionary. So lets add those to the current Dataframe and new columns","metadata":{}},{"cell_type":"code","source":"df_test[\"event_aid\"] = 0\ndf_test[\"event_ts\"] = 0\ndf_test[\"event_type\"] = \"\"\nl = len(df_test)\ndf_test = df_test.set_index(np.arange(0,l,1))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:54.840372Z","iopub.execute_input":"2022-12-01T12:48:54.840892Z","iopub.status.idle":"2022-12-01T12:48:54.853296Z","shell.execute_reply.started":"2022-12-01T12:48:54.840853Z","shell.execute_reply":"2022-12-01T12:48:54.851928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,l,1) :\n    x = df_test[\"events\"][i]\n    #print(type(x.values()))\n    dv = list(x.values())\n    df_test[\"event_aid\"][i] = int(dv[0])\n    df_test[\"event_ts\"][i] = int(dv[1])\n    df_test[\"event_type\"][i] = dv[2]","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:54.854960Z","iopub.execute_input":"2022-12-01T12:48:54.855508Z","iopub.status.idle":"2022-12-01T12:48:56.112221Z","shell.execute_reply.started":"2022-12-01T12:48:54.855471Z","shell.execute_reply":"2022-12-01T12:48:56.111091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop the column events which is not needed anymore\ndf_test = df_test.drop([\"events\"], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:56.113846Z","iopub.execute_input":"2022-12-01T12:48:56.114203Z","iopub.status.idle":"2022-12-01T12:48:56.120821Z","shell.execute_reply.started":"2022-12-01T12:48:56.114171Z","shell.execute_reply":"2022-12-01T12:48:56.119574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create empty DataFrame for Train","metadata":{}},{"cell_type":"code","source":"df_train = pd.DataFrame()\nprint(df_train)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:56.122410Z","iopub.execute_input":"2022-12-01T12:48:56.122793Z","iopub.status.idle":"2022-12-01T12:48:56.135680Z","shell.execute_reply.started":"2022-12-01T12:48:56.122692Z","shell.execute_reply":"2022-12-01T12:48:56.134460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert train.jsonl into a list of nested Dictionary","metadata":{}},{"cell_type":"code","source":"with open(\"../input/otto-recommender-system/train.jsonl\",\"r\") as json_file2 :\n    json_list2 = list(json_file2)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:48:56.137202Z","iopub.execute_input":"2022-12-01T12:48:56.138000Z","iopub.status.idle":"2022-12-01T12:50:37.760263Z","shell.execute_reply.started":"2022-12-01T12:48:56.137956Z","shell.execute_reply":"2022-12-01T12:50:37.759186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Converting the Train list to a dataframe","metadata":{}},{"cell_type":"code","source":"n = 1\nfor json_str in json_list2 :\n    result = json.loads(json_str)\n    df2 = pd.DataFrame(result)\n    df_train= df_train.append(df2)    \n    n = n +1\n    if( n > 100) :\n        break","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:38.677436Z","iopub.execute_input":"2022-12-01T12:50:38.678110Z","iopub.status.idle":"2022-12-01T12:50:39.418236Z","shell.execute_reply.started":"2022-12-01T12:50:38.678062Z","shell.execute_reply":"2022-12-01T12:50:39.417129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:39.419464Z","iopub.execute_input":"2022-12-01T12:50:39.420580Z","iopub.status.idle":"2022-12-01T12:50:39.438050Z","shell.execute_reply.started":"2022-12-01T12:50:39.420542Z","shell.execute_reply":"2022-12-01T12:50:39.436795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### From the above output we find that the Dataframe has a third dimentsion in the Events Column which is again a Dictionary. So lets add those to the current Dataframe and new columns","metadata":{}},{"cell_type":"code","source":"df_train[\"event_aid\"] = 0\ndf_train[\"event_ts\"] = 0\ndf_train[\"event_type\"] = \"\"\nl = len(df_train)\ndf_train = df_train.set_index(np.arange(0,l,1))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:39.439478Z","iopub.execute_input":"2022-12-01T12:50:39.439833Z","iopub.status.idle":"2022-12-01T12:50:39.456686Z","shell.execute_reply.started":"2022-12-01T12:50:39.439801Z","shell.execute_reply":"2022-12-01T12:50:39.455400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,l,1) :\n    x = df_train[\"events\"][i]\n    #print(type(x.values()))\n    dv = list(x.values())\n    df_train[\"event_aid\"][i] = int(dv[0])\n    df_train[\"event_ts\"][i] = int(dv[1])\n    df_train[\"event_type\"][i] = dv[2]","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:39.458628Z","iopub.execute_input":"2022-12-01T12:50:39.459050Z","iopub.status.idle":"2022-12-01T12:50:48.925129Z","shell.execute_reply.started":"2022-12-01T12:50:39.458971Z","shell.execute_reply":"2022-12-01T12:50:48.924044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop the column events which is not needed anymore\ndf_train = df_train.drop([\"events\"], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:48.926416Z","iopub.execute_input":"2022-12-01T12:50:48.927431Z","iopub.status.idle":"2022-12-01T12:50:48.934668Z","shell.execute_reply.started":"2022-12-01T12:50:48.927381Z","shell.execute_reply":"2022-12-01T12:50:48.933387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Writing .jsonl to .csv file","metadata":{}},{"cell_type":"code","source":"df_test.to_csv(\"test.csv\", index = False)\ndf_train.to_csv(\"train.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:48.935962Z","iopub.execute_input":"2022-12-01T12:50:48.936276Z","iopub.status.idle":"2022-12-01T12:50:48.974146Z","shell.execute_reply.started":"2022-12-01T12:50:48.936248Z","shell.execute_reply":"2022-12-01T12:50:48.972920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EDA on TEST DATA","metadata":{}},{"cell_type":"code","source":"df = df_test\ndf.columns","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:48.975804Z","iopub.execute_input":"2022-12-01T12:50:48.976149Z","iopub.status.idle":"2022-12-01T12:50:49.253835Z","shell.execute_reply.started":"2022-12-01T12:50:48.976117Z","shell.execute_reply":"2022-12-01T12:50:49.252711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df[df['event_type'] == 'clicks']\ndf2 = df[df['event_type'] == 'carts']\ndf3 = df[df['event_type'] == 'orders']","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:49.255217Z","iopub.execute_input":"2022-12-01T12:50:49.255978Z","iopub.status.idle":"2022-12-01T12:50:49.269004Z","shell.execute_reply.started":"2022-12-01T12:50:49.255940Z","shell.execute_reply":"2022-12-01T12:50:49.267809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eda1 = df1.groupby(['session','event_aid','event_type'])['event_type'].count()\ndf_eda2 = df2.groupby(['session','event_aid','event_type'])['event_type'].count()\ndf_eda3 = df3.groupby(['session','event_aid','event_type'])['event_type'].count()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:49.270480Z","iopub.execute_input":"2022-12-01T12:50:49.271792Z","iopub.status.idle":"2022-12-01T12:50:49.290549Z","shell.execute_reply.started":"2022-12-01T12:50:49.271752Z","shell.execute_reply":"2022-12-01T12:50:49.288884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eda1 = df1.groupby(['session','event_aid','event_type'])['event_type'].count()\ndf_eda2 = df2.groupby(['session','event_aid','event_type'])['event_type'].count()\ndf_eda3 = df3.groupby(['session','event_aid','event_type'])['event_type'].count()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:49.292150Z","iopub.execute_input":"2022-12-01T12:50:49.292656Z","iopub.status.idle":"2022-12-01T12:50:49.304295Z","shell.execute_reply.started":"2022-12-01T12:50:49.292618Z","shell.execute_reply":"2022-12-01T12:50:49.303053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eda2.plot.barh()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:49.305912Z","iopub.execute_input":"2022-12-01T12:50:49.307006Z","iopub.status.idle":"2022-12-01T12:50:53.109350Z","shell.execute_reply.started":"2022-12-01T12:50:49.306959Z","shell.execute_reply":"2022-12-01T12:50:53.107968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eda3.plot.barh()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:53.111069Z","iopub.execute_input":"2022-12-01T12:50:53.111597Z","iopub.status.idle":"2022-12-01T12:50:54.608061Z","shell.execute_reply.started":"2022-12-01T12:50:53.111563Z","shell.execute_reply":"2022-12-01T12:50:54.606802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['session'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:54.609681Z","iopub.execute_input":"2022-12-01T12:50:54.610988Z","iopub.status.idle":"2022-12-01T12:50:56.203653Z","shell.execute_reply.started":"2022-12-01T12:50:54.610927Z","shell.execute_reply":"2022-12-01T12:50:56.202321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['session'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:56.205904Z","iopub.execute_input":"2022-12-01T12:50:56.206380Z","iopub.status.idle":"2022-12-01T12:50:56.694314Z","shell.execute_reply.started":"2022-12-01T12:50:56.206333Z","shell.execute_reply":"2022-12-01T12:50:56.693114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3['session'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:56.696378Z","iopub.execute_input":"2022-12-01T12:50:56.697047Z","iopub.status.idle":"2022-12-01T12:50:57.003669Z","shell.execute_reply.started":"2022-12-01T12:50:56.697012Z","shell.execute_reply":"2022-12-01T12:50:57.002470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_total = df['event_type'].count()\ncount_clicks = df1['event_type'].count()\ncount_carts = df2['event_type'].count()\ncount_orders = df3['event_type'].count()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:57.005053Z","iopub.execute_input":"2022-12-01T12:50:57.005445Z","iopub.status.idle":"2022-12-01T12:50:57.013310Z","shell.execute_reply.started":"2022-12-01T12:50:57.005411Z","shell.execute_reply":"2022-12-01T12:50:57.011624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"click_percent = 100*count_clicks/count_total\nclick_percent","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:57.015558Z","iopub.execute_input":"2022-12-01T12:50:57.016091Z","iopub.status.idle":"2022-12-01T12:50:57.029271Z","shell.execute_reply.started":"2022-12-01T12:50:57.016053Z","shell.execute_reply":"2022-12-01T12:50:57.028269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cart_percent = 100*count_carts/count_total\ncart_percent","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:57.030574Z","iopub.execute_input":"2022-12-01T12:50:57.030960Z","iopub.status.idle":"2022-12-01T12:50:57.041928Z","shell.execute_reply.started":"2022-12-01T12:50:57.030927Z","shell.execute_reply":"2022-12-01T12:50:57.040795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"orders_percent = 100*count_orders/count_total\norders_percent","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:57.043266Z","iopub.execute_input":"2022-12-01T12:50:57.043588Z","iopub.status.idle":"2022-12-01T12:50:57.058314Z","shell.execute_reply.started":"2022-12-01T12:50:57.043559Z","shell.execute_reply":"2022-12-01T12:50:57.057194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The click to cart ratio is just 11.14% of the Total event type and the Click to Orders ratio is 4.51% of the Total event.","metadata":{}},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:57.059549Z","iopub.execute_input":"2022-12-01T12:50:57.059903Z","iopub.status.idle":"2022-12-01T12:50:57.087992Z","shell.execute_reply.started":"2022-12-01T12:50:57.059871Z","shell.execute_reply":"2022-12-01T12:50:57.086620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:57.089577Z","iopub.execute_input":"2022-12-01T12:50:57.090114Z","iopub.status.idle":"2022-12-01T12:50:57.106180Z","shell.execute_reply.started":"2022-12-01T12:50:57.090066Z","shell.execute_reply":"2022-12-01T12:50:57.104611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:57.107943Z","iopub.execute_input":"2022-12-01T12:50:57.108438Z","iopub.status.idle":"2022-12-01T12:50:58.807627Z","shell.execute_reply.started":"2022-12-01T12:50:57.108390Z","shell.execute_reply":"2022-12-01T12:50:58.806719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.loc[:,['session','event_aid','event_ts']]\nX","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.808872Z","iopub.execute_input":"2022-12-01T12:50:58.809893Z","iopub.status.idle":"2022-12-01T12:50:58.826439Z","shell.execute_reply.started":"2022-12-01T12:50:58.809857Z","shell.execute_reply":"2022-12-01T12:50:58.825324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['event_type']\ny","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.828332Z","iopub.execute_input":"2022-12-01T12:50:58.829114Z","iopub.status.idle":"2022-12-01T12:50:58.838227Z","shell.execute_reply.started":"2022-12-01T12:50:58.829072Z","shell.execute_reply":"2022-12-01T12:50:58.837219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(\n             X, y, test_size = 0.2, random_state=3)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.840072Z","iopub.execute_input":"2022-12-01T12:50:58.840871Z","iopub.status.idle":"2022-12-01T12:50:58.852688Z","shell.execute_reply.started":"2022-12-01T12:50:58.840824Z","shell.execute_reply":"2022-12-01T12:50:58.851619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.854509Z","iopub.execute_input":"2022-12-01T12:50:58.855306Z","iopub.status.idle":"2022-12-01T12:50:58.864196Z","shell.execute_reply.started":"2022-12-01T12:50:58.855264Z","shell.execute_reply":"2022-12-01T12:50:58.863216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.865983Z","iopub.execute_input":"2022-12-01T12:50:58.866664Z","iopub.status.idle":"2022-12-01T12:50:58.876711Z","shell.execute_reply.started":"2022-12-01T12:50:58.866623Z","shell.execute_reply":"2022-12-01T12:50:58.875749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.886231Z","iopub.execute_input":"2022-12-01T12:50:58.887098Z","iopub.status.idle":"2022-12-01T12:50:58.893778Z","shell.execute_reply.started":"2022-12-01T12:50:58.887052Z","shell.execute_reply":"2022-12-01T12:50:58.892807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.895531Z","iopub.execute_input":"2022-12-01T12:50:58.896276Z","iopub.status.idle":"2022-12-01T12:50:58.907021Z","shell.execute_reply.started":"2022-12-01T12:50:58.896235Z","shell.execute_reply":"2022-12-01T12:50:58.905993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors=7)\nknn.fit(X_train, y_train) ","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.908799Z","iopub.execute_input":"2022-12-01T12:50:58.909574Z","iopub.status.idle":"2022-12-01T12:50:58.929090Z","shell.execute_reply.started":"2022-12-01T12:50:58.909532Z","shell.execute_reply":"2022-12-01T12:50:58.928048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict on dataset which model has not seen before\nprint(knn.predict(X_test))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.930950Z","iopub.execute_input":"2022-12-01T12:50:58.931688Z","iopub.status.idle":"2022-12-01T12:50:58.951970Z","shell.execute_reply.started":"2022-12-01T12:50:58.931645Z","shell.execute_reply":"2022-12-01T12:50:58.950875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nk_range = range(1,35)\nscores = {}\nscores_list =[]\nfor k in k_range :\n    knn = KNeighborsClassifier(n_neighbors = k)\n    knn.fit(X_train, y_train)\n    y_pred = knn.predict(X_test)\n    scores[k] = metrics.accuracy_score(y_test,y_pred)\n    scores_list.append(metrics.accuracy_score(y_test,y_pred))\nplt.plot(k_range,scores_list)\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:50:58.953976Z","iopub.execute_input":"2022-12-01T12:50:58.954309Z","iopub.status.idle":"2022-12-01T12:50:59.693469Z","shell.execute_reply.started":"2022-12-01T12:50:58.954279Z","shell.execute_reply":"2022-12-01T12:50:59.692247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  we can choose a optimal value of K as 10 and retrain the model with all the available data. And that will be our final model which is ready to make predictions.","metadata":{}},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors = 10)\nknn.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:54:23.205063Z","iopub.execute_input":"2022-12-01T12:54:23.205501Z","iopub.status.idle":"2022-12-01T12:54:23.219540Z","shell.execute_reply.started":"2022-12-01T12:54:23.205468Z","shell.execute_reply":"2022-12-01T12:54:23.218273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = {0: 'clicks', 1: 'carts', 2:'orders'}\n\nx_new = df_test.loc[:,['session','event_aid','event_ts']]\ny_predict = knn.predict(x_new)\ny_predict","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:54:30.840859Z","iopub.execute_input":"2022-12-01T12:54:30.841286Z","iopub.status.idle":"2022-12-01T12:54:30.892126Z","shell.execute_reply.started":"2022-12-01T12:54:30.841252Z","shell.execute_reply":"2022-12-01T12:54:30.890976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = len(y_predict)\nfor i in range(0,a,1) :\n    print(y_predict[i])","metadata":{"execution":{"iopub.status.busy":"2022-12-01T12:54:34.511342Z","iopub.execute_input":"2022-12-01T12:54:34.511777Z","iopub.status.idle":"2022-12-01T12:54:34.529106Z","shell.execute_reply.started":"2022-12-01T12:54:34.511725Z","shell.execute_reply":"2022-12-01T12:54:34.527597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}