{"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":"# Sirintra Kunakornpaiboonsiri 6338237321\nVirada Poopipathiranyakul 6338203021\nSasicha Chotiprayanakul 6338208121","metadata":{}},{"cell_type":"code","source":"\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-30T03:54:07.819848Z","iopub.execute_input":"2022-03-30T03:54:07.820199Z","iopub.status.idle":"2022-03-30T03:54:07.845549Z","shell.execute_reply.started":"2022-03-30T03:54:07.820112Z","shell.execute_reply":"2022-03-30T03:54:07.844769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:54:14.860897Z","iopub.execute_input":"2022-03-30T03:54:14.861235Z","iopub.status.idle":"2022-03-30T03:54:14.866324Z","shell.execute_reply.started":"2022-03-30T03:54:14.861198Z","shell.execute_reply":"2022-03-30T03:54:14.865300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\ncustomers = customers[['customer_id','age','fashion_news_frequency','club_member_status']]\ncustomers.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:54:18.585942Z","iopub.execute_input":"2022-03-30T03:54:18.586460Z","iopub.status.idle":"2022-03-30T03:54:25.381742Z","shell.execute_reply.started":"2022-03-30T03:54:18.586411Z","shell.execute_reply":"2022-03-30T03:54:25.380805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntransactions.tail()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:54:30.761469Z","iopub.execute_input":"2022-03-30T03:54:30.761829Z","iopub.status.idle":"2022-03-30T03:55:42.191741Z","shell.execute_reply.started":"2022-03-30T03:54:30.761791Z","shell.execute_reply":"2022-03-30T03:55:42.190674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['year'] = pd.DatetimeIndex(transactions['t_dat']).year\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:57:19.932495Z","iopub.execute_input":"2022-03-30T03:57:19.932873Z","iopub.status.idle":"2022-03-30T03:57:32.418386Z","shell.execute_reply.started":"2022-03-30T03:57:19.932832Z","shell.execute_reply":"2022-03-30T03:57:32.417498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['month'] = pd.DatetimeIndex(transactions['t_dat']).month","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:58:41.591407Z","iopub.execute_input":"2022-03-30T03:58:41.591709Z","iopub.status.idle":"2022-03-30T03:58:54.099229Z","shell.execute_reply.started":"2022-03-30T03:58:41.591678Z","shell.execute_reply":"2022-03-30T03:58:54.098145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:59:12.046935Z","iopub.execute_input":"2022-03-30T03:59:12.047285Z","iopub.status.idle":"2022-03-30T03:59:12.063158Z","shell.execute_reply.started":"2022-03-30T03:59:12.047246Z","shell.execute_reply":"2022-03-30T03:59:12.062107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"for customer who bought at least 20 pieces in 2020","metadata":{}},{"cell_type":"code","source":"transactions.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:59:29.628514Z","iopub.execute_input":"2022-03-30T03:59:29.628843Z","iopub.status.idle":"2022-03-30T03:59:29.648049Z","shell.execute_reply.started":"2022-03-30T03:59:29.628809Z","shell.execute_reply":"2022-03-30T03:59:29.646678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_per_year = transactions[['year','customer_id']].value_counts().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:59:34.563176Z","iopub.execute_input":"2022-03-30T03:59:34.563485Z","iopub.status.idle":"2022-03-30T03:59:53.550099Z","shell.execute_reply.started":"2022-03-30T03:59:34.563453Z","shell.execute_reply":"2022-03-30T03:59:53.549190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_per_year.columns=['year', 'customer_id', 'num_of_purchase']\ntransactions_per_year","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:59:57.227418Z","iopub.execute_input":"2022-03-30T03:59:57.227735Z","iopub.status.idle":"2022-03-30T03:59:57.244072Z","shell.execute_reply.started":"2022-03-30T03:59:57.227704Z","shell.execute_reply":"2022-03-30T03:59:57.243116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"transaction for customer who bought at least 20 pieces in 2020","metadata":{}},{"cell_type":"code","source":"freq_trans=transactions_per_year[(transactions_per_year['num_of_purchase']>29)&(transactions_per_year['year']==2020)]","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:00:01.856164Z","iopub.execute_input":"2022-03-30T04:00:01.857108Z","iopub.status.idle":"2022-03-30T04:00:01.912348Z","shell.execute_reply.started":"2022-03-30T04:00:01.857060Z","shell.execute_reply":"2022-03-30T04:00:01.911492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_trans","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:00:05.061676Z","iopub.execute_input":"2022-03-30T04:00:05.061986Z","iopub.status.idle":"2022-03-30T04:00:05.077788Z","shell.execute_reply.started":"2022-03-30T04:00:05.061955Z","shell.execute_reply":"2022-03-30T04:00:05.076556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_trans_info = transactions[transactions['customer_id'].isin(freq_trans['customer_id'])]","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:00:44.730820Z","iopub.execute_input":"2022-03-30T04:00:44.731529Z","iopub.status.idle":"2022-03-30T04:00:52.494556Z","shell.execute_reply.started":"2022-03-30T04:00:44.731476Z","shell.execute_reply":"2022-03-30T04:00:52.493457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_trans_info.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:00:55.363677Z","iopub.execute_input":"2022-03-30T04:00:55.364009Z","iopub.status.idle":"2022-03-30T04:00:55.377415Z","shell.execute_reply.started":"2022-03-30T04:00:55.363975Z","shell.execute_reply":"2022-03-30T04:00:55.376600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ran_freq_trans_info = freq_trans_info.sample(n=40000)\nran_freq_trans_info","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:00.044752Z","iopub.execute_input":"2022-03-30T04:01:00.045089Z","iopub.status.idle":"2022-03-30T04:01:00.721660Z","shell.execute_reply.started":"2022-03-30T04:01:00.045052Z","shell.execute_reply":"2022-03-30T04:01:00.720682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"list of customer who bought at least 20 pieces in 2020","metadata":{}},{"cell_type":"code","source":"freq_customers=customers[customers['customer_id'].isin(freq_trans['customer_id'])]\nfreq_customers","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:09.456836Z","iopub.execute_input":"2022-03-30T04:01:09.457166Z","iopub.status.idle":"2022-03-30T04:01:10.184056Z","shell.execute_reply.started":"2022-03-30T04:01:09.457130Z","shell.execute_reply":"2022-03-30T04:01:10.183101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_customers.loc[freq_customers['fashion_news_frequency']=='NONE','fashion_news_frequency'] = float(0.0)\nfreq_customers.loc[freq_customers['fashion_news_frequency']=='Regularly','fashion_news_frequency'] = float(0.5)\nfreq_customers.loc[freq_customers['fashion_news_frequency']=='Monthly','fashion_news_frequency'] = float(1.0)\n\nfreq_customers.loc[freq_customers['club_member_status']=='PRE-CREATE','club_member_status'] = float(0.5)\nfreq_customers.loc[freq_customers['club_member_status']=='LEFT CLUB','club_member_status'] = float(0.0)\nfreq_customers.loc[freq_customers['club_member_status']=='ACTIVE','club_member_status'] = float(1.0)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:13.810235Z","iopub.execute_input":"2022-03-30T04:01:13.810620Z","iopub.status.idle":"2022-03-30T04:01:13.911367Z","shell.execute_reply.started":"2022-03-30T04:01:13.810566Z","shell.execute_reply":"2022-03-30T04:01:13.910382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:17.683633Z","iopub.execute_input":"2022-03-30T04:01:17.684336Z","iopub.status.idle":"2022-03-30T04:01:19.208310Z","shell.execute_reply.started":"2022-03-30T04:01:17.684221Z","shell.execute_reply":"2022-03-30T04:01:19.207319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = articles[['article_id','product_code','product_type_no','colour_group_code','section_no','garment_group_no']]","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:23.930067Z","iopub.execute_input":"2022-03-30T04:01:23.930373Z","iopub.status.idle":"2022-03-30T04:01:23.937819Z","shell.execute_reply.started":"2022-03-30T04:01:23.930340Z","shell.execute_reply":"2022-03-30T04:01:23.936796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['article_id'] = articles.article_id.astype('int32')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:32.394288Z","iopub.execute_input":"2022-03-30T04:01:32.394582Z","iopub.status.idle":"2022-03-30T04:01:32.401568Z","shell.execute_reply.started":"2022-03-30T04:01:32.394552Z","shell.execute_reply":"2022-03-30T04:01:32.400648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info = ran_freq_trans_info.merge(freq_customers,on='customer_id').merge(articles,on='article_id')\nfreq_info.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:35.610050Z","iopub.execute_input":"2022-03-30T04:01:35.611445Z","iopub.status.idle":"2022-03-30T04:01:35.790760Z","shell.execute_reply.started":"2022-03-30T04:01:35.611316Z","shell.execute_reply":"2022-03-30T04:01:35.789770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#run\nfreq_info_r = freq_trans_info.merge(freq_customers,on='customer_id').merge(articles,on='article_id')\nfreq_info_r","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:01:40.444022Z","iopub.execute_input":"2022-03-30T04:01:40.444379Z","iopub.status.idle":"2022-03-30T04:01:56.141765Z","shell.execute_reply.started":"2022-03-30T04:01:40.444338Z","shell.execute_reply":"2022-03-30T04:01:56.140895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info['fashion_news_frequency'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:00.317898Z","iopub.execute_input":"2022-03-30T04:02:00.318203Z","iopub.status.idle":"2022-03-30T04:02:00.333100Z","shell.execute_reply.started":"2022-03-30T04:02:00.318172Z","shell.execute_reply":"2022-03-30T04:02:00.332008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info['club_member_status'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:03.819429Z","iopub.execute_input":"2022-03-30T04:02:03.819763Z","iopub.status.idle":"2022-03-30T04:02:03.833265Z","shell.execute_reply.started":"2022-03-30T04:02:03.819724Z","shell.execute_reply":"2022-03-30T04:02:03.832506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:06.295556Z","iopub.execute_input":"2022-03-30T04:02:06.295920Z","iopub.status.idle":"2022-03-30T04:02:06.335692Z","shell.execute_reply.started":"2022-03-30T04:02:06.295873Z","shell.execute_reply":"2022-03-30T04:02:06.334722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:09.369325Z","iopub.execute_input":"2022-03-30T04:02:09.370028Z","iopub.status.idle":"2022-03-30T04:02:09.404735Z","shell.execute_reply.started":"2022-03-30T04:02:09.369967Z","shell.execute_reply":"2022-03-30T04:02:09.403541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info=freq_info.dropna()\nfreq_info","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:13.915181Z","iopub.execute_input":"2022-03-30T04:02:13.916327Z","iopub.status.idle":"2022-03-30T04:02:13.981399Z","shell.execute_reply.started":"2022-03-30T04:02:13.916212Z","shell.execute_reply":"2022-03-30T04:02:13.980626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info.loc[freq_info['fashion_news_frequency']=='NONE','fashion_news_frequency'] = float(0.0)\nfreq_info.loc[freq_info['fashion_news_frequency']=='Regularly','fashion_news_frequency'] = float(0.5)\nfreq_info.loc[freq_info['fashion_news_frequency']=='Monthly','fashion_news_frequency'] = float(1.0)\nfreq_info","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:18.944503Z","iopub.execute_input":"2022-03-30T04:02:18.944958Z","iopub.status.idle":"2022-03-30T04:02:18.989328Z","shell.execute_reply.started":"2022-03-30T04:02:18.944926Z","shell.execute_reply":"2022-03-30T04:02:18.988724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info.loc[freq_info['club_member_status']=='PRE-CREATE','club_member_status'] = float(0.5)\nfreq_info.loc[freq_info['club_member_status']=='LEFT CLUB','club_member_status'] = float(0.0)\nfreq_info.loc[freq_info['club_member_status']=='ACTIVE','club_member_status'] = float(1.0)\nfreq_info","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:24.597878Z","iopub.execute_input":"2022-03-30T04:02:24.598362Z","iopub.status.idle":"2022-03-30T04:02:24.645803Z","shell.execute_reply.started":"2022-03-30T04:02:24.598327Z","shell.execute_reply":"2022-03-30T04:02:24.644739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info['fashion_news_frequency'] = freq_info['fashion_news_frequency'].astype('float64')\nfreq_info['club_member_status'] = freq_info['club_member_status'].astype('float64')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:29.891131Z","iopub.execute_input":"2022-03-30T04:02:29.892189Z","iopub.status.idle":"2022-03-30T04:02:29.920153Z","shell.execute_reply.started":"2022-03-30T04:02:29.892129Z","shell.execute_reply":"2022-03-30T04:02:29.918904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info_r['fashion_news_frequency'] = freq_info_r['fashion_news_frequency'].astype('float64')\nfreq_info_r['club_member_status'] = freq_info_r['club_member_status'].astype('float64')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:32.613858Z","iopub.execute_input":"2022-03-30T04:02:32.614843Z","iopub.status.idle":"2022-03-30T04:02:34.054046Z","shell.execute_reply.started":"2022-03-30T04:02:32.614799Z","shell.execute_reply":"2022-03-30T04:02:34.052930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:38.062171Z","iopub.execute_input":"2022-03-30T04:02:38.062518Z","iopub.status.idle":"2022-03-30T04:02:38.082573Z","shell.execute_reply.started":"2022-03-30T04:02:38.062482Z","shell.execute_reply":"2022-03-30T04:02:38.081618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info['colour_group_code_scale'] = (freq_info['colour_group_code']-freq_info['colour_group_code'].min())/(freq_info['colour_group_code'].max()-freq_info['colour_group_code'].min())\nfreq_info['garment_group_no_scale'] = (freq_info['garment_group_no']-freq_info['garment_group_no'].min())/(freq_info['garment_group_no'].max()-freq_info['garment_group_no'].min())\nfreq_info['product_type_no_scale'] = (freq_info['product_type_no']-freq_info['product_type_no'].min())/(freq_info['product_type_no'].max()-freq_info['product_type_no'].min())\nfreq_info['month_scale'] = (freq_info['month']-freq_info['month'].min())/(freq_info['month'].max()-freq_info['month'].min())\nfreq_info['age_scale'] = (freq_info['age']-freq_info['age'].min())/(freq_info['age'].max()-freq_info['age'].min())\nfreq_info.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:41.908116Z","iopub.execute_input":"2022-03-30T04:02:41.909279Z","iopub.status.idle":"2022-03-30T04:02:41.947778Z","shell.execute_reply.started":"2022-03-30T04:02:41.909230Z","shell.execute_reply":"2022-03-30T04:02:41.946860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfreq_info_r['colour_group_code_scale'] = (freq_info_r['colour_group_code']-freq_info_r['colour_group_code'].min())/(freq_info_r['colour_group_code'].max()-freq_info_r['colour_group_code'].min())\nfreq_info_r['garment_group_no_scale'] = (freq_info_r['garment_group_no']-freq_info_r['garment_group_no'].min())/(freq_info_r['garment_group_no'].max()-freq_info_r['garment_group_no'].min())\nfreq_info_r['product_type_no_scale'] = (freq_info_r['product_type_no']-freq_info_r['product_type_no'].min())/(freq_info_r['product_type_no'].max()-freq_info_r['product_type_no'].min())\nfreq_info_r['month_scale'] = (freq_info_r['month']-freq_info_r['month'].min())/(freq_info_r['month'].max()-freq_info_r['month'].min())\nfreq_info_r['age_scale'] = (freq_info_r['age']-freq_info_r['age'].min())/(freq_info_r['age'].max()-freq_info_r['age'].min())\nfreq_info_r.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:50.693988Z","iopub.execute_input":"2022-03-30T04:02:50.694926Z","iopub.status.idle":"2022-03-30T04:02:51.325327Z","shell.execute_reply.started":"2022-03-30T04:02:50.694872Z","shell.execute_reply":"2022-03-30T04:02:51.324274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info_r_model = freq_info_r[['age_scale','colour_group_code_scale','garment_group_no_scale','fashion_news_frequency','product_type_no_scale','month_scale','club_member_status']]","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:55.594388Z","iopub.execute_input":"2022-03-30T04:02:55.594726Z","iopub.status.idle":"2022-03-30T04:02:57.390864Z","shell.execute_reply.started":"2022-03-30T04:02:55.594693Z","shell.execute_reply":"2022-03-30T04:02:57.389864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info_r=freq_info_r.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:03:01.270466Z","iopub.execute_input":"2022-03-30T04:03:01.271544Z","iopub.status.idle":"2022-03-30T04:03:08.196700Z","shell.execute_reply.started":"2022-03-30T04:03:01.271494Z","shell.execute_reply":"2022-03-30T04:03:08.195623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = freq_info[['age_scale','colour_group_code_scale','garment_group_no_scale','fashion_news_frequency','product_type_no_scale','month_scale','club_member_status']]\ny = freq_info['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:04:42.130025Z","iopub.execute_input":"2022-03-30T04:04:42.130699Z","iopub.status.idle":"2022-03-30T04:04:42.138774Z","shell.execute_reply.started":"2022-03-30T04:04:42.130648Z","shell.execute_reply":"2022-03-30T04:04:42.137816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:04:45.152929Z","iopub.execute_input":"2022-03-30T04:04:45.153236Z","iopub.status.idle":"2022-03-30T04:04:45.168716Z","shell.execute_reply.started":"2022-03-30T04:04:45.153207Z","shell.execute_reply":"2022-03-30T04:04:45.167530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the dataset into the training set and test set\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:04:47.813683Z","iopub.execute_input":"2022-03-30T04:04:47.814028Z","iopub.status.idle":"2022-03-30T04:04:47.827899Z","shell.execute_reply.started":"2022-03-30T04:04:47.813981Z","shell.execute_reply":"2022-03-30T04:04:47.826877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"KNN","metadata":{"execution":{"iopub.status.busy":"2022-03-29T08:26:36.459994Z","iopub.execute_input":"2022-03-29T08:26:36.460311Z","iopub.status.idle":"2022-03-29T08:26:36.485118Z","shell.execute_reply.started":"2022-03-29T08:26:36.460281Z","shell.execute_reply":"2022-03-29T08:26:36.484062Z"}}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:03:22.509271Z","iopub.execute_input":"2022-03-30T04:03:22.509840Z","iopub.status.idle":"2022-03-30T04:03:22.652323Z","shell.execute_reply.started":"2022-03-30T04:03:22.509783Z","shell.execute_reply":"2022-03-30T04:03:22.650950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k = 1\n#Train Model and Predict  \nneigh = KNeighborsClassifier(n_neighbors = k).fit(X_train,y_train)\nneigh","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:07:09.817985Z","iopub.execute_input":"2022-03-30T04:07:09.818319Z","iopub.status.idle":"2022-03-30T04:07:09.864303Z","shell.execute_reply.started":"2022-03-30T04:07:09.818281Z","shell.execute_reply":"2022-03-30T04:07:09.863659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat = neigh.predict(X_test)\nyhat[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:07:13.369402Z","iopub.execute_input":"2022-03-30T04:07:13.369735Z","iopub.status.idle":"2022-03-30T04:07:13.984163Z","shell.execute_reply.started":"2022-03-30T04:07:13.369699Z","shell.execute_reply":"2022-03-30T04:07:13.983144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nprint(\"Train set Accuracy: \", metrics.accuracy_score(y_train, neigh.predict(X_train)))\nprint(\"Test set Accuracy: \", metrics.accuracy_score(y_test, yhat))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:07:15.927422Z","iopub.execute_input":"2022-03-30T04:07:15.928016Z","iopub.status.idle":"2022-03-30T04:07:17.195641Z","shell.execute_reply.started":"2022-03-30T04:07:15.927958Z","shell.execute_reply":"2022-03-30T04:07:17.194742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info_r_model.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:07:36.613205Z","iopub.execute_input":"2022-03-30T04:07:36.613513Z","iopub.status.idle":"2022-03-30T04:07:37.476718Z","shell.execute_reply.started":"2022-03-30T04:07:36.613482Z","shell.execute_reply":"2022-03-30T04:07:37.475859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info_r_model = freq_info_r_model.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:07:40.906218Z","iopub.execute_input":"2022-03-30T04:07:40.906516Z","iopub.status.idle":"2022-03-30T04:07:41.637511Z","shell.execute_reply.started":"2022-03-30T04:07:40.906486Z","shell.execute_reply":"2022-03-30T04:07:41.636597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_freq = neigh.predict(freq_info_r_model)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:08:25.691866Z","iopub.execute_input":"2022-03-30T04:08:25.692198Z","iopub.status.idle":"2022-03-30T04:17:07.670310Z","shell.execute_reply.started":"2022-03-30T04:08:25.692161Z","shell.execute_reply":"2022-03-30T04:17:07.669251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_freq","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:17:11.907226Z","iopub.execute_input":"2022-03-30T04:17:11.908620Z","iopub.status.idle":"2022-03-30T04:17:11.927782Z","shell.execute_reply.started":"2022-03-30T04:17:11.908455Z","shell.execute_reply":"2022-03-30T04:17:11.925141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(result_freq)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:17:15.031413Z","iopub.execute_input":"2022-03-30T04:17:15.032104Z","iopub.status.idle":"2022-03-30T04:17:15.038969Z","shell.execute_reply.started":"2022-03-30T04:17:15.032052Z","shell.execute_reply":"2022-03-30T04:17:15.038109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info_r['article'] = result_freq.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:17:18.591990Z","iopub.execute_input":"2022-03-30T04:17:18.592273Z","iopub.status.idle":"2022-03-30T04:17:24.434916Z","shell.execute_reply.started":"2022-03-30T04:17:18.592243Z","shell.execute_reply":"2022-03-30T04:17:24.434092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info_r","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:17:35.847382Z","iopub.execute_input":"2022-03-30T04:17:35.848175Z","iopub.status.idle":"2022-03-30T04:17:46.486870Z","shell.execute_reply.started":"2022-03-30T04:17:35.848134Z","shell.execute_reply":"2022-03-30T04:17:46.485882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"re=freq_info_r.groupby('customer_id').head(12)\nre","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:21:48.749361Z","iopub.execute_input":"2022-03-30T04:21:48.749741Z","iopub.status.idle":"2022-03-30T04:21:59.474319Z","shell.execute_reply.started":"2022-03-30T04:21:48.749703Z","shell.execute_reply":"2022-03-30T04:21:59.473349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"re['customer_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:23:35.975399Z","iopub.execute_input":"2022-03-30T04:23:35.975771Z","iopub.status.idle":"2022-03-30T04:23:36.799801Z","shell.execute_reply.started":"2022-03-30T04:23:35.975732Z","shell.execute_reply":"2022-03-30T04:23:36.798965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ree=re.drop_duplicates(subset = ['customer_id','article'])\nree","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:29:05.788015Z","iopub.execute_input":"2022-03-30T04:29:05.788349Z","iopub.status.idle":"2022-03-30T04:29:07.552363Z","shell.execute_reply.started":"2022-03-30T04:29:05.788312Z","shell.execute_reply":"2022-03-30T04:29:07.551445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reee=ree[['customer_id','article']]\nreee","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:33:21.569095Z","iopub.execute_input":"2022-03-30T04:33:21.569482Z","iopub.status.idle":"2022-03-30T04:33:21.703909Z","shell.execute_reply.started":"2022-03-30T04:33:21.569440Z","shell.execute_reply":"2022-03-30T04:33:21.703122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reee['customer_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:33:34.238892Z","iopub.execute_input":"2022-03-30T04:33:34.239181Z","iopub.status.idle":"2022-03-30T04:33:34.887324Z","shell.execute_reply.started":"2022-03-30T04:33:34.239150Z","shell.execute_reply":"2022-03-30T04:33:34.886395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reee['article'] = ' 0'+ree['article'].astype('str') ","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:52:30.368729Z","iopub.execute_input":"2022-03-30T04:52:30.369032Z","iopub.status.idle":"2022-03-30T04:52:31.590695Z","shell.execute_reply.started":"2022-03-30T04:52:30.369001Z","shell.execute_reply":"2022-03-30T04:52:31.589763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = reee.groupby('customer_id').sum().reset_index()\nresult","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:52:36.286971Z","iopub.execute_input":"2022-03-30T04:52:36.287274Z","iopub.status.idle":"2022-03-30T04:52:37.611394Z","shell.execute_reply.started":"2022-03-30T04:52:36.287243Z","shell.execute_reply":"2022-03-30T04:52:37.610412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.columns = ['customer_id','prediction']","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:53:32.604652Z","iopub.execute_input":"2022-03-30T04:53:32.605051Z","iopub.status.idle":"2022-03-30T04:53:32.610766Z","shell.execute_reply.started":"2022-03-30T04:53:32.605010Z","shell.execute_reply":"2022-03-30T04:53:32.609707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:55:39.344860Z","iopub.execute_input":"2022-03-30T04:55:39.345698Z","iopub.status.idle":"2022-03-30T04:55:40.184602Z","shell.execute_reply.started":"2022-03-30T04:55:39.345647Z","shell.execute_reply":"2022-03-30T04:55:40.183423Z"},"trusted":true},"execution_count":null,"outputs":[]}]}