{"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":"# 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-30T04:13:55.598711Z","iopub.execute_input":"2022-03-30T04:13:55.599606Z","iopub.status.idle":"2022-03-30T04:13:55.605735Z","shell.execute_reply.started":"2022-03-30T04:13:55.599566Z","shell.execute_reply":"2022-03-30T04:13:55.604770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:14:22.943506Z","iopub.execute_input":"2022-03-30T04:14:22.943805Z","iopub.status.idle":"2022-03-30T04:14:22.948000Z","shell.execute_reply.started":"2022-03-30T04:14:22.943774Z","shell.execute_reply":"2022-03-30T04:14:22.947146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\ntransaction = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\narticles = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:15:31.877403Z","iopub.execute_input":"2022-03-30T04:15:31.878127Z","iopub.status.idle":"2022-03-30T04:16:21.696246Z","shell.execute_reply.started":"2022-03-30T04:15:31.878093Z","shell.execute_reply":"2022-03-30T04:16:21.695245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = customers[['customer_id', 'club_member_status',\n       'fashion_news_frequency', 'age']]","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:16:25.369856Z","iopub.execute_input":"2022-03-30T04:16:25.370182Z","iopub.status.idle":"2022-03-30T04:16:25.493404Z","shell.execute_reply.started":"2022-03-30T04:16:25.370146Z","shell.execute_reply":"2022-03-30T04:16:25.492440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:16:28.836125Z","iopub.execute_input":"2022-03-30T04:16:28.836412Z","iopub.status.idle":"2022-03-30T04:16:29.221383Z","shell.execute_reply.started":"2022-03-30T04:16:28.836380Z","shell.execute_reply":"2022-03-30T04:16:29.220575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.loc[customers['fashion_news_frequency']=='NONE','fashion_news_frequency'] = float(0.0)\ncustomers.loc[customers['fashion_news_frequency']=='Regularly','fashion_news_frequency'] = float(0.5)\ncustomers.loc[customers['fashion_news_frequency']=='Monthly','fashion_news_frequency'] = float(1.0)\n\ncustomers.loc[customers['club_member_status']=='ACTIVE','club_member_status'] = float(1.0)\ncustomers.loc[customers['club_member_status']=='PRE-CREATE','club_member_status'] = float(0.5)\ncustomers.loc[customers['club_member_status']=='LEFT CLUB','club_member_status'] = float(.0)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:21:15.987179Z","iopub.execute_input":"2022-03-30T04:21:15.987449Z","iopub.status.idle":"2022-03-30T04:21:17.020608Z","shell.execute_reply.started":"2022-03-30T04:21:15.987418Z","shell.execute_reply":"2022-03-30T04:21:17.019753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = customers.dropna()\ncustomers.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:16:41.817285Z","iopub.execute_input":"2022-03-30T04:16:41.818098Z","iopub.status.idle":"2022-03-30T04:16:42.639246Z","shell.execute_reply.started":"2022-03-30T04:16:41.818033Z","shell.execute_reply":"2022-03-30T04:16:42.638234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction.columns","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:27:56.85651Z","iopub.execute_input":"2022-03-30T03:27:56.856736Z","iopub.status.idle":"2022-03-30T03:27:56.862545Z","shell.execute_reply.started":"2022-03-30T03:27:56.856708Z","shell.execute_reply":"2022-03-30T03:27:56.86179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.columns","metadata":{"execution":{"iopub.status.busy":"2022-03-30T03:27:56.863759Z","iopub.execute_input":"2022-03-30T03:27:56.863979Z","iopub.status.idle":"2022-03-30T03:27:56.874941Z","shell.execute_reply.started":"2022-03-30T03:27:56.863951Z","shell.execute_reply":"2022-03-30T03:27:56.874307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = articles[['article_id','product_type_no',\n       'graphical_appearance_no', 'perceived_colour_value_id','section_no', 'garment_group_no']]","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:17:29.929665Z","iopub.execute_input":"2022-03-30T04:17:29.929966Z","iopub.status.idle":"2022-03-30T04:17:29.944118Z","shell.execute_reply.started":"2022-03-30T04:17:29.929933Z","shell.execute_reply":"2022-03-30T04:17:29.943136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = transaction[['article_id']].value_counts().reset_index()\ncount.columns = ['article_id', 'cnt']\ncount = count.sort_values(by = 'cnt', ascending = False).head(24)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:17:31.894518Z","iopub.execute_input":"2022-03-30T04:17:31.894776Z","iopub.status.idle":"2022-03-30T04:17:33.047681Z","shell.execute_reply.started":"2022-03-30T04:17:31.894746Z","shell.execute_reply":"2022-03-30T04:17:33.046675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:39:04.822967Z","iopub.execute_input":"2022-03-30T04:39:04.823554Z","iopub.status.idle":"2022-03-30T04:39:36.591452Z","shell.execute_reply.started":"2022-03-30T04:39:04.823521Z","shell.execute_reply":"2022-03-30T04:39:36.590407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction = transaction[transaction['article_id'].isin(count['article_id'])]\n","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:40:23.214024Z","iopub.execute_input":"2022-03-30T04:40:23.214512Z","iopub.status.idle":"2022-03-30T04:40:24.718360Z","shell.execute_reply.started":"2022-03-30T04:40:23.214461Z","shell.execute_reply":"2022-03-30T04:40:24.717674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction = transaction.drop(['t_dat'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:41:50.788845Z","iopub.execute_input":"2022-03-30T04:41:50.789392Z","iopub.status.idle":"2022-03-30T04:41:50.814670Z","shell.execute_reply.started":"2022-03-30T04:41:50.789360Z","shell.execute_reply":"2022-03-30T04:41:50.813775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:42:20.415831Z","iopub.execute_input":"2022-03-30T04:42:20.416146Z","iopub.status.idle":"2022-03-30T04:42:20.733674Z","shell.execute_reply.started":"2022-03-30T04:42:20.416113Z","shell.execute_reply":"2022-03-30T04:42:20.732719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction = transaction.drop_duplicates(keep='first')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:41:05.641054Z","iopub.execute_input":"2022-03-30T04:41:05.641896Z","iopub.status.idle":"2022-03-30T04:41:05.989688Z","shell.execute_reply.started":"2022-03-30T04:41:05.641839Z","shell.execute_reply":"2022-03-30T04:41:05.989009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:42:02.008776Z","iopub.execute_input":"2022-03-30T04:42:02.009090Z","iopub.status.idle":"2022-03-30T04:42:02.104202Z","shell.execute_reply.started":"2022-03-30T04:42:02.009042Z","shell.execute_reply":"2022-03-30T04:42:02.103250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master = transaction.merge(articles, on='article_id').merge(customers, on='customer_id')\nmaster.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T04:56:34.097059Z","iopub.execute_input":"2022-03-30T04:56:34.097389Z","iopub.status.idle":"2022-03-30T04:56:35.442828Z","shell.execute_reply.started":"2022-03-30T04:56:34.097352Z","shell.execute_reply":"2022-03-30T04:56:35.441755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master_sam = master.sample(n=10000)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:18:13.901494Z","iopub.execute_input":"2022-03-30T05:18:13.902297Z","iopub.status.idle":"2022-03-30T05:18:13.918881Z","shell.execute_reply.started":"2022-03-30T05:18:13.902258Z","shell.execute_reply":"2022-03-30T05:18:13.918129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master_sam['index'] = np.arange(0, len(master_sam))\nmaster_sam.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:18:36.594827Z","iopub.execute_input":"2022-03-30T05:18:36.595228Z","iopub.status.idle":"2022-03-30T05:18:36.613540Z","shell.execute_reply.started":"2022-03-30T05:18:36.595198Z","shell.execute_reply":"2022-03-30T05:18:36.612754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master_ohe = pd.get_dummies(master_sam['article_id'])\nmaster_ohe['index'] = np.arange(0, len(master_ohe))\nmaster_ohe.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:19:37.285172Z","iopub.execute_input":"2022-03-30T05:19:37.285468Z","iopub.status.idle":"2022-03-30T05:19:37.316051Z","shell.execute_reply.started":"2022-03-30T05:19:37.285436Z","shell.execute_reply":"2022-03-30T05:19:37.315447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master_sam = master_sam.merge(master_ohe, on = 'index')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:19:42.838206Z","iopub.execute_input":"2022-03-30T05:19:42.838915Z","iopub.status.idle":"2022-03-30T05:19:42.855234Z","shell.execute_reply.started":"2022-03-30T05:19:42.838869Z","shell.execute_reply":"2022-03-30T05:19:42.854551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master_sam['fashion_news_frequency'] = master_sam['fashion_news_frequency'].astype('float64')\nmaster_sam['club_member_status'] = master_sam['club_member_status'].astype('float64')","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:37:22.708827Z","iopub.execute_input":"2022-03-30T05:37:22.709513Z","iopub.status.idle":"2022-03-30T05:37:22.716784Z","shell.execute_reply.started":"2022-03-30T05:37:22.709479Z","shell.execute_reply":"2022-03-30T05:37:22.716131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master_sam.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:37:40.173771Z","iopub.execute_input":"2022-03-30T05:37:40.174393Z","iopub.status.idle":"2022-03-30T05:37:40.193396Z","shell.execute_reply.started":"2022-03-30T05:37:40.174351Z","shell.execute_reply":"2022-03-30T05:37:40.192597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"put in model","metadata":{}},{"cell_type":"code","source":"# split X and y into training and testing sets\n\nX = master_sam.drop(['customer_id', 'article_id'], axis = 1)\ny = master_sam['article_id']\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:38:54.037166Z","iopub.execute_input":"2022-03-30T05:38:54.037602Z","iopub.status.idle":"2022-03-30T05:38:54.093049Z","shell.execute_reply.started":"2022-03-30T05:38:54.037571Z","shell.execute_reply":"2022-03-30T05:38:54.092405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import DecisionTreeClassifier\n\nfrom sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:40:22.184656Z","iopub.execute_input":"2022-03-30T05:40:22.185250Z","iopub.status.idle":"2022-03-30T05:40:22.335244Z","shell.execute_reply.started":"2022-03-30T05:40:22.185193Z","shell.execute_reply":"2022-03-30T05:40:22.334360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# instantiate the DecisionTreeClassifier model with criterion gini index\n\nclf_gini = DecisionTreeClassifier(criterion='gini', max_depth=3, random_state=0)\n\n\n# fit the model\nclf_gini.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:40:32.497257Z","iopub.execute_input":"2022-03-30T05:40:32.497554Z","iopub.status.idle":"2022-03-30T05:40:32.521027Z","shell.execute_reply.started":"2022-03-30T05:40:32.497519Z","shell.execute_reply":"2022-03-30T05:40:32.520322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_gini = clf_gini.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:40:47.277583Z","iopub.execute_input":"2022-03-30T05:40:47.277869Z","iopub.status.idle":"2022-03-30T05:40:47.284894Z","shell.execute_reply.started":"2022-03-30T05:40:47.277839Z","shell.execute_reply":"2022-03-30T05:40:47.284235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_train_gini = clf_gini.predict(X_train)\n\ny_pred_train_gini","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:41:53.761325Z","iopub.execute_input":"2022-03-30T05:41:53.761615Z","iopub.status.idle":"2022-03-30T05:41:53.772636Z","shell.execute_reply.started":"2022-03-30T05:41:53.761583Z","shell.execute_reply":"2022-03-30T05:41:53.771610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nprint('Model accuracy score with criterion gini index: {0:0.4f}'. format(accuracy_score(y_test, y_pred_gini)))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:41:03.957579Z","iopub.execute_input":"2022-03-30T05:41:03.957993Z","iopub.status.idle":"2022-03-30T05:41:03.964014Z","shell.execute_reply.started":"2022-03-30T05:41:03.957958Z","shell.execute_reply":"2022-03-30T05:41:03.963295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training-set accuracy score: {0:0.4f}'. format(accuracy_score(y_train, y_pred_train_gini)))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:42:04.028924Z","iopub.execute_input":"2022-03-30T05:42:04.029236Z","iopub.status.idle":"2022-03-30T05:42:04.035682Z","shell.execute_reply.started":"2022-03-30T05:42:04.029203Z","shell.execute_reply":"2022-03-30T05:42:04.034723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print the scores on training and test set\n\nprint('Training set score: {:.4f}'.format(clf_gini.score(X_train, y_train)))\n\nprint('Test set score: {:.4f}'.format(clf_gini.score(X_test, y_test)))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T05:42:25.323498Z","iopub.execute_input":"2022-03-30T05:42:25.323772Z","iopub.status.idle":"2022-03-30T05:42:25.337045Z","shell.execute_reply.started":"2022-03-30T05:42:25.323742Z","shell.execute_reply":"2022-03-30T05:42:25.336446Z"},"trusted":true},"execution_count":null,"outputs":[]}]}