0

I have a dataset - around 30000 users and I want to calculate an influence of each user based on UserRank algorithm, which is based on PageRank: enter image description here

For each user I have a list of followers. I tried to calculate it in Python, but I'm getting:

RecursionError: maximum recursion depth exceeded while calling a Python object

Here's the code:

def calculate_user_rank(user_id):
    user_rank = 0
    for j in user[user_id]["followers"]:
        user_rank += (1 + (user[user_id]["followers_count"]/user[user_id]["tweets"]) * calculate_user_rank(j))/user[j]["followers_count"]
    return user_rank

Is there any way how to calculate this measure for each user?

Yakimget
  • 25
  • 4

2 Answers2

0

One way to deal with the problem is to utilise the concepts of dynamic programming, because you might be repetitively solving same sub-problem over and over. So, its better to store the result of the page ranks of user rather than calculating them again and again.

Use:

def calculate_user_rank(user_id, memo={}):
    user_rank = 0
    for j in user[user_id]["followers"]:
        if j not in memo:
            memo[j] = calculate_user_rank(j, memo)

        user_rank += (1 + (user[user_id]["followers_count"]/user[user_id]["tweets"]) * memo[j])/user[j]["followers_count"]
    memo[user_id] = user_rank

    return memo[user_id]
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
0

You can always change the default recursion limit: What is the maximum recursion depth in Python, and how to increase it? But in this example I would prefer iterative solution for your problem (like here: https://github.com/thomasjhuang/PageRank/blob/master/run_pagerank.py) and cache for each user his score for performance.

Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24