{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip install scikit-surprise","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-14T14:50:30.273682Z","iopub.execute_input":"2024-04-14T14:50:30.274389Z","iopub.status.idle":"2024-04-14T14:50:42.63484Z","shell.execute_reply.started":"2024-04-14T14:50:30.274349Z","shell.execute_reply":"2024-04-14T14:50:42.633719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\nfrom mlxtend.preprocessing import TransactionEncoder\nfrom mlxtend.frequent_patterns import apriori, association_rules\nfrom surprise import Reader, Dataset, SVD\nfrom surprise.model_selection import train_test_split, cross_validate\nfrom surprise.accuracy import rmse\nimport os","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:50:42.636903Z","iopub.execute_input":"2024-04-14T14:50:42.637235Z","iopub.status.idle":"2024-04-14T14:50:42.642887Z","shell.execute_reply.started":"2024-04-14T14:50:42.637208Z","shell.execute_reply":"2024-04-14T14:50:42.642005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\ncustomers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv',engine='python')\ntransactions_train = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:50:42.644119Z","iopub.execute_input":"2024-04-14T14:50:42.64441Z","iopub.status.idle":"2024-04-14T14:51:35.955327Z","shell.execute_reply.started":"2024-04-14T14:50:42.64438Z","shell.execute_reply":"2024-04-14T14:51:35.954516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:35.95643Z","iopub.execute_input":"2024-04-14T14:51:35.956715Z","iopub.status.idle":"2024-04-14T14:51:35.984608Z","shell.execute_reply.started":"2024-04-14T14:51:35.956691Z","shell.execute_reply":"2024-04-14T14:51:35.983705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:35.987934Z","iopub.execute_input":"2024-04-14T14:51:35.988245Z","iopub.status.idle":"2024-04-14T14:51:36.00309Z","shell.execute_reply.started":"2024-04-14T14:51:35.988221Z","shell.execute_reply":"2024-04-14T14:51:36.002106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:36.004272Z","iopub.execute_input":"2024-04-14T14:51:36.004545Z","iopub.status.idle":"2024-04-14T14:51:36.018088Z","shell.execute_reply.started":"2024-04-14T14:51:36.004515Z","shell.execute_reply":"2024-04-14T14:51:36.017204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(articles.shape)\nprint(customers.shape)\nprint(transactions_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:36.019121Z","iopub.execute_input":"2024-04-14T14:51:36.019372Z","iopub.status.idle":"2024-04-14T14:51:36.028542Z","shell.execute_reply.started":"2024-04-14T14:51:36.01935Z","shell.execute_reply":"2024-04-14T14:51:36.027717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:36.029791Z","iopub.execute_input":"2024-04-14T14:51:36.030093Z","iopub.status.idle":"2024-04-14T14:51:36.180731Z","shell.execute_reply.started":"2024-04-14T14:51:36.030046Z","shell.execute_reply":"2024-04-14T14:51:36.179777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:36.181802Z","iopub.execute_input":"2024-04-14T14:51:36.182085Z","iopub.status.idle":"2024-04-14T14:51:36.801308Z","shell.execute_reply.started":"2024-04-14T14:51:36.182039Z","shell.execute_reply":"2024-04-14T14:51:36.800354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:36.802457Z","iopub.execute_input":"2024-04-14T14:51:36.802724Z","iopub.status.idle":"2024-04-14T14:51:42.791126Z","shell.execute_reply.started":"2024-04-14T14:51:36.802701Z","shell.execute_reply":"2024-04-14T14:51:42.79026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib\nmatplotlib.rcParams['figure.figsize'] = (20,6)\nsns.heatmap(customers.isnull(),yticklabels = False, cbar = False , cmap = 'viridis')\nmatplotlib.pyplot.title(\"Missing null values\")","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:42.792559Z","iopub.execute_input":"2024-04-14T14:51:42.793194Z","iopub.status.idle":"2024-04-14T14:51:54.809391Z","shell.execute_reply.started":"2024-04-14T14:51:42.793151Z","shell.execute_reply":"2024-04-14T14:51:54.808422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['age'].mean()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:54.810655Z","iopub.execute_input":"2024-04-14T14:51:54.810954Z","iopub.status.idle":"2024-04-14T14:51:54.823071Z","shell.execute_reply.started":"2024-04-14T14:51:54.810928Z","shell.execute_reply":"2024-04-14T14:51:54.822202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## use mean value of the age to deal with null value\ncustomers['age']= customers['age'].fillna(customers['age'].mean())","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:54.824185Z","iopub.execute_input":"2024-04-14T14:51:54.824442Z","iopub.status.idle":"2024-04-14T14:51:54.842936Z","shell.execute_reply.started":"2024-04-14T14:51:54.82442Z","shell.execute_reply":"2024-04-14T14:51:54.842212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:54.847206Z","iopub.execute_input":"2024-04-14T14:51:54.84799Z","iopub.status.idle":"2024-04-14T14:51:55.013032Z","shell.execute_reply.started":"2024-04-14T14:51:54.847954Z","shell.execute_reply":"2024-04-14T14:51:55.012033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Bin and classify the age column into three group \n\ncustomers['age_by_decade'] = pd.cut(x=customers['age'], bins=[ 10, 49, 69, 100], labels=['Young age', 'Medium age', 'Older age'])","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:55.014362Z","iopub.execute_input":"2024-04-14T14:51:55.014684Z","iopub.status.idle":"2024-04-14T14:51:55.050099Z","shell.execute_reply.started":"2024-04-14T14:51:55.014659Z","shell.execute_reply":"2024-04-14T14:51:55.049275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## find the number of each customer age groups\nsns.countplot(data=customers, x=\"age_by_decade\", hue=\"Active\")","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:55.051261Z","iopub.execute_input":"2024-04-14T14:51:55.051535Z","iopub.status.idle":"2024-04-14T14:51:55.440335Z","shell.execute_reply.started":"2024-04-14T14:51:55.051512Z","shell.execute_reply":"2024-04-14T14:51:55.439222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=customers, x= \"club_member_status\", hue=\"age_by_decade\")","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:55.441696Z","iopub.execute_input":"2024-04-14T14:51:55.442031Z","iopub.status.idle":"2024-04-14T14:51:57.347826Z","shell.execute_reply.started":"2024-04-14T14:51:55.442Z","shell.execute_reply":"2024-04-14T14:51:57.346894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### find number of transaction each day  \n\ntran_freq = pd.DataFrame(transactions_train.groupby(['t_dat']).size(), columns= ['Frequency'])\n\nprint(tran_freq)\n\n## Plot transactions over time day by day from 2018-09-20 to 2020-09-22\n\n\nfrom matplotlib import pyplot\ntran_freq.plot()\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:51:57.349285Z","iopub.execute_input":"2024-04-14T14:51:57.349972Z","iopub.status.idle":"2024-04-14T14:52:00.8708Z","shell.execute_reply.started":"2024-04-14T14:51:57.349934Z","shell.execute_reply":"2024-04-14T14:52:00.869768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train['t_dat'] = pd.to_datetime(transactions_train['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:52:00.872008Z","iopub.execute_input":"2024-04-14T14:52:00.872328Z","iopub.status.idle":"2024-04-14T14:52:06.04923Z","shell.execute_reply.started":"2024-04-14T14:52:00.872303Z","shell.execute_reply":"2024-04-14T14:52:06.048383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### find number of transaction each day  \n\ntran_freq = pd.DataFrame(transactions_train.groupby(['t_dat']).size(), columns= ['Frequency'])\n\nprint(tran_freq)\n\n## Plot transactions over time day by day from 2018-09-20 to 2020-09-22\n\n\nfrom matplotlib import pyplot\ntran_freq.plot()\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:52:06.050397Z","iopub.execute_input":"2024-04-14T14:52:06.050684Z","iopub.status.idle":"2024-04-14T14:52:07.177774Z","shell.execute_reply.started":"2024-04-14T14:52:06.050658Z","shell.execute_reply":"2024-04-14T14:52:07.176904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tran_freq = tran_freq.reset_index(level=['t_dat'])\ntran_freq.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:52:07.179007Z","iopub.execute_input":"2024-04-14T14:52:07.179386Z","iopub.status.idle":"2024-04-14T14:52:07.190742Z","shell.execute_reply.started":"2024-04-14T14:52:07.179354Z","shell.execute_reply":"2024-04-14T14:52:07.18975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### find the year and months columns from date column\n\ntran_freq['Year'] = pd.DatetimeIndex(tran_freq['t_dat']).year\ntran_freq['Month'] = pd.DatetimeIndex(tran_freq['t_dat']).month\ntran_freq['Month_Year'] = pd.to_datetime(tran_freq['t_dat']).dt.to_period('M')\n \ntran_freq.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:52:07.192356Z","iopub.execute_input":"2024-04-14T14:52:07.19268Z","iopub.status.idle":"2024-04-14T14:52:07.210429Z","shell.execute_reply.started":"2024-04-14T14:52:07.192651Z","shell.execute_reply":"2024-04-14T14:52:07.209474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### find the total sales for each month in 2018 and 2019 \n\npv = pd.pivot_table(tran_freq, index='Month', columns= 'Year', values= 'Frequency', aggfunc='sum')\n\nprint(pv)\n\npv.plot(kind='bar', figsize=(17, 10), color=['red', 'black', 'green'], rot=0)                                       \nplt.title(\"Historical Count TOT_SALES: Month by Month Comparison\", y=1.013, fontsize=22)\nplt.xlabel(\"Date [Month] \", labelpad=16)\nplt.ylabel(\"Count [TOT_SALES]\", labelpad=16);","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:52:07.211496Z","iopub.execute_input":"2024-04-14T14:52:07.21175Z","iopub.status.idle":"2024-04-14T14:52:07.702995Z","shell.execute_reply.started":"2024-04-14T14:52:07.211728Z","shell.execute_reply":"2024-04-14T14:52:07.702118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read the CSV file into a pandas DataFrame\ntransaction_data = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv', engine='python')\n\n# Convert the last 16 characters of 'customer_id' to hex, then to int64\ntransaction_data['customer_id'] = transaction_data['customer_id'].str[-16:].apply(lambda x: int(x, 16)).astype('int64')\n\n\n# Convert 'article_id' to int32\ntransaction_data['article_id'] = transaction_data['article_id'].astype('int32')\n\n# Convert 't_dat' to datetime\ntransaction_data['t_dat'] = pd.to_datetime(transaction_data['t_dat'])\n\n# Select specific columns and rearrange the DataFrame\ntransaction_data = transaction_data[['t_dat', 'customer_id', 'article_id', 'price']]","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:52:07.704298Z","iopub.execute_input":"2024-04-14T14:52:07.7047Z","iopub.status.idle":"2024-04-14T14:56:57.160006Z","shell.execute_reply.started":"2024-04-14T14:52:07.704655Z","shell.execute_reply":"2024-04-14T14:56:57.159219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data preprocess\ntransaction_data.loc[:, 'year'] = transaction_data['t_dat'].dt.year\ntransaction_data.loc[:, 'month'] = transaction_data['t_dat'].dt.month\ntransaction_data.loc[:, 'day'] = transaction_data['t_dat'].dt.day\n\n# Convert the DataFrame to parquet format for faster read/write\ntransaction_data.to_parquet('transaction_data_hm.pqt', index=False)\n\n# Read the transaction data from the parquet file\ntransaction_data = pd.read_parquet(\"./transaction_data_hm.pqt\")","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:56:57.161654Z","iopub.execute_input":"2024-04-14T14:56:57.162009Z","iopub.status.idle":"2024-04-14T14:57:07.751165Z","shell.execute_reply.started":"2024-04-14T14:56:57.161975Z","shell.execute_reply":"2024-04-14T14:57:07.75012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizations\nplt.figure(figsize=(10, 6))\nsns.countplot(x=transaction_data['year'])\nplt.xlabel(\"Year\")\nplt.ylabel(\"Count of transactions\")\nplt.title(\"Transaction count by year\")\nplt.show()\n\nplt.figure(figsize=(10, 6))\ntransaction_data.month.value_counts().plot(kind=\"bar\")\nplt.xlabel(\"Month\")\nplt.ylabel(\"Count of transactions\")\nplt.title(\"Transaction count by month\")\nplt.show()\n\nplt.figure(figsize=(10, 6))\nsns.countplot(x=\"day\", data=transaction_data)\nplt.xlabel(\"Day\")\nplt.ylabel(\"Count of transactions\")\nplt.title(\"Transaction count by day\")\nplt.show()\n\ntransactions_2020 = transaction_data[transaction_data['year'] == 2020]\nplt.figure(figsize=(10, 6))\nsns.lineplot(x=\"t_dat\", y=\"price\", data=transactions_2020)\nplt.xlabel(\"Date\")\nplt.ylabel(\"Price\")\nplt.title(\"Price trend in 2020\")\nplt.xticks(rotation=45)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:57:07.752218Z","iopub.execute_input":"2024-04-14T14:57:07.752476Z","iopub.status.idle":"2024-04-14T14:58:32.055426Z","shell.execute_reply.started":"2024-04-14T14:57:07.752453Z","shell.execute_reply":"2024-04-14T14:58:32.054412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Surprise dataset\nreader = Reader(rating_scale=(1, 5))\ndata = Dataset.load_from_df(transaction_data[['customer_id', 'article_id', 'price']], reader)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:58:32.056747Z","iopub.execute_input":"2024-04-14T14:58:32.057382Z","iopub.status.idle":"2024-04-14T14:59:17.415071Z","shell.execute_reply.started":"2024-04-14T14:58:32.057347Z","shell.execute_reply":"2024-04-14T14:59:17.414216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and testing sets\ntrainset, testset = train_test_split(data, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T14:59:17.416108Z","iopub.execute_input":"2024-04-14T14:59:17.416368Z","iopub.status.idle":"2024-04-14T15:00:48.74466Z","shell.execute_reply.started":"2024-04-14T14:59:17.416346Z","shell.execute_reply":"2024-04-14T15:00:48.743565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the SVD algorithm\nsvd = SVD()\nsvd.fit(trainset)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:19.198667Z","iopub.execute_input":"2024-04-14T15:04:19.199045Z","iopub.status.idle":"2024-04-14T15:12:43.207439Z","shell.execute_reply.started":"2024-04-14T15:04:19.199014Z","shell.execute_reply":"2024-04-14T15:12:43.20634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from surprise import BaselineOnly ","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:14:14.534287Z","iopub.execute_input":"2024-04-14T15:14:14.534657Z","iopub.status.idle":"2024-04-14T15:14:14.539147Z","shell.execute_reply.started":"2024-04-14T15:14:14.534627Z","shell.execute_reply":"2024-04-14T15:14:14.538208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train ALS\nals = BaselineOnly(bsl_options={'method': 'als'})\nals.fit(trainset)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:14:19.039667Z","iopub.execute_input":"2024-04-14T15:14:19.04003Z","iopub.status.idle":"2024-04-14T15:16:06.678503Z","shell.execute_reply.started":"2024-04-14T15:14:19.040001Z","shell.execute_reply":"2024-04-14T15:16:06.67748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test set using both models\nsvd_predictions = svd.test(testset)\nals_predictions = als.test(testset)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:16:06.68052Z","iopub.execute_input":"2024-04-14T15:16:06.68116Z","iopub.status.idle":"2024-04-14T15:18:40.322218Z","shell.execute_reply.started":"2024-04-14T15:16:06.681123Z","shell.execute_reply":"2024-04-14T15:18:40.321363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from surprise import accuracy","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:18:40.323317Z","iopub.execute_input":"2024-04-14T15:18:40.323573Z","iopub.status.idle":"2024-04-14T15:18:40.327761Z","shell.execute_reply.started":"2024-04-14T15:18:40.32355Z","shell.execute_reply":"2024-04-14T15:18:40.326877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate RMSE for both models\nprint(\"SVD RMSE:\", rmse(svd_predictions))\nprint(\"ALS RMSE:\", rmse(als_predictions))\n","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:19:26.222214Z","iopub.execute_input":"2024-04-14T15:19:26.222941Z","iopub.status.idle":"2024-04-14T15:19:35.573984Z","shell.execute_reply.started":"2024-04-14T15:19:26.222909Z","shell.execute_reply":"2024-04-14T15:19:35.573016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(combined_rmse)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:00.354286Z","iopub.status.idle":"2024-04-14T15:04:00.35463Z","shell.execute_reply.started":"2024-04-14T15:04:00.354436Z","shell.execute_reply":"2024-04-14T15:04:00.354448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_predictions = []\nfor svd_pred, als_pred in zip(svd_predictions, als_predictions):\n    combined_rating = (svd_pred.est + als_pred.est) / 2\n    combined_predictions.append(combined_rating)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:18:49.582424Z","iopub.execute_input":"2024-04-14T15:18:49.582721Z","iopub.status.idle":"2024-04-14T15:18:52.346039Z","shell.execute_reply.started":"2024-04-14T15:18:49.582695Z","shell.execute_reply":"2024-04-14T15:18:52.345236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from surprise import Prediction\n\n# Convert combined predictions to Prediction objects\ncombined_predictions_objects = [Prediction(pred.uid, pred.iid, pred.r_ui, pred.est, pred.details) for pred in svd_pred]","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:21:05.968271Z","iopub.execute_input":"2024-04-14T15:21:05.968613Z","iopub.status.idle":"2024-04-14T15:21:06.010199Z","shell.execute_reply.started":"2024-04-14T15:21:05.968588Z","shell.execute_reply":"2024-04-14T15:21:06.00891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate RMSE for the combined predictions\ncombined_rmse = rmse(combined_predictions)\nprint(\"Combined RMSE:\", combined_rmse)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:18:52.347185Z","iopub.execute_input":"2024-04-14T15:18:52.347486Z","iopub.status.idle":"2024-04-14T15:18:52.503901Z","shell.execute_reply.started":"2024-04-14T15:18:52.34746Z","shell.execute_reply":"2024-04-14T15:18:52.502744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract association rules using Apriori\nbasket = transaction_data.groupby(['customer_id', 'article_id'])['price'].sum().unstack().reset_index().fillna(0).set_index('customer_id')\nbasket_sets = basket.applymap(lambda x: 1 if x > 0 else 0)\n\nfrequent_itemsets = apriori(basket_sets, min_support=0.05, use_colnames=True)\nrules = association_rules(frequent_itemsets, metric=\"lift\", min_threshold=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:00.358582Z","iopub.status.idle":"2024-04-14T15:04:00.358872Z","shell.execute_reply.started":"2024-04-14T15:04:00.358725Z","shell.execute_reply":"2024-04-14T15:04:00.358737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display association rules\nprint(rules)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:00.360247Z","iopub.status.idle":"2024-04-14T15:04:00.360547Z","shell.execute_reply.started":"2024-04-14T15:04:00.360398Z","shell.execute_reply":"2024-04-14T15:04:00.36041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualization of association rules\nplt.figure(figsize=(10, 6))\nplt.scatter(rules['support'], rules['confidence'], alpha=0.5)\nplt.xlabel('Support')\nplt.ylabel('Confidence')\nplt.title('Support vs Confidence')\nplt.show()\n\nplt.figure(figsize=(10, 6))\nplt.scatter(rules['support'], rules['lift'], alpha=0.5)\nplt.xlabel('Support')\nplt.ylabel('Lift')\nplt.title('Support vs Lift')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:00.36171Z","iopub.status.idle":"2024-04-14T15:04:00.362008Z","shell.execute_reply.started":"2024-04-14T15:04:00.361857Z","shell.execute_reply":"2024-04-14T15:04:00.36187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Top customers\ntop_customers_id = transaction_data['customer_id'].value_counts().index[:10].tolist()\ntop_10_customers = transaction_data[transaction_data['customer_id'].isin(top_customers_id)]\nplt.figure(figsize=(10, 6))\nsns.countplot(x='customer_id', data=top_10_customers)\nplt.xlabel('Customer ID')\nplt.ylabel('Count of Transactions')\nplt.title('Top 10 Customers')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:00.362951Z","iopub.status.idle":"2024-04-14T15:04:00.363285Z","shell.execute_reply.started":"2024-04-14T15:04:00.363121Z","shell.execute_reply":"2024-04-14T15:04:00.363135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Top articles\ntop_articles = transaction_data['article_id'].value_counts().head(10)\nplt.figure(figsize=(10, 6))\ntop_articles.plot(kind='bar')\nplt.xlabel('Article ID')\nplt.ylabel('Count of Transactions')\nplt.title('Top 10 Articles')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:00.364938Z","iopub.status.idle":"2024-04-14T15:04:00.365275Z","shell.execute_reply.started":"2024-04-14T15:04:00.365115Z","shell.execute_reply":"2024-04-14T15:04:00.365129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Recommendations for a specific customer using SVD\ndef get_top_n_recommendations(customer_id, n=5):\n    user_items = basket_sets.loc[customer_id]\n    items_to_recommend = user_items[user_items == 0].index\n    recommendations = []\n\n    for item_id in items_to_recommend:\n        est_rating = svd.predict(customer_id, item_id).est\n        recommendations.append((item_id, est_rating))\n\n    recommendations.sort(key=lambda x: x[1], reverse=True)\n    return recommendations[:n]\n\ncustomer_id = 1234567890  # Replace with a specific customer ID\nrecommendations = get_top_n_recommendations(customer_id)\nprint(\"Top 5 recommendations for Customer {}: \".format(customer_id))\nfor item_id, est_rating in recommendations:\n    print(\"- Article ID: {}, Estimated Rating: {:.2f}\".format(item_id, est_rating))","metadata":{"execution":{"iopub.status.busy":"2024-04-14T15:04:00.366746Z","iopub.status.idle":"2024-04-14T15:04:00.367203Z","shell.execute_reply.started":"2024-04-14T15:04:00.366952Z","shell.execute_reply":"2024-04-14T15:04:00.366969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}