{"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":"**Using Content Based Filtering to recommend similar items by:**\n1. Creating user-feature matrix\n1. Creating item-feature matrix\n1. Measuring similraity using dot product as metric\n1. Recommending top-k similar items \n","metadata":{}},{"cell_type":"markdown","source":"**Second Approach**\n\nPerform dimensionality reduction using PCA on user_feature and item_feature matrices","metadata":{}},{"cell_type":"markdown","source":"**I used first 100000 rows from transactions record**\n\n**Limited customers to customers who bought at least two items**\n","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport plotly.graph_objects as go\nimport random\nfrom skimage import io","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-23T08:03:32.480739Z","iopub.execute_input":"2022-03-23T08:03:32.481659Z","iopub.status.idle":"2022-03-23T08:03:33.101884Z","shell.execute_reply.started":"2022-03-23T08:03:32.481565Z","shell.execute_reply":"2022-03-23T08:03:33.101034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv', chunksize=100000)\narticles = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\nusers = next(df)\ndf = users.merge(articles, on='article_id')\ndf = df[['t_dat', 'customer_id', 'article_id', 'prod_name', 'product_type_name',\n       'product_group_name', \n       'graphical_appearance_name', 'colour_group_name',\n       'perceived_colour_value_name',\n       'perceived_colour_master_name',\n       'department_name', 'index_name',\n       'index_group_name', 'section_name',\n       'garment_group_name', 'detail_desc']]\n\nfeature_subset = ['product_group_name', \n       'graphical_appearance_name', 'colour_group_name',\n       'perceived_colour_value_name',\n       'perceived_colour_master_name',\n       'department_name', 'index_name',\n       'index_group_name', 'section_name',\n       'garment_group_name']","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:03:35.081434Z","iopub.execute_input":"2022-03-23T08:03:35.081974Z","iopub.status.idle":"2022-03-23T08:03:36.565077Z","shell.execute_reply.started":"2022-03-23T08:03:35.081939Z","shell.execute_reply":"2022-03-23T08:03:36.564323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:03:38.002456Z","iopub.execute_input":"2022-03-23T08:03:38.002718Z","iopub.status.idle":"2022-03-23T08:03:38.010356Z","shell.execute_reply.started":"2022-03-23T08:03:38.002688Z","shell.execute_reply":"2022-03-23T08:03:38.009694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:03:38.890047Z","iopub.execute_input":"2022-03-23T08:03:38.890318Z","iopub.status.idle":"2022-03-23T08:03:38.911003Z","shell.execute_reply.started":"2022-03-23T08:03:38.890288Z","shell.execute_reply":"2022-03-23T08:03:38.910077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Choose features to build feature space\nfeatures = feature_subset\ndf1 = df[['customer_id', 'article_id'] + features]\ndummies_df = pd.get_dummies(df1, columns=features)\ndummies_df","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:03:40.994357Z","iopub.execute_input":"2022-03-23T08:03:40.994628Z","iopub.status.idle":"2022-03-23T08:03:41.358841Z","shell.execute_reply.started":"2022-03-23T08:03:40.994594Z","shell.execute_reply":"2022-03-23T08:03:41.358157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"minimum_items = 2\ngroupby_customer = dummies_df.groupby('customer_id')\n\nl = []\ncutomer_ids = []\narticle_ids = []\nfor key in groupby_customer.groups.keys():\n    temp = groupby_customer.get_group(key)\n    if temp.article_id.nunique() >= minimum_items:\n        l.append(temp.drop('article_id', axis=1).sum(numeric_only=True).values)\n        cutomer_ids.append(key)\n        article_ids.extend(temp.article_id.values.tolist())","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:03:51.104324Z","iopub.execute_input":"2022-03-23T08:03:51.104583Z","iopub.status.idle":"2022-03-23T08:04:39.569460Z","shell.execute_reply.started":"2022-03-23T08:03:51.104553Z","shell.execute_reply":"2022-03-23T08:04:39.568764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_feature = pd.DataFrame(l, columns = dummies_df.columns[2:])\nnormalized_user_feature = user_feature.div(user_feature.sum(axis=1), axis=0)\nnormalized_user_feature.insert(0, 'customer_id', cutomer_ids)\nnormalized_user_feature = normalized_user_feature.set_index('customer_id')\nnormalized_user_feature","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:04:43.906042Z","iopub.execute_input":"2022-03-23T08:04:43.906723Z","iopub.status.idle":"2022-03-23T08:04:49.579969Z","shell.execute_reply.started":"2022-03-23T08:04:43.906688Z","shell.execute_reply":"2022-03-23T08:04:49.579285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_feature = dummies_df.drop_duplicates(subset='article_id')\nitem_feature = item_feature[item_feature.article_id.isin(article_ids)].drop('customer_id', axis=1)\nitem_feature = item_feature.set_index('article_id')\nitem_feature","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:04:52.481195Z","iopub.execute_input":"2022-03-23T08:04:52.481909Z","iopub.status.idle":"2022-03-23T08:04:52.611633Z","shell.execute_reply.started":"2022-03-23T08:04:52.481867Z","shell.execute_reply":"2022-03-23T08:04:52.610830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = normalized_user_feature.dot(item_feature.T)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:04:55.455570Z","iopub.execute_input":"2022-03-23T08:04:55.455937Z","iopub.status.idle":"2022-03-23T08:04:59.802498Z","shell.execute_reply.started":"2022-03-23T08:04:55.455841Z","shell.execute_reply":"2022-03-23T08:04:59.801737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rcmnd(customer_id, scores):\n    cutomer_scores = scores.loc[customer_id]\n    customer_prev_items = groupby_customer.get_group(customer_id)['article_id']\n    prev_dropped = cutomer_scores.drop(customer_prev_items.values)\n    ordered = prev_dropped.sort_values(ascending=False)   \n    return ordered, customer_prev_items","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:04.386163Z","iopub.execute_input":"2022-03-23T08:05:04.386815Z","iopub.status.idle":"2022-03-23T08:05:04.392089Z","shell.execute_reply.started":"2022-03-23T08:05:04.386779Z","shell.execute_reply":"2022-03-23T08:05:04.391080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_prev(prev_items):\n    fig = plt.figure(figsize=(20, 10))\n    for item, i in zip(prev_items, range(1, len(prev_items)+1)):\n        item = '0' + str(item)\n        sub = item[:3]\n        image = path + \"/\"+ sub + \"/\"+ item +\".jpg\"\n        image = plt.imread(image)\n        fig.add_subplot(1, 6, i)\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:06.079275Z","iopub.execute_input":"2022-03-23T08:05:06.079526Z","iopub.status.idle":"2022-03-23T08:05:06.085663Z","shell.execute_reply.started":"2022-03-23T08:05:06.079497Z","shell.execute_reply":"2022-03-23T08:05:06.084915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_rcmnd(rcmnds):\n    fig = plt.figure(figsize=(20, 10))\n    for item, i in zip(rcmnds, range(1, k+1)):\n        item = '0' + str(item)\n        sub = item[:3]\n        image = path + \"/\"+ sub + \"/\"+ item +\".jpg\"\n        image = plt.imread(image)\n        fig.add_subplot(1, 6, i)\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:08.147218Z","iopub.execute_input":"2022-03-23T08:05:08.147471Z","iopub.status.idle":"2022-03-23T08:05:08.152564Z","shell.execute_reply.started":"2022-03-23T08:05:08.147442Z","shell.execute_reply":"2022-03-23T08:05:08.151925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:09.459273Z","iopub.execute_input":"2022-03-23T08:05:09.459943Z","iopub.status.idle":"2022-03-23T08:05:10.169459Z","shell.execute_reply.started":"2022-03-23T08:05:09.459906Z","shell.execute_reply":"2022-03-23T08:05:10.168730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=100)\npca.fit(normalized_user_feature)\npca.explained_variance_ratio_.sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:11.627156Z","iopub.execute_input":"2022-03-23T08:05:11.627422Z","iopub.status.idle":"2022-03-23T08:05:13.277060Z","shell.execute_reply.started":"2022-03-23T08:05:11.627393Z","shell.execute_reply":"2022-03-23T08:05:13.276340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_feature_pca = pd.DataFrame(pca.transform(normalized_user_feature), columns=['component_{}'.format(i) for i in range(1, 101)]).set_index(normalized_user_feature.index)\nitem_feature_pca = pd.DataFrame(pca.transform(item_feature), columns=['component_{}'.format(i) for i in range(1, 101)]).set_index(item_feature.index)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:15.548999Z","iopub.execute_input":"2022-03-23T08:05:15.549253Z","iopub.status.idle":"2022-03-23T08:05:15.770391Z","shell.execute_reply.started":"2022-03-23T08:05:15.549224Z","shell.execute_reply":"2022-03-23T08:05:15.769412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores_pca = user_feature_pca.dot(item_feature_pca.T)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:18.572264Z","iopub.execute_input":"2022-03-23T08:05:18.572515Z","iopub.status.idle":"2022-03-23T08:05:20.090567Z","shell.execute_reply.started":"2022-03-23T08:05:18.572487Z","shell.execute_reply":"2022-03-23T08:05:20.089625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k = 6\ncustomer_id = scores.index[1]\nrcmnds, prev_items = get_rcmnd(customer_id, scores)\nrcmnds_pca, prev_items = get_rcmnd(customer_id, scores_pca)\nrcmnds = rcmnds.index.values[:k]\nrcmnds_pca = rcmnds_pca.index.values[:k]\npath = \"../input/h-and-m-personalized-fashion-recommendations/images\"","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:22.944151Z","iopub.execute_input":"2022-03-23T08:05:22.944406Z","iopub.status.idle":"2022-03-23T08:05:22.964269Z","shell.execute_reply.started":"2022-03-23T08:05:22.944377Z","shell.execute_reply":"2022-03-23T08:05:22.963586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_prev(prev_items)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:25.941289Z","iopub.execute_input":"2022-03-23T08:05:25.941543Z","iopub.status.idle":"2022-03-23T08:05:27.213687Z","shell.execute_reply.started":"2022-03-23T08:05:25.941514Z","shell.execute_reply":"2022-03-23T08:05:27.212821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_rcmnd(rcmnds)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:29.945764Z","iopub.execute_input":"2022-03-23T08:05:29.946440Z","iopub.status.idle":"2022-03-23T08:05:32.819252Z","shell.execute_reply.started":"2022-03-23T08:05:29.946405Z","shell.execute_reply":"2022-03-23T08:05:32.817784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_rcmnd(rcmnds_pca)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:55.858627Z","iopub.execute_input":"2022-03-23T08:05:55.859269Z","iopub.status.idle":"2022-03-23T08:05:58.464301Z","shell.execute_reply.started":"2022-03-23T08:05:55.859227Z","shell.execute_reply":"2022-03-23T08:05:58.463680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos = 2000\nusers_cnt = len(normalized_user_feature)\nitems_cnt = len(item_feature)\ntrain_df = pd.DataFrame(columns = normalized_user_feature.columns.tolist()+item_feature.columns.tolist())\nfor _ in range(pos):\n    idx = np.random.randint(0,users_cnt-1)\n    user = normalized_user_feature.iloc[idx]\n    temp = groupby_customer.get_group(normalized_user_feature.index[idx])\n    if temp.article_id.nunique() >= minimum_items:\n        user_items = item_feature.loc[temp.sample(frac=0.75).article_id.unique()]\n        user_items = user_items.apply(lambda row:pd.concat([user,row]),axis = 'columns')\n        train_df = train_df.append(user_items,ignore_index = True)\nlen(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:09:51.438632Z","iopub.execute_input":"2022-03-23T08:09:51.438913Z","iopub.status.idle":"2022-03-23T08:10:09.323062Z","shell.execute_reply.started":"2022-03-23T08:09:51.438878Z","shell.execute_reply":"2022-03-23T08:10:09.322231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos_labels = pd.Series(np.ones(len(train_df)))","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:10:13.915505Z","iopub.execute_input":"2022-03-23T08:10:13.915760Z","iopub.status.idle":"2022-03-23T08:10:13.920046Z","shell.execute_reply.started":"2022-03-23T08:10:13.915730Z","shell.execute_reply":"2022-03-23T08:10:13.919315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_ids = set(article_ids)\n\nlen(article_ids)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:10:15.789255Z","iopub.execute_input":"2022-03-23T08:10:15.789509Z","iopub.status.idle":"2022-03-23T08:10:15.798706Z","shell.execute_reply.started":"2022-03-23T08:10:15.789478Z","shell.execute_reply":"2022-03-23T08:10:15.798078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"neg = 60\nfor _ in range(neg):\n    idx = np.random.randint(0,users_cnt-1)\n    user = normalized_user_feature.iloc[idx]\n    temp = groupby_customer.get_group(normalized_user_feature.index[idx])\n    user_articles_neg = list(article_ids - set(temp.article_id.unique()))\n    items_current_user = item_feature.loc[np.random.choice(user_articles_neg,len(user_articles_neg)//150)]\n    items_current_user = items_current_user.apply(lambda row:pd.concat([user,row]),axis='columns')\n    train_df = train_df.append(items_current_user,ignore_index = True)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:10:23.361555Z","iopub.execute_input":"2022-03-23T08:10:23.363997Z","iopub.status.idle":"2022-03-23T08:10:27.868543Z","shell.execute_reply.started":"2022-03-23T08:10:23.363946Z","shell.execute_reply":"2022-03-23T08:10:27.867810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"neg_labels = pd.Series(np.zeros(len(train_df)-len(pos_labels)))\nlen(neg_labels)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:10:30.607572Z","iopub.execute_input":"2022-03-23T08:10:30.607818Z","iopub.status.idle":"2022-03-23T08:10:30.614748Z","shell.execute_reply.started":"2022-03-23T08:10:30.607789Z","shell.execute_reply":"2022-03-23T08:10:30.613925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:10:32.659475Z","iopub.execute_input":"2022-03-23T08:10:32.660027Z","iopub.status.idle":"2022-03-23T08:10:36.385348Z","shell.execute_reply.started":"2022-03-23T08:10:32.659989Z","shell.execute_reply":"2022-03-23T08:10:36.383528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\nSHUFFLE = 100\nTRAIN_PERCENT = 0.8\n\nfeatures = np.array(train_df)\nlabels = pd.concat([pos_labels,neg_labels],ignore_index = True)\nfull_dataset = tf.data.Dataset.from_tensor_slices((features, tf.one_hot(indices = labels,depth = 2)))\nsep = int(len(features)*TRAIN_PERCENT)\ntrain_features = features[:sep]\ntest_features = features[sep:]\ntrain_labels = labels.iloc[:sep]\ntest_labels = labels.iloc[sep:]\n\ntrain_dataset = tf.data.Dataset.from_tensor_slices((train_features, tf.one_hot(indices = train_labels,depth = 2)))\ntest_dataset = tf.data.Dataset.from_tensor_slices((test_features, tf.one_hot(indices = test_labels,depth = 2)))\n\n\ntrain_dataset = train_dataset.shuffle(SHUFFLE).batch(BATCH_SIZE)\ntest_dataset = test_dataset.shuffle(SHUFFLE).batch(BATCH_SIZE)\nfull_dataset = full_dataset.shuffle(SHUFFLE).batch(BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:10:50.266204Z","iopub.execute_input":"2022-03-23T08:10:50.266774Z","iopub.status.idle":"2022-03-23T08:10:52.777077Z","shell.execute_reply.started":"2022-03-23T08:10:50.266735Z","shell.execute_reply":"2022-03-23T08:10:52.776364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential(\n    [\n        tf.keras.layers.Dense(512,activation='relu'),\n        tf.keras.layers.Dropout(0.2),\n        tf.keras.layers.Dense(256,activation='relu'),\n        tf.keras.layers.Dropout(0.2),\n        tf.keras.layers.Dense(128,activation='relu'),\n        tf.keras.layers.Dropout(0.2),\n        tf.keras.layers.Dense(64,activation='relu'),\n        tf.keras.layers.Dropout(0.2),\n        tf.keras.layers.Dense(32,activation='relu'),\n        tf.keras.layers.Dense(2)\n    ]\n)\nmodel.compile(loss=tf.losses.BinaryCrossentropy(from_logits=True),\n                optimizer=tf.optimizers.Adam(learning_rate=0.0001),\n             metrics=[tf.keras.metrics.BinaryAccuracy()])","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:15:23.980271Z","iopub.execute_input":"2022-03-23T08:15:23.980538Z","iopub.status.idle":"2022-03-23T08:15:24.006544Z","shell.execute_reply.started":"2022-03-23T08:15:23.980509Z","shell.execute_reply":"2022-03-23T08:15:24.005892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset, epochs=30,validation_data = test_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:15:28.348612Z","iopub.execute_input":"2022-03-23T08:15:28.348972Z","iopub.status.idle":"2022-03-23T08:15:51.831617Z","shell.execute_reply.started":"2022-03-23T08:15:28.348936Z","shell.execute_reply":"2022-03-23T08:15:51.830919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt_possible_rcmnd = 3\nuser = normalized_user_feature.iloc[30]\nfor _ in range(cnt_possible_rcmnd):\n    item = item_feature.iloc[np.random.randint(0,items_cnt)]\n    print(item.name)\n    datapoint = np.array(pd.concat([user,item],ignore_index = True))\n    datapoint = np.reshape(datapoint,(1,-1))\n    x = model.predict(datapoint)\n    print(tf.nn.softmax(x))","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:15:55.735590Z","iopub.execute_input":"2022-03-23T08:15:55.736400Z","iopub.status.idle":"2022-03-23T08:15:55.897282Z","shell.execute_reply.started":"2022-03-23T08:15:55.736352Z","shell.execute_reply":"2022-03-23T08:15:55.896581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:16:02.135194Z","iopub.execute_input":"2022-03-23T08:16:02.135445Z","iopub.status.idle":"2022-03-23T08:16:02.249986Z","shell.execute_reply.started":"2022-03-23T08:16:02.135416Z","shell.execute_reply":"2022-03-23T08:16:02.249289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train','val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:16:34.233696Z","iopub.execute_input":"2022-03-23T08:16:34.234449Z","iopub.status.idle":"2022-03-23T08:16:34.447643Z","shell.execute_reply.started":"2022-03-23T08:16:34.234408Z","shell.execute_reply":"2022-03-23T08:16:34.446982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['binary_accuracy'])\nplt.plot(history.history['val_binary_accuracy'])\nplt.title('model binary accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train','val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:17:06.481500Z","iopub.execute_input":"2022-03-23T08:17:06.481757Z","iopub.status.idle":"2022-03-23T08:17:06.661055Z","shell.execute_reply.started":"2022-03-23T08:17:06.481727Z","shell.execute_reply":"2022-03-23T08:17:06.660337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:39:33.889770Z","iopub.execute_input":"2022-03-23T08:39:33.890078Z","iopub.status.idle":"2022-03-23T08:40:33.995944Z","shell.execute_reply.started":"2022-03-23T08:39:33.890030Z","shell.execute_reply":"2022-03-23T08:40:33.995153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"end = pd.to_datetime(df.t_dat.max())\nstart = end-pd.DateOffset(days=7)\nend = str(end.date())\nstart = str(start.date())\n","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:46:57.305445Z","iopub.execute_input":"2022-03-23T08:46:57.305712Z","iopub.status.idle":"2022-03-23T08:47:01.000642Z","shell.execute_reply.started":"2022-03-23T08:46:57.305680Z","shell.execute_reply":"2022-03-23T08:47:00.999907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_to_rcmnd = df[(df.t_dat>=start) & (df.t_dat<=end)].customer_id.unique()\nlen(customers_to_rcmnd)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:01:15.022554Z","iopub.execute_input":"2022-03-23T10:01:15.023381Z","iopub.status.idle":"2022-03-23T10:01:22.613533Z","shell.execute_reply.started":"2022-03-23T10:01:15.023339Z","shell.execute_reply":"2022-03-23T10:01:22.612733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_popular_items_of_the_week = df[(df.t_dat>=start) & (df.t_dat<=end)].groupby('article_id').size().sort_values(ascending=False).iloc[:7]","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:12:44.468275Z","iopub.execute_input":"2022-03-23T09:12:44.468836Z","iopub.status.idle":"2022-03-23T09:12:52.029330Z","shell.execute_reply.started":"2022-03-23T09:12:44.468800Z","shell.execute_reply":"2022-03-23T09:12:52.028604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_popular_items_of_the_week=most_popular_items_of_the_week.index.values\nmost_popular_items_of_the_week = list(map(str,most_popular_items_of_the_week))\nmost_popular_items_of_the_week","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:12:53.785576Z","iopub.execute_input":"2022-03-23T09:12:53.786136Z","iopub.status.idle":"2022-03-23T09:12:53.792364Z","shell.execute_reply.started":"2022-03-23T09:12:53.786096Z","shell.execute_reply":"2022-03-23T09:12:53.791430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(columns = ['customer_id','prediction'])\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:11:21.632692Z","iopub.execute_input":"2022-03-23T09:11:21.632962Z","iopub.status.idle":"2022-03-23T09:11:21.642381Z","shell.execute_reply.started":"2022-03-23T09:11:21.632931Z","shell.execute_reply":"2022-03-23T09:11:21.641473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for el in customers_to_rcmnd:\n    if el in normalized_user_feature.index:\n        cnt_possible_rcmnd = 30\n        user = normalized_user_feature.loc[el]\n        rcmnd = []\n        for _ in range(cnt_possible_rcmnd):\n            item = item_feature.iloc[np.random.randint(0,items_cnt)]\n            datapoint = np.array(pd.concat([user,item],ignore_index = True))\n            datapoint = np.reshape(datapoint,(1,-1))\n            x = tf.nn.softmax(model.predict(datapoint))\n            if x[0][1]>=0.8:\n                rcmnd.append(str(item.name))\n    else:\n        rcmnd = most_popular_items_of_the_week\n    submission = submission.append(pd.Series([el,\" \".join(rcmnd)],index = ['customer_id','prediction']),ignore_index = True)\n        \n        \n                \n            ","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:13:32.905408Z","iopub.execute_input":"2022-03-23T09:13:32.905690Z","iopub.status.idle":"2022-03-23T10:00:32.516616Z","shell.execute_reply.started":"2022-03-23T09:13:32.905658Z","shell.execute_reply":"2022-03-23T10:00:32.515489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:03:18.876566Z","iopub.execute_input":"2022-03-23T10:03:18.876827Z","iopub.status.idle":"2022-03-23T10:03:19.648205Z","shell.execute_reply.started":"2022-03-23T10:03:18.876797Z","shell.execute_reply":"2022-03-23T10:03:19.647362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:04:30.662207Z","iopub.execute_input":"2022-03-23T10:04:30.662687Z","iopub.status.idle":"2022-03-23T10:04:31.112808Z","shell.execute_reply.started":"2022-03-23T10:04:30.662649Z","shell.execute_reply":"2022-03-23T10:04:31.111919Z"},"trusted":true},"execution_count":null,"outputs":[]}]}