{ "cells": [ { "cell_type": "code", "execution_count": 9, "id": "11777db4-14dd-4991-87e4-a8e6ec0c7e89", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "124033\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import networkx as nx\n", "from sklearn.metrics.pairwise import cosine_similarity\n", "\n", "def generate_graph_modality(file_path, threshold=0.2):\n", " # Read the uploaded file containing user-item ratings\n", " ratings_df = pd.read_csv(file_path) # Assuming CSV format, adjust accordingly if different\n", "\n", " # Compute user-item matrix\n", " user_item_matrix = pd.pivot_table(ratings_df, values='rating', index='user_id', columns='business_id', fill_value=0)\n", "\n", " # Compute cosine similarity between users\n", " user_similarity_matrix = cosine_similarity(user_item_matrix)\n", "\n", " # Convert similarity matrix to binary adjacency matrix\n", " binary_adjacency_matrix = np.where(user_similarity_matrix > threshold, 1, 0)\n", "\n", " # Convert binary adjacency matrix to a list of tuples for graph modality\n", " graph_modality_list = []\n", " for i in range(len(user_item_matrix.index)):\n", " for j in range(i + 1, len(user_item_matrix.index)):\n", " if binary_adjacency_matrix[i][j] == 1:\n", " graph_modality_list.append((user_item_matrix.index[i], user_item_matrix.index[j], 1.0))\n", "\n", " return graph_modality_list\n", "\n", "# Example usage:\n", "file_path = \"../data/rating_final.csv\" # Update with the actual file path\n", "graph_modality_list = generate_graph_modality(file_path)\n", "trust_graph_df = pd.DataFrame(graph_modality_list)\n", "# print(\"Graph Modality List:\")\n", "# print(graph_modality_list)\n", "print(len(trust_graph_df))" ] }, { "cell_type": "code", "execution_count": 10, "id": "b877dbe6-7175-4de9-ba89-37d43661500e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rating_threshold = 1.0\n", "exclude_unknowns = True\n", "---\n", "Training data:\n", "Number of users = 10999\n", "Number of items = 4922\n", "Number of ratings = 176857\n", "Max rating = 5.0\n", "Min rating = 1.0\n", "Global mean = 3.8\n", "---\n", "Test data:\n", "Number of users = 10999\n", "Number of items = 4922\n", "Number of ratings = 58885\n", "Number of unknown users = 0\n", "Number of unknown items = 0\n", "---\n", "Validation data:\n", "Number of users = 10999\n", "Number of items = 4922\n", "Number of ratings = 58902\n", "---\n", "Total users = 10999\n", "Total items = 4922\n", "Total number of users: 11000\n", "Total number of restaurants: 4963\n", "Total possible ratings: 54593000\n", "Actual number of ratings: 294763\n", "Sparsity of the data: 99.46007180407744\n", "\n", "[BPR] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 100/100 [00:01<00:00, 51.29it/s, correct=84.93%, skipped=0.81%]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Optimization finished!\n", "\n", "[BPR] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:02<00:00, 4182.97it/s]\n", "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:02<00:00, 4405.11it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[WBPR] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 100/100 [00:01<00:00, 50.09it/s, correct=50.72%, skipped=3.02%]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Optimization finished!\n", "\n", "[WBPR] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:02<00:00, 4141.18it/s]\n", "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:02<00:00, 4526.76it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[MF] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 100/100 [00:00<00:00, 228.21it/s, loss=1346.61]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Optimization finished!\n", "\n", "[MF] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:02<00:00, 4127.18it/s]\n", "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:02<00:00, 4469.11it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[WMF] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 155/155 [01:10<00:00, 2.20it/s, loss=301]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Learning completed!\n", "\n", "[WMF] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:12<00:00, 848.89it/s]\n", "Ranking: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:12<00:00, 876.97it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[NeuMF] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:46<00:00, 9.23s/it, loss=0.501]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[NeuMF] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:19<00:00, 542.52it/s]\n", "Ranking: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:18<00:00, 566.23it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[VAECF] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:06<00:00, 1.22s/it, loss=1.17]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[VAECF] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:03<00:00, 2717.43it/s]\n", "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:03<00:00, 2860.03it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[CVAECF] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [01:31<00:00, 1.84s/it, loss=0.93]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[CVAECF] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:05<00:00, 1832.24it/s]\n", "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:05<00:00, 1897.65it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[BiVAECF] Training started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [01:42<00:00, 2.06s/it, loss_i=0.441, loss_u=0.191]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[BiVAECF] Evaluation started!\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10561/10561 [00:02<00:00, 4216.96it/s]\n", "Ranking: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10534/10534 [00:02<00:00, 4669.25it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "VALIDATION:\n", "...\n", " | NCRR@10 | NDCG@10 | Recall@10 | Time (s)\n", "------- + ------- + ------- + --------- + --------\n", "BPR | 0.0377 | 0.0413 | 0.0468 | 2.3963\n", "WBPR | 0.0297 | 0.0333 | 0.0399 | 2.3315\n", "MF | 0.0040 | 0.0043 | 0.0042 | 2.3616\n", "WMF | 0.0489 | 0.0541 | 0.0632 | 12.0190\n", "NeuMF | 0.0013 | 0.0014 | 0.0015 | 18.6082\n", "VAECF | 0.0347 | 0.0383 | 0.0445 | 3.6877\n", "CVAECF | 0.0545 | 0.0615 | 0.0739 | 5.5564\n", "BiVAECF | 0.0002 | 0.0002 | 0.0002 | 2.2606\n", "\n", "TEST:\n", "...\n", " | NCRR@10 | NDCG@10 | Recall@10 | Train (s) | Test (s)\n", "------- + ------- + ------- + --------- + --------- + --------\n", "BPR | 0.0425 | 0.0456 | 0.0502 | 1.9605 | 2.5325\n", "WBPR | 0.0332 | 0.0365 | 0.0422 | 2.0041 | 2.5546\n", "MF | 0.0033 | 0.0035 | 0.0034 | 0.4536 | 2.5634\n", "WMF | 0.0533 | 0.0583 | 0.0669 | 70.6555 | 12.4469\n", "NeuMF | 0.0009 | 0.0011 | 0.0014 | 46.3940 | 19.4710\n", "VAECF | 0.0401 | 0.0427 | 0.0469 | 6.0933 | 3.8909\n", "CVAECF | 0.0601 | 0.0661 | 0.0770 | 91.9570 | 5.7691\n", "BiVAECF | 0.0005 | 0.0005 | 0.0005 | 103.3335 | 2.5094\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import cornac\n", "from cornac.eval_methods import RatioSplit\n", "from cornac.models import BPR, MF, NeuMF, VAECF, CVAECF, BiVAECF, LightGCN, WBPR, WMF\n", "from cornac.metrics import NCRR\n", "from cornac.data import GraphModality\n", "import pandas as pd\n", "\n", "# Assume data is a Cornac dataset object\n", "# data = cornac.data.Dataset.from_uir(your_data)\n", "\n", "# Model parameters\n", "LATENT_DIM = 50\n", "ENCODER_DIMS = [20]\n", "ACT_FUNC = \"tanh\"\n", "LIKELIHOOD = \"gaus\"\n", "NUM_EPOCHS = 5\n", "BATCH_SIZE = 128\n", "LEARNING_RATE = 0.01\n", "\n", "SEED=4567\n", "VERBOSE=True\n", "\n", "df = pd.read_csv('../data/rating_final.csv')\n", "data_list = df.values.tolist()\n", "\n", "eval_metrics = [\n", " cornac.metrics.Recall(k=10),\n", " cornac.metrics.NDCG(k=10),\n", " cornac.metrics.NCRR(k=10),\n", "]\n", "\n", "user_graph_modality = GraphModality(data=graph_modality_list)\n", "\n", "# Split the data\n", "ratio_split = RatioSplit(data=data_list, val_size=0.2, test_size=0.2, \n", " user_graph=user_graph_modality,\n", " exclude_unknowns=True, seed=SEED, verbose=True)\n", "\n", "# Define models\n", "models = [\n", " BPR(k=50, learning_rate=0.01, lambda_reg=0.01, max_iter=100),\n", " WBPR(k=50, max_iter=100, learning_rate=0.001, lambda_reg=0.01, verbose=True),\n", " MF(k=50, learning_rate=0.01, lambda_reg=0.01, max_iter=100),\n", " WMF(k=50, max_iter=155, a=1.0, b=0.1, learning_rate=0.00555, lambda_u=0.0155, lambda_v=0.0155,\n", " verbose=VERBOSE, seed=SEED),\n", " NeuMF(num_factors=50, layers=(64, 64, 32, 16), act_fn='relu', reg=0.01, num_epochs=5, \n", " batch_size=128, num_neg=4, lr=0.01, learner='adam', trainable=True, verbose=True, seed=SEED),\n", " VAECF(k=50, autoencoder_structure=[20], act_fn='tanh', likelihood='pois', n_epochs=5, batch_size=128),\n", " # LightGCN(seed=SEED,emb_size=64,num_epochs=5,num_layers=3,early_stopping={\"min_delta\": 1e-4, \"patience\": 50},batch_size=128,\n", " # learning_rate=0.01,lambda_reg=0.01,verbose=True),\n", " CVAECF(z_dim=50,h_dim=20,autoencoder_structure=[40],learning_rate=0.01,n_epochs = 50,batch_size = 128,seed = SEED),\n", " BiVAECF(k=LATENT_DIM,encoder_structure=ENCODER_DIMS,act_fn=ACT_FUNC,likelihood=LIKELIHOOD,n_epochs=50,batch_size=BATCH_SIZE,\n", " learning_rate=LEARNING_RATE,seed=SEED,trainable = True,use_gpu=True,verbose=True)\n", "]\n", "\n", "# Count the total number of unique users and unique businesses\n", "num_users = df['user_id'].nunique()\n", "num_businesses = df['business_id'].nunique()\n", "\n", "# Calculate the total number of possible ratings\n", "total_possible_ratings = num_users * num_businesses\n", "\n", "# Count the actual number of ratings in the dataset\n", "num_ratings = len(df)\n", "\n", "# Calculate the sparsity of the data\n", "sparsity = 1 - (num_ratings / total_possible_ratings)\n", "\n", "print(\"Total number of users:\", num_users)\n", "print(\"Total number of restaurants:\", num_businesses)\n", "print(\"Total possible ratings:\", total_possible_ratings)\n", "print(\"Actual number of ratings:\", num_ratings)\n", "print(\"Sparsity of the data:\", sparsity * 100)\n", "\n", "\n", "# Evaluate models\n", "cornac.Experiment(eval_method=ratio_split, models=models, metrics=eval_metrics, verbose=True).run()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e44e15b4-1127-4048-b429-895a1382ddfb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "91efdbb7-6e6c-491a-8ca3-5812429807fe", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 5 }