{"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":"# Sasicha Chotiprayanakul 6338208121\n### Sirintra Kunakornpaiboonsiri 6338237321, Virada Poopipathiranyakul 6338203021 ","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-30T03:15:02.222308Z","iopub.execute_input":"2022-03-30T03:15:02.222610Z","iopub.status.idle":"2022-03-30T03:15:02.227968Z","shell.execute_reply.started":"2022-03-30T03:15:02.222579Z","shell.execute_reply":"2022-03-30T03:15:02.226893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:15:41.513856Z","iopub.execute_input":"2022-03-30T03:15:41.514188Z","iopub.status.idle":"2022-03-30T03:15:41.519078Z","shell.execute_reply.started":"2022-03-30T03:15:41.514153Z","shell.execute_reply":"2022-03-30T03:15:41.517969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\n# customers.columns\ncustomers = customers[['customer_id','age','fashion_news_frequency','club_member_status']]\ncustomers.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:15:45.894461Z","iopub.execute_input":"2022-03-30T03:15:45.895244Z","iopub.status.idle":"2022-03-30T03:15:52.144388Z","shell.execute_reply.started":"2022-03-30T03:15:45.895197Z","shell.execute_reply":"2022-03-30T03:15:52.143563Z"},"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.columns\n# transactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:15:55.936012Z","iopub.execute_input":"2022-03-30T03:15:55.937187Z","iopub.status.idle":"2022-03-30T03:17:05.104432Z","shell.execute_reply.started":"2022-03-30T03:15:55.937127Z","shell.execute_reply":"2022-03-30T03:17:05.103151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['year'] = pd.DatetimeIndex(transactions['t_dat']).year\ntransactions['month'] = pd.DatetimeIndex(transactions['t_dat']).month\ntransactions.tail()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T01:42:29.401679Z","iopub.execute_input":"2022-03-30T01:42:29.401943Z","iopub.status.idle":"2022-03-30T01:42:53.984139Z","shell.execute_reply.started":"2022-03-30T01:42:29.401916Z","shell.execute_reply":"2022-03-30T01:42:53.982472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\n# articles.columns\narticles = articles[['article_id','product_code','product_type_no','graphical_appearance_no','department_no','colour_group_code','section_no','garment_group_no']]\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:19:38.843772Z","iopub.execute_input":"2022-03-30T03:19:38.846537Z","iopub.status.idle":"2022-03-30T03:19:40.495352Z","shell.execute_reply.started":"2022-03-30T03:19:38.846427Z","shell.execute_reply":"2022-03-30T03:19:40.493754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Find weeks since each transaction","metadata":{}},{"cell_type":"code","source":"transactions[\"t_dat\"] = pd.to_datetime(transactions[\"t_dat\"])\ntransactions[\"week_since\"] = (transactions[\"t_dat\"].max() - transactions[\"t_dat\"]).dt.days // 7\ntransactions[\"week_since\"].value_counts()\ntransactions.tail()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:19:45.113223Z","iopub.execute_input":"2022-03-30T03:19:45.113571Z","iopub.status.idle":"2022-03-30T03:19:53.986709Z","shell.execute_reply.started":"2022-03-30T03:19:45.113539Z","shell.execute_reply":"2022-03-30T03:19:53.985693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transactions in the past month","metadata":{}},{"cell_type":"code","source":"last_month_transactions=transactions[(transactions.week_since>=0) & (transactions.week_since<=4)]\nlast_month_transactions","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:25:36.418800Z","iopub.execute_input":"2022-03-30T03:25:36.419070Z","iopub.status.idle":"2022-03-30T03:25:37.513526Z","shell.execute_reply.started":"2022-03-30T03:25:36.419044Z","shell.execute_reply":"2022-03-30T03:25:37.512446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Most popular articles in the past month","metadata":{}},{"cell_type":"code","source":"last_month_articles= last_month_transactions[\"article_id\"].value_counts().reset_index().sort_values(by='article_id',ascending=False)[0:24]\nlast_month_articles.columns= ['article_id','count']\nlast_month_articles","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:41:44.387248Z","iopub.execute_input":"2022-03-30T03:41:44.387624Z","iopub.status.idle":"2022-03-30T03:41:44.478636Z","shell.execute_reply.started":"2022-03-30T03:41:44.387586Z","shell.execute_reply":"2022-03-30T03:41:44.477436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_trans = last_month_transactions[last_month_transactions['article_id'].isin(last_month_articles['article_id'])]\nfreq_trans","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:53:43.275571Z","iopub.execute_input":"2022-03-30T03:53:43.277109Z","iopub.status.idle":"2022-03-30T03:53:43.351237Z","shell.execute_reply.started":"2022-03-30T03:53:43.277054Z","shell.execute_reply":"2022-03-30T03:53:43.350165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_customers=customers[customers['customer_id'].isin(freq_trans['customer_id'])]\nfreq_customers","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:54:21.593591Z","iopub.execute_input":"2022-03-30T03:54:21.594606Z","iopub.status.idle":"2022-03-30T03:54:22.032550Z","shell.execute_reply.started":"2022-03-30T03:54:21.594548Z","shell.execute_reply":"2022-03-30T03:54:22.031418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_customers.loc[freq_customers['fashion_news_frequency']=='NONE','fashion_news_frequency'] = int(0)\nfreq_customers.loc[freq_customers['fashion_news_frequency']=='Monthly','fashion_news_frequency'] = int(1)\nfreq_customers.loc[freq_customers['fashion_news_frequency']=='Regularly','fashion_news_frequency'] = int(2)\n\nfreq_customers.loc[freq_customers['club_member_status']=='LEFT CLUB','club_member_status'] = int(0)\nfreq_customers.loc[freq_customers['club_member_status']=='PRE-CREATE','club_member_status'] = int(1)\nfreq_customers.loc[freq_customers['club_member_status']=='ACTIVE','club_member_status'] = int(2)\n\nfreq_customers","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:57:37.665473Z","iopub.execute_input":"2022-03-30T03:57:37.665805Z","iopub.status.idle":"2022-03-30T03:57:37.733217Z","shell.execute_reply.started":"2022-03-30T03:57:37.665772Z","shell.execute_reply":"2022-03-30T03:57:37.732664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_freq_customers=customers[~customers['customer_id'].isin(freq_trans['customer_id'])]\nnot_freq_customers","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:58:51.016787Z","iopub.execute_input":"2022-03-30T03:58:51.017089Z","iopub.status.idle":"2022-03-30T03:58:51.365623Z","shell.execute_reply.started":"2022-03-30T03:58:51.017056Z","shell.execute_reply":"2022-03-30T03:58:51.364783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info = freq_trans.merge(freq_customers,on='customer_id').merge(last_month_articles,on='article_id')\nfreq_info","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:02:09.399960Z","iopub.execute_input":"2022-03-30T04:02:09.400305Z","iopub.status.idle":"2022-03-30T04:02:09.510694Z","shell.execute_reply.started":"2022-03-30T04:02:09.400271Z","shell.execute_reply":"2022-03-30T04:02:09.509721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_info=freq_info.dropna()\nfreq_info=freq_info.drop_duplicates()\nfreq_info","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:29:44.954903Z","iopub.execute_input":"2022-03-30T04:29:44.955235Z","iopub.status.idle":"2022-03-30T04:29:45.072479Z","shell.execute_reply.started":"2022-03-30T04:29:44.955203Z","shell.execute_reply":"2022-03-30T04:29:45.071509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = freq_info[['article_id','price','sales_channel_id','week_since','age','fashion_news_frequency','club_member_status']]\ny = freq_info['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:35:37.293747Z","iopub.execute_input":"2022-03-30T04:35:37.294744Z","iopub.status.idle":"2022-03-30T04:35:37.303892Z","shell.execute_reply.started":"2022-03-30T04:35:37.294697Z","shell.execute_reply":"2022-03-30T04:35:37.302719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_trainset, X_testset, y_trainset, y_testset = train_test_split(X, y, test_size=0.3, random_state=3)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:35:40.523703Z","iopub.execute_input":"2022-03-30T04:35:40.524282Z","iopub.status.idle":"2022-03-30T04:35:40.541017Z","shell.execute_reply.started":"2022-03-30T04:35:40.524246Z","shell.execute_reply":"2022-03-30T04:35:40.539768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape of X training set {}'.format(X_trainset.shape),'&',' Size of Y training set {}'.format(y_trainset.shape))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:30:28.647726Z","iopub.execute_input":"2022-03-30T04:30:28.647996Z","iopub.status.idle":"2022-03-30T04:30:28.655508Z","shell.execute_reply.started":"2022-03-30T04:30:28.647968Z","shell.execute_reply":"2022-03-30T04:30:28.654403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\narticleTree = DecisionTreeClassifier(criterion=\"entropy\", max_depth = 4)\narticleTree","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:36:27.533305Z","iopub.execute_input":"2022-03-30T04:36:27.533571Z","iopub.status.idle":"2022-03-30T04:36:27.543489Z","shell.execute_reply.started":"2022-03-30T04:36:27.533545Z","shell.execute_reply":"2022-03-30T04:36:27.542341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articleTree.fit(X_trainset,y_trainset)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:37:16.674203Z","iopub.execute_input":"2022-03-30T04:37:16.675092Z","iopub.status.idle":"2022-03-30T04:37:16.732123Z","shell.execute_reply.started":"2022-03-30T04:37:16.675013Z","shell.execute_reply":"2022-03-30T04:37:16.731132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predTree = articleTree.predict(X_testset)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:38:40.041768Z","iopub.execute_input":"2022-03-30T04:38:40.042140Z","iopub.status.idle":"2022-03-30T04:38:40.065348Z","shell.execute_reply.started":"2022-03-30T04:38:40.042089Z","shell.execute_reply":"2022-03-30T04:38:40.063944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (predTree [0:5])\nprint (y_testset [0:5])","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:38:42.644141Z","iopub.execute_input":"2022-03-30T04:38:42.644418Z","iopub.status.idle":"2022-03-30T04:38:42.651971Z","shell.execute_reply.started":"2022-03-30T04:38:42.644388Z","shell.execute_reply":"2022-03-30T04:38:42.650691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nimport matplotlib.pyplot as plt\nprint(\"DecisionTrees's Train set Accuracy: \", metrics.accuracy_score(y_trainset, articleTree.predict(X_trainset)))\nprint(\"DecisionTrees's Test set Accuracy: \", metrics.accuracy_score(y_testset, predTree))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:51:57.052305Z","iopub.execute_input":"2022-03-30T04:51:57.053456Z","iopub.status.idle":"2022-03-30T04:51:57.102497Z","shell.execute_reply.started":"2022-03-30T04:51:57.053398Z","shell.execute_reply":"2022-03-30T04:51:57.101732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install pydotplus","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:43:34.181623Z","iopub.execute_input":"2022-03-30T04:43:34.181945Z","iopub.status.idle":"2022-03-30T04:43:48.172695Z","shell.execute_reply.started":"2022-03-30T04:43:34.181915Z","shell.execute_reply":"2022-03-30T04:43:48.171299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from  io import StringIO\nimport pydotplus\nimport matplotlib.image as mpimg\nfrom sklearn import tree\n%matplotlib inline ","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:43:56.601095Z","iopub.execute_input":"2022-03-30T04:43:56.601588Z","iopub.status.idle":"2022-03-30T04:43:56.607289Z","shell.execute_reply.started":"2022-03-30T04:43:56.601552Z","shell.execute_reply":"2022-03-30T04:43:56.606675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dot_data = StringIO()\nfilename = \"articletree.png\"\nfeatureNames = freq_info.columns[0:7]\nout=tree.export_graphviz(articleTree,feature_names=featureNames, out_file=dot_data, class_names= np.unique(y_trainset), filled=True,  special_characters=True,rotate=False)  \ngraph = pydotplus.graph_from_dot_data(dot_data.getvalue())  \ngraph.write_png(filename)\nimg = mpimg.imread(filename)\nplt.figure(figsize=(100, 200))\nplt.imshow(img,interpolation='nearest')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:47:14.139555Z","iopub.execute_input":"2022-03-30T04:47:14.140275Z","iopub.status.idle":"2022-03-30T04:47:14.175598Z","shell.execute_reply.started":"2022-03-30T04:47:14.140222Z","shell.execute_reply":"2022-03-30T04:47:14.174392Z"},"trusted":true},"execution_count":null,"outputs":[]}]}