{"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 cudf\nimport cupy as cp\nimport cuml","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-16T18:23:12.093285Z","iopub.execute_input":"2022-03-16T18:23:12.093916Z","iopub.status.idle":"2022-03-16T18:23:15.955895Z","shell.execute_reply.started":"2022-03-16T18:23:12.093818Z","shell.execute_reply":"2022-03-16T18:23:15.955068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Loading dataset**","metadata":{}},{"cell_type":"code","source":"trans_train = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntrans_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:15.958650Z","iopub.execute_input":"2022-03-16T18:23:15.959196Z","iopub.status.idle":"2022-03-16T18:23:55.607838Z","shell.execute_reply.started":"2022-03-16T18:23:15.959155Z","shell.execute_reply":"2022-03-16T18:23:55.607190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cus_train= cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')\ncus_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:55.608942Z","iopub.execute_input":"2022-03-16T18:23:55.609181Z","iopub.status.idle":"2022-03-16T18:23:58.065979Z","shell.execute_reply.started":"2022-03-16T18:23:55.609145Z","shell.execute_reply":"2022-03-16T18:23:58.065187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arti_train=cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\narti_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:58.067916Z","iopub.execute_input":"2022-03-16T18:23:58.068316Z","iopub.status.idle":"2022-03-16T18:23:58.634956Z","shell.execute_reply.started":"2022-03-16T18:23:58.068279Z","shell.execute_reply":"2022-03-16T18:23:58.634148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **DATA PREPROCESSING**  \nRemoved unnecessary columns from training dataset,reducing memory consumption by storing article id as int32 and reducing the memory consumption of training dataset by storing customer id as int64 instead of string ","metadata":{}},{"cell_type":"code","source":"trans_train['customer_id'] = trans_train['customer_id'].str[-16:].str.hex_to_int().astype('int64')\ntrans_train['article_id'] = trans_train.article_id.astype('int32')\ntrans_train.t_dat = cudf.to_datetime(trans_train.t_dat)\ntrans_train = trans_train[['t_dat','customer_id','article_id']]\ntrans_train_original = trans_train\nprint( trans_train.shape )\ntrans_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:58.636411Z","iopub.execute_input":"2022-03-16T18:23:58.636882Z","iopub.status.idle":"2022-03-16T18:23:59.060746Z","shell.execute_reply.started":"2022-03-16T18:23:58.636840Z","shell.execute_reply":"2022-03-16T18:23:59.060075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cus_train = cus_train[['customer_id','age']]\ncus_train['customer_id'] = cus_train['customer_id'].str[-16:].str.hex_to_int().astype('int64')\ncus_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.061811Z","iopub.execute_input":"2022-03-16T18:23:59.062616Z","iopub.status.idle":"2022-03-16T18:23:59.095810Z","shell.execute_reply.started":"2022-03-16T18:23:59.062578Z","shell.execute_reply":"2022-03-16T18:23:59.094746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arti_train=arti_train[['article_id','product_type_no','graphical_appearance_no','colour_group_code']]\narti_train['article_id'] = arti_train.article_id.astype('int32')\narti_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.097286Z","iopub.execute_input":"2022-03-16T18:23:59.097567Z","iopub.status.idle":"2022-03-16T18:23:59.118490Z","shell.execute_reply.started":"2022-03-16T18:23:59.097534Z","shell.execute_reply":"2022-03-16T18:23:59.117889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Items purchased  within 2 weeks**","metadata":{}},{"cell_type":"code","source":"pucr = trans_train.groupby('customer_id').t_dat.max().reset_index()\npucr.columns = ['customer_id','max_dat']\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.119883Z","iopub.execute_input":"2022-03-16T18:23:59.120369Z","iopub.status.idle":"2022-03-16T18:23:59.177684Z","shell.execute_reply.started":"2022-03-16T18:23:59.120319Z","shell.execute_reply":"2022-03-16T18:23:59.177067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pucr","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.178801Z","iopub.execute_input":"2022-03-16T18:23:59.179037Z","iopub.status.idle":"2022-03-16T18:23:59.226973Z","shell.execute_reply.started":"2022-03-16T18:23:59.179005Z","shell.execute_reply":"2022-03-16T18:23:59.226352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train = trans_train.merge(pucr,on=['customer_id'],how='left')\ntrans_train['diff_dat'] = (trans_train.max_dat - trans_train.t_dat).dt.days\ntrans_train = trans_train.loc[trans_train['diff_dat']<=14]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.229872Z","iopub.execute_input":"2022-03-16T18:23:59.230056Z","iopub.status.idle":"2022-03-16T18:23:59.354318Z","shell.execute_reply.started":"2022-03-16T18:23:59.230033Z","shell.execute_reply":"2022-03-16T18:23:59.353720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train['diff_dat'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.355698Z","iopub.execute_input":"2022-03-16T18:23:59.355943Z","iopub.status.idle":"2022-03-16T18:23:59.388851Z","shell.execute_reply.started":"2022-03-16T18:23:59.355901Z","shell.execute_reply":"2022-03-16T18:23:59.388198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **sorting by most recent date**","metadata":{}},{"cell_type":"code","source":"pucr = trans_train.groupby(['customer_id','article_id'])['t_dat'].agg('count').reset_index() \npucr.columns = ['customer_id','article_id','count']\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.390009Z","iopub.execute_input":"2022-03-16T18:23:59.390231Z","iopub.status.idle":"2022-03-16T18:23:59.416727Z","shell.execute_reply.started":"2022-03-16T18:23:59.390201Z","shell.execute_reply":"2022-03-16T18:23:59.416139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pucr","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.417847Z","iopub.execute_input":"2022-03-16T18:23:59.418092Z","iopub.status.idle":"2022-03-16T18:23:59.465235Z","shell.execute_reply.started":"2022-03-16T18:23:59.418060Z","shell.execute_reply":"2022-03-16T18:23:59.464509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train = trans_train.merge(pucr,on=['customer_id','article_id'],how='left')\ntrans_train = trans_train.sort_values(['count','t_dat'],ascending=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.466289Z","iopub.execute_input":"2022-03-16T18:23:59.466605Z","iopub.status.idle":"2022-03-16T18:23:59.564212Z","shell.execute_reply.started":"2022-03-16T18:23:59.466570Z","shell.execute_reply":"2022-03-16T18:23:59.563588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.565292Z","iopub.execute_input":"2022-03-16T18:23:59.565529Z","iopub.status.idle":"2022-03-16T18:23:59.631922Z","shell.execute_reply.started":"2022-03-16T18:23:59.565498Z","shell.execute_reply":"2022-03-16T18:23:59.631291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train = trans_train.drop_duplicates(['customer_id','article_id'])\ntrans_train = trans_train.sort_values(['count','t_dat'],ascending=False)\ntrans_train= trans_train.reset_index(drop=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.632943Z","iopub.execute_input":"2022-03-16T18:23:59.633249Z","iopub.status.idle":"2022-03-16T18:23:59.773637Z","shell.execute_reply.started":"2022-03-16T18:23:59.633213Z","shell.execute_reply":"2022-03-16T18:23:59.773047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.774669Z","iopub.execute_input":"2022-03-16T18:23:59.774939Z","iopub.status.idle":"2022-03-16T18:23:59.838109Z","shell.execute_reply.started":"2022-03-16T18:23:59.774901Z","shell.execute_reply":"2022-03-16T18:23:59.837208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train=trans_train.reset_index(drop=False)\ntrans_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.839206Z","iopub.execute_input":"2022-03-16T18:23:59.839619Z","iopub.status.idle":"2022-03-16T18:23:59.907845Z","shell.execute_reply.started":"2022-03-16T18:23:59.839565Z","shell.execute_reply":"2022-03-16T18:23:59.907207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Recommendation according to age of customer**","metadata":{}},{"cell_type":"code","source":"cust_age=cudf.merge(trans_train, cus_train, on='customer_id')\ncust_age.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.908885Z","iopub.execute_input":"2022-03-16T18:23:59.909143Z","iopub.status.idle":"2022-03-16T18:23:59.950044Z","shell.execute_reply.started":"2022-03-16T18:23:59.909108Z","shell.execute_reply":"2022-03-16T18:23:59.949272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cust_age=cust_age[['index','customer_id','age','article_id']]\ncust_age=cust_age.fillna({'age':18})\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.951084Z","iopub.execute_input":"2022-03-16T18:23:59.951392Z","iopub.status.idle":"2022-03-16T18:23:59.958814Z","shell.execute_reply.started":"2022-03-16T18:23:59.951358Z","shell.execute_reply":"2022-03-16T18:23:59.958061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cust_age","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:23:59.959840Z","iopub.execute_input":"2022-03-16T18:23:59.960272Z","iopub.status.idle":"2022-03-16T18:24:00.026854Z","shell.execute_reply.started":"2022-03-16T18:23:59.960147Z","shell.execute_reply":"2022-03-16T18:24:00.026148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"art_sel = cudf.merge(cust_age, arti_train, on='article_id')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:00.031048Z","iopub.execute_input":"2022-03-16T18:24:00.032978Z","iopub.status.idle":"2022-03-16T18:24:00.053551Z","shell.execute_reply.started":"2022-03-16T18:24:00.032940Z","shell.execute_reply":"2022-03-16T18:24:00.052711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"art_sel","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:00.060856Z","iopub.execute_input":"2022-03-16T18:24:00.064294Z","iopub.status.idle":"2022-03-16T18:24:00.174902Z","shell.execute_reply.started":"2022-03-16T18:24:00.064255Z","shell.execute_reply":"2022-03-16T18:24:00.173946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output1=art_sel[['age','product_type_no','graphical_appearance_no','colour_group_code']]\noutput1.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:00.179583Z","iopub.execute_input":"2022-03-16T18:24:00.182140Z","iopub.status.idle":"2022-03-16T18:24:00.217888Z","shell.execute_reply.started":"2022-03-16T18:24:00.182095Z","shell.execute_reply":"2022-03-16T18:24:00.216993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output2=art_sel[['article_id']]\noutput2.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:00.219368Z","iopub.execute_input":"2022-03-16T18:24:00.219669Z","iopub.status.idle":"2022-03-16T18:24:00.240011Z","shell.execute_reply.started":"2022-03-16T18:24:00.219632Z","shell.execute_reply":"2022-03-16T18:24:00.239204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KNN \nTaking k value equal to 13 and using euclidean distance","metadata":{}},{"cell_type":"code","source":"from cuml.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:00.241491Z","iopub.execute_input":"2022-03-16T18:24:00.241719Z","iopub.status.idle":"2022-03-16T18:24:00.247565Z","shell.execute_reply.started":"2022-03-16T18:24:00.241687Z","shell.execute_reply":"2022-03-16T18:24:00.246860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors=13,metric='euclidean')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:00.249451Z","iopub.execute_input":"2022-03-16T18:24:00.250334Z","iopub.status.idle":"2022-03-16T18:24:00.257219Z","shell.execute_reply.started":"2022-03-16T18:24:00.250297Z","shell.execute_reply":"2022-03-16T18:24:00.256481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(output1[:75000], output2[:75000])","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:00.264457Z","iopub.execute_input":"2022-03-16T18:24:00.265331Z","iopub.status.idle":"2022-03-16T18:24:03.653555Z","shell.execute_reply.started":"2022-03-16T18:24:00.265291Z","shell.execute_reply":"2022-03-16T18:24:03.652868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans=knn.predict(output1[:50000])","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:03.654788Z","iopub.execute_input":"2022-03-16T18:24:03.655102Z","iopub.status.idle":"2022-03-16T18:24:05.701307Z","shell.execute_reply.started":"2022-03-16T18:24:03.655062Z","shell.execute_reply":"2022-03-16T18:24:05.700506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_actual = output2[:50000]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:05.703354Z","iopub.execute_input":"2022-03-16T18:24:05.703736Z","iopub.status.idle":"2022-03-16T18:24:05.709382Z","shell.execute_reply.started":"2022-03-16T18:24:05.703699Z","shell.execute_reply":"2022-03-16T18:24:05.708738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:05.710641Z","iopub.execute_input":"2022-03-16T18:24:05.710984Z","iopub.status.idle":"2022-03-16T18:24:05.717075Z","shell.execute_reply.started":"2022-03-16T18:24:05.710946Z","shell.execute_reply":"2022-03-16T18:24:05.716330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Calculating R2 sccore","metadata":{}},{"cell_type":"code","source":"print(type(y_actual))\ntype(y_actual['article_id'][0])","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:05.718491Z","iopub.execute_input":"2022-03-16T18:24:05.718745Z","iopub.status.idle":"2022-03-16T18:24:06.608428Z","shell.execute_reply.started":"2022-03-16T18:24:05.718708Z","shell.execute_reply":"2022-03-16T18:24:06.607722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1=y_actual['article_id'].values","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:06.609667Z","iopub.execute_input":"2022-03-16T18:24:06.610063Z","iopub.status.idle":"2022-03-16T18:24:06.614442Z","shell.execute_reply.started":"2022-03-16T18:24:06.610023Z","shell.execute_reply":"2022-03-16T18:24:06.613774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1=y1.astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:06.615718Z","iopub.execute_input":"2022-03-16T18:24:06.615981Z","iopub.status.idle":"2022-03-16T18:24:06.861303Z","shell.execute_reply.started":"2022-03-16T18:24:06.615947Z","shell.execute_reply":"2022-03-16T18:24:06.860500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans=ans.astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:06.862771Z","iopub.execute_input":"2022-03-16T18:24:06.863040Z","iopub.status.idle":"2022-03-16T18:24:06.867567Z","shell.execute_reply.started":"2022-03-16T18:24:06.863002Z","shell.execute_reply":"2022-03-16T18:24:06.866829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(y1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:06.869093Z","iopub.execute_input":"2022-03-16T18:24:06.869976Z","iopub.status.idle":"2022-03-16T18:24:06.879914Z","shell.execute_reply.started":"2022-03-16T18:24:06.869939Z","shell.execute_reply":"2022-03-16T18:24:06.879229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1.flatten()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:06.881172Z","iopub.execute_input":"2022-03-16T18:24:06.881453Z","iopub.status.idle":"2022-03-16T18:24:07.126110Z","shell.execute_reply.started":"2022-03-16T18:24:06.881415Z","shell.execute_reply":"2022-03-16T18:24:07.125432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1=y1.astype(cp.int_)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.127170Z","iopub.execute_input":"2022-03-16T18:24:07.127440Z","iopub.status.idle":"2022-03-16T18:24:07.366001Z","shell.execute_reply.started":"2022-03-16T18:24:07.127397Z","shell.execute_reply":"2022-03-16T18:24:07.365287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.367323Z","iopub.execute_input":"2022-03-16T18:24:07.367590Z","iopub.status.idle":"2022-03-16T18:24:07.372968Z","shell.execute_reply.started":"2022-03-16T18:24:07.367554Z","shell.execute_reply":"2022-03-16T18:24:07.372309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1.reshape(len(y1),1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.374277Z","iopub.execute_input":"2022-03-16T18:24:07.374677Z","iopub.status.idle":"2022-03-16T18:24:07.384581Z","shell.execute_reply.started":"2022-03-16T18:24:07.374642Z","shell.execute_reply":"2022-03-16T18:24:07.383882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.385755Z","iopub.execute_input":"2022-03-16T18:24:07.385985Z","iopub.status.idle":"2022-03-16T18:24:07.398459Z","shell.execute_reply.started":"2022-03-16T18:24:07.385960Z","shell.execute_reply":"2022-03-16T18:24:07.397465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans1=ans.astype(cp.int_)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.399644Z","iopub.execute_input":"2022-03-16T18:24:07.399952Z","iopub.status.idle":"2022-03-16T18:24:07.404469Z","shell.execute_reply.started":"2022-03-16T18:24:07.399917Z","shell.execute_reply":"2022-03-16T18:24:07.403745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data=np.concatenate((ans.reshape(len(ans), 1), output2[]))\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.405821Z","iopub.execute_input":"2022-03-16T18:24:07.406731Z","iopub.status.idle":"2022-03-16T18:24:07.411876Z","shell.execute_reply.started":"2022-03-16T18:24:07.406695Z","shell.execute_reply":"2022-03-16T18:24:07.411178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(ans)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.412961Z","iopub.execute_input":"2022-03-16T18:24:07.413335Z","iopub.status.idle":"2022-03-16T18:24:07.421089Z","shell.execute_reply.started":"2022-03-16T18:24:07.413300Z","shell.execute_reply":"2022-03-16T18:24:07.420334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.422591Z","iopub.execute_input":"2022-03-16T18:24:07.422851Z","iopub.status.idle":"2022-03-16T18:24:07.430765Z","shell.execute_reply.started":"2022-03-16T18:24:07.422819Z","shell.execute_reply":"2022-03-16T18:24:07.430019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(y1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.432017Z","iopub.execute_input":"2022-03-16T18:24:07.432385Z","iopub.status.idle":"2022-03-16T18:24:07.438919Z","shell.execute_reply.started":"2022-03-16T18:24:07.432289Z","shell.execute_reply":"2022-03-16T18:24:07.438133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.440445Z","iopub.execute_input":"2022-03-16T18:24:07.440975Z","iopub.status.idle":"2022-03-16T18:24:07.447051Z","shell.execute_reply.started":"2022-03-16T18:24:07.440940Z","shell.execute_reply":"2022-03-16T18:24:07.446407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score=accuracy_score(ans,y1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:07.448550Z","iopub.execute_input":"2022-03-16T18:24:07.448835Z","iopub.status.idle":"2022-03-16T18:24:07.652285Z","shell.execute_reply.started":"2022-03-16T18:24:07.448762Z","shell.execute_reply":"2022-03-16T18:24:07.651389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final=cuml.metrics.r2_score( ans, y1 )","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:24:41.844953Z","iopub.execute_input":"2022-03-16T18:24:41.845672Z","iopub.status.idle":"2022-03-16T18:24:42.094247Z","shell.execute_reply.started":"2022-03-16T18:24:41.845637Z","shell.execute_reply":"2022-03-16T18:24:42.093545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:25:20.320509Z","iopub.execute_input":"2022-03-16T18:25:20.320942Z","iopub.status.idle":"2022-03-16T18:25:20.326391Z","shell.execute_reply.started":"2022-03-16T18:25:20.320906Z","shell.execute_reply":"2022-03-16T18:25:20.325535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission File","metadata":{}},{"cell_type":"code","source":"trans_train.article_id = ' 0' + trans_train.article_id.astype('str')\ntrans_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:25:32.495375Z","iopub.execute_input":"2022-03-16T18:25:32.495811Z","iopub.status.idle":"2022-03-16T18:25:32.588134Z","shell.execute_reply.started":"2022-03-16T18:25:32.495771Z","shell.execute_reply":"2022-03-16T18:25:32.587327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_trans_train = trans_train[['customer_id','article_id']].to_pandas() \np_trans_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:25:36.972298Z","iopub.execute_input":"2022-03-16T18:25:36.972989Z","iopub.status.idle":"2022-03-16T18:25:37.399850Z","shell.execute_reply.started":"2022-03-16T18:25:36.972951Z","shell.execute_reply":"2022-03-16T18:25:37.399172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purc = p_trans_train.groupby('customer_id').sum().reset_index()\npurc.columns = ['customer_id','prediction']\ntrans_train=cudf.DataFrame(purc)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:25:40.676288Z","iopub.execute_input":"2022-03-16T18:25:40.676974Z","iopub.status.idle":"2022-03-16T18:25:42.701891Z","shell.execute_reply.started":"2022-03-16T18:25:40.676935Z","shell.execute_reply":"2022-03-16T18:25:42.701197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:25:46.331405Z","iopub.execute_input":"2022-03-16T18:25:46.332138Z","iopub.status.idle":"2022-03-16T18:25:46.383440Z","shell.execute_reply.started":"2022-03-16T18:25:46.332103Z","shell.execute_reply":"2022-03-16T18:25:46.382740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"trans_train.rename(columns={'customer_id':'customer_id_edited'},inplace=True)\ntrans_train","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:25:49.915547Z","iopub.execute_input":"2022-03-16T18:25:49.916368Z","iopub.status.idle":"2022-03-16T18:25:49.965953Z","shell.execute_reply.started":"2022-03-16T18:25:49.916299Z","shell.execute_reply":"2022-03-16T18:25:49.965255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\nsubmission = submission[['customer_id']]\nsubmission['customer_id_edited'] = submission['customer_id'].str[-16:].str.hex_to_int().astype('int64')\nsubmission = submission.merge(trans_train, on='customer_id_edited', how='left').fillna('')\ndel submission['customer_id_edited']\nsubmission\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:25:55.716024Z","iopub.execute_input":"2022-03-16T18:25:55.716309Z","iopub.status.idle":"2022-03-16T18:25:58.764014Z","shell.execute_reply.started":"2022-03-16T18:25:55.716277Z","shell.execute_reply":"2022-03-16T18:25:58.763225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.prediction = submission.prediction.str.strip()\nsubmission.prediction = submission.prediction.str[:131]\nsubmission.to_csv('submission.csv',index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T18:26:01.680285Z","iopub.execute_input":"2022-03-16T18:26:01.680589Z","iopub.status.idle":"2022-03-16T18:26:02.089058Z","shell.execute_reply.started":"2022-03-16T18:26:01.680558Z","shell.execute_reply":"2022-03-16T18:26:02.088243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}