How to use Python to figure out why your friends have more friends than you

Allow me to demonstrate: The Friendship Paradox

Khuyen Tran

Do your friends, on average, have more friends than you? If you are an average person, there is a high chance that you have fewer friends than your friends.

This is called the friendship paradox. This phenomenon states that most people have fewer friends than their friends have, on average.

Greetings, humanoids

Subscribe to our newsletter now for a weekly recap of our favorite AI stories in your inbox.

To understand why the friend paradox exists, let’s start with a minimal example. We will create a network of people. Two people are friends if they are listed in the same Python tuple.

Now we will use Pyvis to visualize the network.

`pip install pyvis`

If you move the nodes around, you can see that Ben is the center of the circle of friends.

We are interested in finding what percentage of people in this network have fewer friends than their friends do on average. We will create multiple functions that help us answer this question. The functions are:

• Get friends of a specific person:

For example, Lauren’s friends are:

```>>> get_friends(friends, "Lauren")
['Khuyen', 'Ben']```
• Get the numbers of friends a specific person has:

```>>> num_friends_map = get_num_friends_map(friends)
>>> get_num_friends_of_a_person_friends(friends, "Lauren",
...                                     num_friends_map)

[3, 5]```

The result shows that Khuyen has 3 friends and Ben has 5 friends.

• Get the number of friends a person’s friends have on average:

```>>> get_average_friends_of_a_person_friends(friends, "Lauren")
4.0```

The output shows that the number of friends Lauren’s friends have on average is 4, which is higher than the number of friends she has.

If we observe the picture above carefully, we can see that Ben’s number of friends gives a boost to the number of friends Lauren’s friends have on average.

Since Ben has many friends, many of his friends will be in a similar situation as Lauren. In other words, the number of friends their friends have on average is higher than their number of friends because one or two of their friends are the influencers.

• Get the number of friends for all people in the network

In the table above,

• The column num_friends shows the number of friends a person has.
• The column avg_friends_of_friends shows the number of friends a person’s friends have on average.
• The column friends_have_more_friends indicates whether a person’s friends have more friends than himself/herself on average.

Let’s find out what percentage of people in the network have fewer friends than their friends have on average.

`0.8333333333333334`

The Facebook data consists of friend lists from Facebook. Facebook data was collected from survey participants, and the users in this data have been anonymized.

We will use the previous functions to get the number of friends people in the network have.

Let’s find out what percentage of people in the network have fewer friends than their friends have on average.

`0.874721465709334`

87% of people in the network have fewer friends than their friends have on average!

Visualize the influencers

Which nodes are the influencers in the network? Let’s visualize them using Pyvis.

We will define influencers as those who have more friends than their friends have on average. We will mark the nodes that are defined as influencers in red.

Add edges and show the network graph:

As we can see, the red nodes (people who have more friends than their friends have on average) tend to be in the center of the graph. If we move a red node, many blue nodes will move with it. This indicates that these red nodes are the influencers of one or two subgroups in the network.

Conclusion

Congratulations! You have just learned what the friend paradox is, and how to observe this paradox in Facebook data using Python. I hope this article will give you the motivation to observe other paradoxes around you using visualization and data science techniques.

Feel free to fork and play with the code for this article in this repo.

This article was originally published on Towards Data Science, you can find it here. Khuyen writes about basic data science concepts and enjoys playing with different algorithms and data science tools. You can connect with them on LinkedIn and Twitter.

Greetings Humanoids! Did you know we have a newsletter all about AI? You can subscribe to it right here.

See who you could meet in 2023!

Check out the TNW Conference 2022 post-show report →