File size: 7,107 Bytes
0e490a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import math\n",
"import pickle as pkl\n",
"\n",
"from torch_geometric.data import Data\n",
"import torch\n",
"import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_5327/3476166533.py:2: DtypeWarning: Columns (2,3,4,6,7,8,9,14,17,18,21) have mixed types. Specify dtype option on import or set low_memory=False.\n",
" df = pd.read_csv(\"reddit.csv\")\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1070077, 22)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_5327/3476166533.py:18: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df_graph.rename(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(256388, 8)\n"
]
}
],
"source": [
"## Preprocessing\n",
"df = pd.read_csv(\"reddit.csv\")\n",
"print(df.shape)\n",
"\n",
"# select columns\n",
"df_graph = df[\n",
" [\n",
" \"subreddit_id\",\n",
" \"subreddit\",\n",
" \"name\",\n",
" \"body\",\n",
" \"score\",\n",
" \"author\",\n",
" \"author_flair_text\",\n",
" \"distinguished\",\n",
" ]\n",
"]\n",
"df_graph.rename(\n",
" columns={\n",
" \"name\": \"post_id\",\n",
" \"body\": \"post\",\n",
" \"author\": \"user\",\n",
" \"author_flair_text\": \"user_flair\",\n",
" },\n",
" inplace=True,\n",
" errors=\"raise\",\n",
")\n",
"\n",
"# drop na, duplicates and deleted post\n",
"df_graph = df_graph.drop_duplicates()\n",
"df_graph = df_graph[df_graph[\"post\"] != \"[deleted]\"]\n",
"df_graph = df_graph.dropna(subset=[\"post_id\"])\n",
"df_graph = df_graph.dropna(subset=[\"user_flair\"])\n",
"df_graph = df_graph.dropna(subset=[\"subreddit\"])\n",
"df_graph = df_graph.dropna(subset=[\"post\"])\n",
"print(df_graph.shape)\n",
"\n",
"df_graph[\"distinguished\"] = df_graph[\"distinguished\"].apply(\n",
" lambda x: \"ordinary\" if pd.isna(x) else \"distinguished\"\n",
")\n",
"df_graph[\"user_flair\"] = df_graph[\"user_flair\"].apply(lambda x: \"\" if pd.isna(x) else x)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>subreddit_id</th>\n",
" <th>subreddit</th>\n",
" <th>post_id</th>\n",
" <th>post</th>\n",
" <th>score</th>\n",
" <th>user</th>\n",
" <th>user_flair</th>\n",
" <th>distinguished</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>t5_2qhon</td>\n",
" <td>comicbooks</td>\n",
" <td>t1_cqug9dk</td>\n",
" <td>It's not contradictory. Snyder's rendition of ...</td>\n",
" <td>1.0</td>\n",
" <td>eskimo_bros</td>\n",
" <td>Luke Cage</td>\n",
" <td>ordinary</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subreddit_id subreddit post_id \\\n",
"3 t5_2qhon comicbooks t1_cqug9dk \n",
"\n",
" post score user \\\n",
"3 It's not contradictory. Snyder's rendition of ... 1.0 eskimo_bros \n",
"\n",
" user_flair distinguished \n",
"3 Luke Cage ordinary "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = df_graph\n",
"df.head(1)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"256388it [00:15, 17023.98it/s]\n"
]
}
],
"source": [
"text_nodes = []\n",
"node_labels = []\n",
"sub_id2idx = {}\n",
"sub_nodes = []\n",
"user_id2idx = {} \n",
"user_nodes = []\n",
"count = 0\n",
"text_edges = []\n",
"for _, row in tqdm.tqdm(df.iterrows()):\n",
" sub_id = str(row[\"subreddit\"])\n",
" user_id = str(row['user'])\n",
"\n",
" if sub_id not in sub_id2idx:\n",
" sub_id2idx[sub_id] = count\n",
" sub_nodes.append(count)\n",
" count += 1\n",
" else:\n",
" sub_nodes.append(sub_id2idx[sub_id])\n",
" text_nodes.append(f\"subreddit {sub_id}\")\n",
" node_labels.append(-1)\n",
"\n",
" if user_id not in user_id2idx:\n",
" user_id2idx[user_id] = count\n",
" user_nodes.append(count)\n",
" count += 1\n",
" else:\n",
" user_nodes.append(user_id2idx[user_id])\n",
" text_nodes.append(f\"user {user_id} has flair {row['user_flair']}\")\n",
" node_labels.append(row['distinguished'])\n",
" text_edges.append(str(row['post']))\n",
" \n",
"\n",
"## Save it as torch data\n",
"graph = Data(\n",
" text_nodes=text_nodes,\n",
" text_edges=text_edges,\n",
" node_labels=node_labels,\n",
" edge_index=torch.tensor([user_nodes, sub_nodes], dtype=torch.long),\n",
")\n",
"\n",
"with open(\"../processed/reddit.pkl\", \"wb\") as file:\n",
" pkl.dump(graph, file)"
]
}
],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|