Files
004_comission/tunmnlu/task_04/hw4-skeleton/Q1/Q1.ipynb
louiscklaw 0687d83118 update,
2025-01-31 22:22:19 +08:00

257 lines
8.4 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# DVA HW4 Q1\n",
"## Pagerank Algorithm\n",
"\n",
"Do not distribute or publish this code \n",
"Do not change the `#export` statements or add and other code or comments above them. They are needed for grading."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"'''\n",
"*** Imports ***\n",
" DO NOT EDIT or add anything to this section\n",
"'''\n",
"import numpy as np\n",
"import time\n",
"import argparse\n",
"import sys"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Update your GT username and Id"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def author(): \n",
" return \"gburdell3\" # replace gburdell3 with your Georgia Tech username. \n",
" \n",
"def gtid(): \n",
" return 987654321 # replace with your GT ID number "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'''\n",
"*** Utility function ***\n",
" DO NOT EDIT\n",
"'''\n",
"def dump_results(command, iterations, result):\n",
" print(\"Sorting...\", file=sys.stderr)\n",
" sorted_result = sorted(enumerate(result), key=lambda x: x[1], reverse=True)\n",
" output_result = \"node_id\\tpr_value\\n\"\n",
" for node_id, pr_value in sorted_result[:10]:\n",
" output_result += \"{0}\\t{1}\\n\".format(node_id, pr_value)\n",
" print(output_result)\n",
"\n",
" with open(command+'_iter'+str(iterations)+\".txt\",\"w\") as output_file:\n",
" output_file.write(output_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PageRank Class\n",
"Please add your code as indicated below \n",
"You do not need to add code outside of the indicated areas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"class PageRank:\n",
" def __init__(self, edge_file):\n",
"\n",
" self.node_degree = {}\n",
" self.max_node_id = 0\n",
" self.edge_file = edge_file\n",
"\n",
" def read_edge_file(self, edge_file):\n",
" with open(edge_file) as f:\n",
" for line in f:\n",
" val = line.split('\\t')\n",
" yield int(val[0]), int(val[1])\n",
"\n",
" \"\"\"\n",
" Step1: Calculate the out-degree of each node and maximum node_id of the graph.\n",
" Store the out-degree in class variable \"node_degree\" and maximum node id to \"max_node_id\".\n",
" \"\"\"\n",
" def calculate_node_degree(self):\n",
" for source,target in self.read_edge_file(self.edge_file):\n",
"\n",
" ### STEP 1\n",
" ### Implement your code here\n",
" #############################################\n",
" pass\n",
"\n",
" #############################################\n",
"\n",
" print(\"Max node id: {}\".format(self.max_node_id))\n",
" print(\"Total source number: {}\".format(len(self.node_degree)))\n",
"\n",
" def get_max_node_id(self):\n",
" return self.max_node_id\n",
"\n",
" def run_pagerank(self, node_weights, damping_factor=0.85, iterations=10):\n",
"\n",
" pr_values = [1.0 / (self.max_node_id + 1)] * (self.max_node_id + 1)\n",
" start_time = time.time()\n",
" \"\"\" \n",
" Step2: Implement pagerank algorithm as mentioned in lecture slides and the question.\n",
"\n",
" Incoming Parameters:\n",
" node_weights: Probability of each node to flyout during random walk\n",
" damping_factor: Probability of continuing on the random walk\n",
" iterations: Number of iterations to run the algorithm \n",
" check the __main__ function to understand node_weights and max_node_id\n",
" \n",
" Use the calculated out-degree to calculate the pagerank value of each node\n",
" \"\"\"\n",
" for it in range(iterations):\n",
" \n",
" new_pr_values = [0.0] * (self.max_node_id + 1)\n",
" for source, target in self.read_edge_file(self.edge_file):\n",
"\n",
" ### STEP 2\n",
" ### Implement your code here\n",
" #############################################\n",
" pass\n",
"\n",
" #############################################\n",
"\n",
" print (\"Completed {0}/{1} iterations. {2} seconds elapsed.\".format(it + 1, iterations, time.time() - start_time))\n",
"\n",
" return pr_values"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simplified Pagerank"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Options\n",
"file = 'network.tsv' # Input file of the edge list - DO NOT EDIT\n",
"command = 'simplified_pagerank' # Command to run - DO NOT EDIT\n",
"damping_factor = 0.85 # Damping factor - submit results for damping_factor = 0.85\n",
"iterations = [10,25] # Number of iterations - sumbit results for iterations = [10,25] \n",
"\n",
"# Run Simplified PageRank\n",
"# DO NOT EDIT\n",
"for i in iterations:\n",
" pr = PageRank(file)\n",
" pr.calculate_node_degree()\n",
" max_node_id = pr.get_max_node_id()\n",
" node_weights = np.ones(max_node_id + 1) / (max_node_id + 1)\n",
" result = pr.run_pagerank(node_weights=node_weights, iterations=i, damping_factor=damping_factor)\n",
" dump_results(command, i, result )"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Personalized Pagerank"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Options\n",
"file = 'network.tsv' # Input file of the edge list - DO NOT EDIT\n",
"command = 'personalized_pagerank' # Command to run - DO NOT EDIT\n",
"damping_factor = 0.85 # Damping factor - submit results for damping_factor = 0.85\n",
"iterations = [10,25] # Number of iterations - sumbit results for iterations = [10,25] \n",
"\n",
"# Run Personalized PageRank\n",
"# DO NOT EDIT\n",
"for i in iterations:\n",
" pr = PageRank(file)\n",
" pr.calculate_node_degree()\n",
" max_node_id = pr.get_max_node_id()\n",
" np.random.seed(gtid())\n",
" node_weights = np.random.rand(max_node_id + 1)\n",
" node_weights = node_weights/node_weights.sum()\n",
" result = pr.run_pagerank(node_weights=node_weights, iterations=i, damping_factor=damping_factor)\n",
" dump_results(command, i, result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Submitting your results to gradescope\n",
"Submit the following on Gradescope \n",
"* This file, Q1.ipynb\n",
"* simplified_pagerank_iter10.txt\n",
"* simplified_pagerank_iter25.txt\n",
"* personalized_pagerank_iter10.txt\n",
"* personalized_pagerank_iter25.txt\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "DVAHW4",
"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.7.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}