{ "cells": [ { "cell_type": "code", "execution_count": 5, "id": "fa4cc168", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2026-06-22 12:26:10,379 - INFO - PHASE 1: Loading data...\n", "2026-06-22 12:26:25,130 - INFO - PHASE 2: Filtering (User >= 20, Item >= 200)...\n", "2026-06-22 12:26:39,908 - INFO - Filtreleme sonrası kalan satır: 35,007,076\n", "2026-06-22 12:26:39,909 - INFO - PHASE 3: Performing random sampling (100000 users, 10000 items)...\n", "2026-06-22 12:26:45,480 - INFO - PHASE 4: Saving to Parquet format...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "=============================================\n", "MSD FINAL SAMPLING REPORT\n", "=============================================\n", "Number of Users : 100,000\n", "Number of Items : 10,000 (Effective: 10,000)\n", "Number of Interactions : 1,282,657\n", "Sparsity : 0.1283%\n", "=============================================\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2026-06-22 12:26:45,744 - INFO - Successfully saved to: C:\\Users\\Hakan YILMAZER\\Downloads\\msd_ready_sampled.parquet\n" ] } ], "source": [ "import pandas as pd\n", "import pyarrow as pa\n", "import pyarrow.parquet as pq\n", "import os\n", "import zipfile\n", "import logging\n", "import numpy as np\n", "\n", "# Configure logging\n", "logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n", "\n", "def extract_and_filter(min_uc=20, min_sc=200, sample_user=100000, sample_item=10000):\n", " # Set your local paths\n", " base_dir = r\"C:\\Users\\Hakan YILMAZER\\Downloads\"\n", " txt_file = os.path.join(base_dir, \"msd_raw\", \"train_triplets.txt\")\n", " parquet_output_path = os.path.join(base_dir, \"msd_ready_sampled.parquet\")\n", "\n", " # 1. Load Data\n", " logging.info(\"PHASE 1: Loading data...\")\n", " df = pd.read_csv(txt_file, sep='\\t', header=None, names=['userId', 'movieId', 'rating'])\n", " df['rating'] = 1 # Implicit feedback binarization\n", "\n", " # 2. Filter First (Establish the stable core)\n", " logging.info(f\"PHASE 2: Filtering (User >= {min_uc}, Item >= {min_sc})...\")\n", " user_counts = df.groupby('userId').size()\n", " item_counts = df.groupby('movieId').size()\n", " \n", " df = df[df['userId'].isin(user_counts[user_counts >= min_uc].index)]\n", " df = df[df['movieId'].isin(item_counts[item_counts >= min_sc].index)]\n", " logging.info(f\"Filtreleme sonrası kalan satır: {len(df):,}\")\n", "\n", " # 3. Random Sampling (Perform on the high-quality core)\n", " logging.info(f\"PHASE 3: Performing random sampling ({sample_user} users, {sample_item} items)...\")\n", " \n", " unique_items = df['movieId'].unique()\n", " chosen_items = np.random.choice(unique_items, size=min(sample_item, len(unique_items)), replace=False)\n", " df = df[df['movieId'].isin(chosen_items)]\n", " \n", " unique_users = df['userId'].unique()\n", " chosen_users = np.random.choice(unique_users, size=min(sample_user, len(unique_users)), replace=False)\n", " df = df[df['userId'].isin(chosen_users)]\n", "\n", " # 4. Final Reporting\n", " num_users = df['userId'].nunique()\n", " num_items = df['movieId'].nunique()\n", " num_ratings = len(df)\n", " \n", " # Check for padding requirement\n", " if num_items % 2 != 0:\n", " effective_num_items = num_items + 1\n", " else:\n", " effective_num_items = num_items\n", "\n", " # Sparsity calculation\n", " sparsity = (num_ratings / (num_users * effective_num_items))\n", " \n", " print(\"\\n\" + \"=\"*45)\n", " print(\"MSD FINAL SAMPLING REPORT\")\n", " print(\"=\"*45)\n", " print(f\"Number of Users : {num_users:,}\")\n", " print(f\"Number of Items : {num_items:,} (Effective: {effective_num_items:,})\")\n", " print(f\"Number of Interactions : {num_ratings:,}\")\n", " print(f\"Sparsity : {sparsity * 100:.4f}%\")\n", " print(\"=\"*45 + \"\\n\")\n", "\n", " # 5. Save to Parquet\n", " logging.info(\"PHASE 4: Saving to Parquet format...\")\n", " table = pa.Table.from_pandas(df, preserve_index=False)\n", " pq.write_table(table, parquet_output_path, compression='snappy')\n", " logging.info(f\"Successfully saved to: {parquet_output_path}\")\n", "\n", "if __name__ == \"__main__\":\n", " # Ensure reproducibility with seed\n", " np.random.seed(98765)\n", " extract_and_filter()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }