{"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\nimport os\nfor 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-04-04T00:09:25.222638Z","iopub.execute_input":"2022-04-04T00:09:25.223223Z","iopub.status.idle":"2022-04-04T00:10:55.531157Z","shell.execute_reply.started":"2022-04-04T00:09:25.223181Z","shell.execute_reply":"2022-04-04T00:10:55.529794Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import dependencies and load data","metadata":{}},{"cell_type":"code","source":"import gc\nimport cudf\nimport cupy\nimport cuml","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:02:07.223365Z","iopub.execute_input":"2022-04-17T18:02:07.223619Z","iopub.status.idle":"2022-04-17T18:02:07.227538Z","shell.execute_reply.started":"2022-04-17T18:02:07.223586Z","shell.execute_reply":"2022-04-17T18:02:07.226719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom PIL import Image\nfrom tqdm import tqdm\nfrom datetime import datetime\n\npd.set_option('display.max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:02:09.844135Z","iopub.execute_input":"2022-04-17T18:02:09.844682Z","iopub.status.idle":"2022-04-17T18:02:09.906925Z","shell.execute_reply.started":"2022-04-17T18:02:09.844643Z","shell.execute_reply":"2022-04-17T18:02:09.906193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\nprint(sample_submission.shape)\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:02:11.021199Z","iopub.execute_input":"2022-04-17T18:02:11.021504Z","iopub.status.idle":"2022-04-17T18:02:15.392258Z","shell.execute_reply.started":"2022-04-17T18:02:11.021471Z","shell.execute_reply":"2022-04-17T18:02:15.391056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\nprint(articles.shape)\narticles.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:02:15.393993Z","iopub.execute_input":"2022-04-17T18:02:15.394722Z","iopub.status.idle":"2022-04-17T18:02:16.227945Z","shell.execute_reply.started":"2022-04-17T18:02:15.394682Z","shell.execute_reply":"2022-04-17T18:02:16.227171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\nprint(customers.shape)\ncustomers.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:02:16.229245Z","iopub.execute_input":"2022-04-17T18:02:16.229774Z","iopub.status.idle":"2022-04-17T18:02:20.735455Z","shell.execute_reply.started":"2022-04-17T18:02:16.229734Z","shell.execute_reply":"2022-04-17T18:02:20.734718Z"},"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')\nprint(transactions.shape)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:02:20.737625Z","iopub.execute_input":"2022-04-17T18:02:20.737914Z","iopub.status.idle":"2022-04-17T18:03:17.959110Z","shell.execute_reply.started":"2022-04-17T18:02:20.737875Z","shell.execute_reply":"2022-04-17T18:03:17.958452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = Image.open('/kaggle/input/h-and-m-personalized-fashion-recommendations/images/073/0736404001.jpg')\nplt.axis('off')\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:17.962823Z","iopub.execute_input":"2022-04-17T18:03:17.964705Z","iopub.status.idle":"2022-04-17T18:03:18.330740Z","shell.execute_reply.started":"2022-04-17T18:03:17.964665Z","shell.execute_reply":"2022-04-17T18:03:18.330055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EDA","metadata":{}},{"cell_type":"code","source":"def plot_data(data, column):\n    \"\"\"\n    Function that takes in dataset and column as input and plots \n    frequency bar chart and pie chart for the same\n    \"\"\"\n    counts = dict(data[column].value_counts())\n    print(data[column].value_counts())\n    print()\n\n    figure = plt.figure(figsize=(11,5))\n\n    # bar chart\n    plt.subplot(1,2,1)\n    ax = sns.countplot(x = data[column])\n    ax.set_xticklabels(ax.get_xticklabels(),rotation = 90)\n\n    # pie chart\n    plt.subplot(1,2,2)\n    plt.pie(x=counts.values(), labels=counts.keys())\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:18.331842Z","iopub.execute_input":"2022-04-17T18:03:18.332402Z","iopub.status.idle":"2022-04-17T18:03:18.340118Z","shell.execute_reply.started":"2022-04-17T18:03:18.332360Z","shell.execute_reply":"2022-04-17T18:03:18.339354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Articles","metadata":{}},{"cell_type":"markdown","source":"This table contains all h&m articles with details such as a type of product, a color, a product group and other features.\nArticle data description:\n\n- __article_id__ : A unique identifier of every article.\n- __product_code__, __prod_name__ : A unique identifier of every product and its name (not the same).\n- __product_type__, __product_type_name__ : The group of product_code and its name\n- __graphical_appearance_no__, __graphical_appearance_name__ : The group of graphics and its name\n- __colour_group_code__, __colour_group_name__ : The group of color and its name\n- __perceived_colour_value_id__, __perceived_colour_value_name__, __perceived_colour_master_id__, __perceived_colour_master_name__ : The added color info\n- __department_no__, __department_name__: : A unique identifier of every dep and its name\n- __index_code__, __index_name__: : A unique identifier of every index and its name\n- __index_group_no__, __index_group_name__: : A group of indeces and its name\n- __section_no__, __section_name__: : A unique identifier of every section and its name\n- __garment_group_no__, __garment_group_name__: : A unique identifier of every garment and its name\n- __detail_desc__: : Details","metadata":{}},{"cell_type":"code","source":"print(articles.shape)\narticles.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:18.341443Z","iopub.execute_input":"2022-04-17T18:03:18.341689Z","iopub.status.idle":"2022-04-17T18:03:18.365974Z","shell.execute_reply.started":"2022-04-17T18:03:18.341655Z","shell.execute_reply":"2022-04-17T18:03:18.365191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dtypes\narticles.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:18.367261Z","iopub.execute_input":"2022-04-17T18:03:18.367499Z","iopub.status.idle":"2022-04-17T18:03:18.375844Z","shell.execute_reply.started":"2022-04-17T18:03:18.367466Z","shell.execute_reply":"2022-04-17T18:03:18.374996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# distribution of articles accoridng to index group\n\nplot_data(data=articles, column='index_group_name')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:18.377156Z","iopub.execute_input":"2022-04-17T18:03:18.377653Z","iopub.status.idle":"2022-04-17T18:03:18.912863Z","shell.execute_reply.started":"2022-04-17T18:03:18.377619Z","shell.execute_reply":"2022-04-17T18:03:18.912141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We see that the 'Ladieswear' and 'Baby/Children' dominate in the index groups.","metadata":{}},{"cell_type":"code","source":"# number of article for each garment group\n\nplot_data(data=articles, column='garment_group_name')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:18.916109Z","iopub.execute_input":"2022-04-17T18:03:18.916548Z","iopub.status.idle":"2022-04-17T18:03:19.474147Z","shell.execute_reply.started":"2022-04-17T18:03:18.916496Z","shell.execute_reply":"2022-04-17T18:03:19.473485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We see that maximum number of article belong to 'Jersy Fancy' garment group.","metadata":{}},{"cell_type":"code","source":"# distribution of articles according to perceived colour\n\nplot_data(data=articles, column='perceived_colour_master_name')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:19.475066Z","iopub.execute_input":"2022-04-17T18:03:19.475348Z","iopub.status.idle":"2022-04-17T18:03:20.009774Z","shell.execute_reply.started":"2022-04-17T18:03:19.475294Z","shell.execute_reply":"2022-04-17T18:03:20.009094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The majority of articles are in 'White', 'Blue' or 'Black' colour.","metadata":{}},{"cell_type":"code","source":"# distribution of perceived color shade\n\nplot_data(data=articles, column='perceived_colour_value_name')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:20.011098Z","iopub.execute_input":"2022-04-17T18:03:20.011352Z","iopub.status.idle":"2022-04-17T18:03:20.408260Z","shell.execute_reply.started":"2022-04-17T18:03:20.011306Z","shell.execute_reply":"2022-04-17T18:03:20.407620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Majority of articles are 'Dusty Light' or 'Dark' in shade.","metadata":{}},{"cell_type":"code","source":"# distribution of article graphical appearance\n\nplot_data(data=articles, column='graphical_appearance_name')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:20.409632Z","iopub.execute_input":"2022-04-17T18:03:20.410105Z","iopub.status.idle":"2022-04-17T18:03:21.186337Z","shell.execute_reply.started":"2022-04-17T18:03:20.410068Z","shell.execute_reply":"2022-04-17T18:03:21.185699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Majority of the articles have a 'Solid' graphical apperance.","metadata":{}},{"cell_type":"code","source":"# distribution of product group\n\nplot_data(data=articles, column='product_group_name')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:21.187753Z","iopub.execute_input":"2022-04-17T18:03:21.188244Z","iopub.status.idle":"2022-04-17T18:03:21.726295Z","shell.execute_reply.started":"2022-04-17T18:03:21.188206Z","shell.execute_reply":"2022-04-17T18:03:21.725640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most of the articles are of 'Garment' product group.","metadata":{}},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:21.727493Z","iopub.execute_input":"2022-04-17T18:03:21.728209Z","iopub.status.idle":"2022-04-17T18:03:21.749490Z","shell.execute_reply.started":"2022-04-17T18:03:21.728169Z","shell.execute_reply":"2022-04-17T18:03:21.748688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot article images and their descriptions\n\nfig, ax = plt.subplots(1,5, figsize=(20,10))\ni = 0\n\nfor row in articles.tail(5).iterrows():\n    article_id = row[1]['article_id']\n    prod_name = row[1]['prod_name']\n    desc = row[1]['detail_desc']\n    desc = desc.split(\" \")\n    for j in range(len(desc)):\n        if j%5==0 and j!=0:\n            desc[j] += '\\n'\n#             desc.insert(j+1, '\\n')\n    desc = \" \".join(desc)\n    image = Image.open(f'/kaggle/input/h-and-m-personalized-fashion-recommendations/images/0{str(article_id)[0:2]}/0{article_id}.jpg')\n    ax[i].imshow(image)\n    ax[i].set_title(prod_name)\n    ax[i].set_xticks([], [])\n    ax[i].set_yticks([], [])\n    ax[i].grid(False)\n    ax[i].set_xlabel(desc, fontsize=8)\n    i += 1","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:21.750720Z","iopub.execute_input":"2022-04-17T18:03:21.750995Z","iopub.status.idle":"2022-04-17T18:03:23.549292Z","shell.execute_reply.started":"2022-04-17T18:03:21.750957Z","shell.execute_reply":"2022-04-17T18:03:23.548677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Customers","metadata":{}},{"cell_type":"markdown","source":"Customers data description:\n\n- __customer_id__ : A unique identifier of every customer\n- __FN__ : Subsribed to Fashion news (1 or missed)\n- __Active__ : 1 or missed\n- __club_member_status__ : Status in club\n- __fashion_news_frequency__ : How often H&M may send news to customer\n- __age__ : The current age\n- __postal_code__ : Postal code of customer","metadata":{}},{"cell_type":"code","source":"print(customers.shape)\ncustomers.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:23.550471Z","iopub.execute_input":"2022-04-17T18:03:23.551034Z","iopub.status.idle":"2022-04-17T18:03:23.566661Z","shell.execute_reply.started":"2022-04-17T18:03:23.550995Z","shell.execute_reply":"2022-04-17T18:03:23.565864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.kdeplot(customers['age'], shade=True)\nplt.title('Customer Age Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:23.568133Z","iopub.execute_input":"2022-04-17T18:03:23.568386Z","iopub.status.idle":"2022-04-17T18:03:28.342946Z","shell.execute_reply.started":"2022-04-17T18:03:23.568352Z","shell.execute_reply":"2022-04-17T18:03:28.342271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The Age of the customers is right skewed and the majority of customers are of the age 18-30 years old.","metadata":{}},{"cell_type":"code","source":"# Distrubution of customers\n\nplot_data(data=customers, column='club_member_status')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:28.344093Z","iopub.execute_input":"2022-04-17T18:03:28.344652Z","iopub.status.idle":"2022-04-17T18:03:30.319008Z","shell.execute_reply.started":"2022-04-17T18:03:28.344611Z","shell.execute_reply":"2022-04-17T18:03:30.318340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The majority of customers are an 'ACTIVE' club member.","metadata":{}},{"cell_type":"markdown","source":"### Transactions","metadata":{}},{"cell_type":"markdown","source":"Transactions data description:\n\n- __t_dat__ : A unique identifier of every customer\n- __customer_id__ : A unique identifier of every customer (in customers table)\n- __article_id__ : A unique identifier of every article (in articles table)\n- __price__ : Price of purchase\n- __sales_channel_id__ : 1 or 2","metadata":{}},{"cell_type":"code","source":"print(transactions.shape)\ntransactions.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:30.320280Z","iopub.execute_input":"2022-04-17T18:03:30.320539Z","iopub.status.idle":"2022-04-17T18:03:30.331271Z","shell.execute_reply.started":"2022-04-17T18:03:30.320504Z","shell.execute_reply":"2022-04-17T18:03:30.330585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_data(data=transactions, column='sales_channel_id')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:30.332618Z","iopub.execute_input":"2022-04-17T18:03:30.334017Z","iopub.status.idle":"2022-04-17T18:03:33.207405Z","shell.execute_reply.started":"2022-04-17T18:03:30.333975Z","shell.execute_reply":"2022-04-17T18:03:33.206662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Majority of of sales are from channel 2.","metadata":{}},{"cell_type":"markdown","source":"## Content-based recommendation system ","metadata":{}},{"cell_type":"code","source":"from cuml.feature_extraction.text import TfidfVectorizer # TF-IDF vectorizer\nfrom cuml.metrics.pairwise_distances import pairwise_distances # cosine similairity","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:33.208660Z","iopub.execute_input":"2022-04-17T18:03:33.208916Z","iopub.status.idle":"2022-04-17T18:03:33.219558Z","shell.execute_reply.started":"2022-04-17T18:03:33.208882Z","shell.execute_reply":"2022-04-17T18:03:33.218956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = cudf.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\nprint(articles.shape)\narticles.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:33.220610Z","iopub.execute_input":"2022-04-17T18:03:33.220929Z","iopub.status.idle":"2022-04-17T18:03:36.437217Z","shell.execute_reply.started":"2022-04-17T18:03:33.220892Z","shell.execute_reply":"2022-04-17T18:03:36.436502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = cudf.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\nprint(customers.shape)\ncustomers.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:36.438454Z","iopub.execute_input":"2022-04-17T18:03:36.439162Z","iopub.status.idle":"2022-04-17T18:03:36.652837Z","shell.execute_reply.started":"2022-04-17T18:03:36.439123Z","shell.execute_reply":"2022-04-17T18:03:36.652127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = cudf.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\nprint(transactions.shape)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:36.654069Z","iopub.execute_input":"2022-04-17T18:03:36.654769Z","iopub.status.idle":"2022-04-17T18:03:39.227338Z","shell.execute_reply.started":"2022-04-17T18:03:36.654732Z","shell.execute_reply":"2022-04-17T18:03:39.226644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = cudf.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\nprint(sample_submission.shape)\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:39.228703Z","iopub.execute_input":"2022-04-17T18:03:39.228968Z","iopub.status.idle":"2022-04-17T18:03:39.449320Z","shell.execute_reply.started":"2022-04-17T18:03:39.228931Z","shell.execute_reply":"2022-04-17T18:03:39.448601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def product_similarity_scores(article_id, index_group):\n#     # creating a subset of dataset accoriding to article index group\n#     index_group = articles.loc[articles['article_id']==article_id, 'department_name']\n#     index_group = index_group['department_name'].reset_index(drop=True)[0]\n    data = articles.loc[articles['department_name']==index_group].reset_index(drop=True)\n    idx = data.loc[data['article_id']==article_id].index[0]\n\n    # defining tfidf vectorizer which also removes all english stop words\n    tfidf = TfidfVectorizer(stop_words='english')\n\n    # replacing null values with empty string\n    data['detail_desc'] = data['detail_desc'].fillna(\" \")\n\n    # creating tfidf matrix\n    tfidf_matrix = tfidf.fit_transform(data['detail_desc'])\n    tfidf_matrix = tfidf_matrix.astype('float32')\n\n    # computing the cosine similarity score of the prodcuts\n    cosine_sim = pairwise_distances(tfidf_matrix, tfidf_matrix, metric='cosine')   \n    # create a dataframe with article id and sim scores\n    sim_scores = cudf.DataFrame({'article_id':data['article_id'], 'sim_score':cosine_sim[idx]})\n    sim_scores['sim_score'] = 1 - sim_scores['sim_score']\n    # sorting sim scores from highest to lowest\n    sorted_sim_scores = sim_scores.sort_values(by=['sim_score'], ascending=False)\n    sorted_sim_scores = sorted_sim_scores.drop(sorted_sim_scores['article_id']==article_id)\n    sorted_sim_scores = sorted_sim_scores.reset_index(drop=True)\n    # \n    sorted_sim_scores = cupy.asarray(sorted_sim_scores.iloc[:12, 0])\n    sorted_sim_scores = ['0'+str(i) for i in sorted_sim_scores]\n    sorted_sim_scores = \" \".join(sorted_sim_scores)\n    \n    return sorted_sim_scores","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:39.450625Z","iopub.execute_input":"2022-04-17T18:03:39.451963Z","iopub.status.idle":"2022-04-17T18:03:39.461256Z","shell.execute_reply.started":"2022-04-17T18:03:39.451922Z","shell.execute_reply":"2022-04-17T18:03:39.460512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nm = product_similarity_scores(108775015, 'Jersey Basic')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:39.465244Z","iopub.execute_input":"2022-04-17T18:03:39.465463Z","iopub.status.idle":"2022-04-17T18:03:47.767606Z","shell.execute_reply.started":"2022-04-17T18:03:39.465438Z","shell.execute_reply":"2022-04-17T18:03:47.766869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = transactions[['t_dat', 'article_id', 'customer_id']]\ntrans = trans.drop_duplicates(subset='customer_id', keep=\"last\")\ntrans['t_dat'] = cudf.to_datetime(trans['t_dat'], format=\"%Y-%m-%d\")\n# trans = trans.set_index('customer_id')","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:47.768902Z","iopub.execute_input":"2022-04-17T18:03:47.769151Z","iopub.status.idle":"2022-04-17T18:03:54.642699Z","shell.execute_reply.started":"2022-04-17T18:03:47.769115Z","shell.execute_reply":"2022-04-17T18:03:54.641967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(trans.shape)\ntrans.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:54.643776Z","iopub.execute_input":"2022-04-17T18:03:54.644034Z","iopub.status.idle":"2022-04-17T18:03:54.671668Z","shell.execute_reply.started":"2022-04-17T18:03:54.644000Z","shell.execute_reply":"2022-04-17T18:03:54.670871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold_date = datetime.strptime('2020-08-22', '%Y-%m-%d')\ntrans_1month = trans[trans['t_dat']>threshold_date]","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:54.672878Z","iopub.execute_input":"2022-04-17T18:03:54.673199Z","iopub.status.idle":"2022-04-17T18:03:54.681329Z","shell.execute_reply.started":"2022-04-17T18:03:54.673163Z","shell.execute_reply":"2022-04-17T18:03:54.680549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(trans_1month.shape)\ntrans_1month.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:03:54.682588Z","iopub.execute_input":"2022-04-17T18:03:54.683374Z","iopub.status.idle":"2022-04-17T18:03:54.703921Z","shell.execute_reply.started":"2022-04-17T18:03:54.683336Z","shell.execute_reply":"2022-04-17T18:03:54.703241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_ids = sample_submission['customer_id']","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.277053Z","iopub.status.idle":"2022-04-17T18:00:53.277836Z","shell.execute_reply.started":"2022-04-17T18:00:53.277559Z","shell.execute_reply":"2022-04-17T18:00:53.277602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def recommendation(article_id):\n#     recommendations = product_similarity_scores(article_id, 'Jersey Basic')\n#     recommendations = cupy.asarray(recommendations.iloc[:12, 0])\n#     recommendations = ['0'+str(i) for i in recommendations]\n#     recommendations = \" \".join(recommendations)\n#     return recommendations","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.279307Z","iopub.status.idle":"2022-04-17T18:00:53.280033Z","shell.execute_reply.started":"2022-04-17T18:00:53.279730Z","shell.execute_reply":"2022-04-17T18:00:53.279756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# recommendation(624486001)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.281675Z","iopub.status.idle":"2022-04-17T18:00:53.282409Z","shell.execute_reply.started":"2022-04-17T18:00:53.282128Z","shell.execute_reply":"2022-04-17T18:00:53.282156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\narticle_id_list = []\ncustomer_id_list = []\nindex_group_list = []\n\n\nfor idx, x in enumerate(zip(trans_1month['article_id'].to_array(), trans_1month['customer_id'].to_array())):\n    article_id, customer_id = x\n    index_group = articles.loc[articles['article_id']==article_id, 'department_name']\n    index_group = index_group['department_name'].reset_index(drop=True)[0]\n    index_group_list.append(index_group)\n    article_id_list.append(article_id)\n    customer_id_list.append(customer_id)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.283821Z","iopub.status.idle":"2022-04-17T18:00:53.284635Z","shell.execute_reply.started":"2022-04-17T18:00:53.284317Z","shell.execute_reply":"2022-04-17T18:00:53.284346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nrec_list = []\nfor idx, x in enumerate(zip(np.array(article_id_list), np.array(index_group_list))):\n    art, index_group = x\n    rec = product_similarity_scores(art, index_group)\n    rec_list.append(rec)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.286222Z","iopub.status.idle":"2022-04-17T18:00:53.287030Z","shell.execute_reply.started":"2022-04-17T18:00:53.286680Z","shell.execute_reply":"2022-04-17T18:00:53.286708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'customer_id':customer_id_list, 'prediction':rec_list})","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.288673Z","iopub.status.idle":"2022-04-17T18:00:53.289449Z","shell.execute_reply.started":"2022-04-17T18:00:53.289140Z","shell.execute_reply":"2022-04-17T18:00:53.289169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_selling_last_month = trans_1month['article_id'].value_counts()[:12]\ntop_selling_last_month = list(top_selling_last_month.index.to_array())\ntop_selling_last_month = ['0'+str(i) for i in top_selling_last_month]\ntop_selling_last_month = \" \".join(top_selling_last_month)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.290919Z","iopub.status.idle":"2022-04-17T18:00:53.291660Z","shell.execute_reply.started":"2022-04-17T18:00:53.291363Z","shell.execute_reply":"2022-04-17T18:00:53.291391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rest_customer_list = customer_ids.to_array()\nrest_rec_list = [top_selling_last_month]*len(rest_customer_list)\nrest_submission = pd.DataFrame({'customer_id':rest_customer_list, 'prediction':rest_rec_list})","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.293107Z","iopub.status.idle":"2022-04-17T18:00:53.293866Z","shell.execute_reply.started":"2022-04-17T18:00:53.293585Z","shell.execute_reply":"2022-04-17T18:00:53.293614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.append(rest_submission, ignore_index=True)\nsubmission.drop_duplicates(subset=['customer_id'], keep='first', inplace=True)\nsubmission = submission.sort_values(by='customer_id').reset_index(drop=True)\nprint(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.295351Z","iopub.status.idle":"2022-04-17T18:00:53.296287Z","shell.execute_reply.started":"2022-04-17T18:00:53.295902Z","shell.execute_reply":"2022-04-17T18:00:53.295959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-17T18:00:53.298153Z","iopub.status.idle":"2022-04-17T18:00:53.299003Z","shell.execute_reply.started":"2022-04-17T18:00:53.298680Z","shell.execute_reply":"2022-04-17T18:00:53.298709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}