Python 对Facebook用户Friendship的聚类分析

CODE:

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2014-8-13
@author: guaguastd
@name: friendship_graph_construct.py
'''

import networkx as nx
import requests
import json

ACCESS_TOKEN = ‘'

# impot login
from login import facebook_login

# access to facebook
facebook_api = facebook_login()

friends = [ (friend['id'], friend['name'],)
              for friend in facebook_api.get_connections('me', 'friends')['data'] ]

url = 'https://graph.facebook.com/me/mutualfriends/%s?access_token=%s'
mutual_friends = {}

# spawn a separate request for each iteration
for friend_id, friend_name in friends:
    r = requests.get(url % (friend_id, ACCESS_TOKEN,), verify=False )
    response_data = json.loads(r.content)['data']
    mutual_friends[friend_name] = [ data['name']
                                    for data in response_data ]

nxg = nx.Graph()
[ nxg.add_edge('me', mf) for mf in mutual_friends ]
[ nxg.add_edge(f1, f2)
  for f1 in mutual_friends
    for f2 in mutual_friends[f1]]

cliques = [c for c in nx.find_cliques(nxg)]
num_cliques = len(cliques)

clique_sizes = [len(c) for c in cliques]
max_clique_size = max(clique_sizes)
avg_clique_size = sum(clique_sizes) / num_cliques

max_cliques = [c for c in cliques if len(c) == max_clique_size]
num_max_cliques = len(max_cliques)

max_clique_sets = [set(c) for c in max_cliques]
friends_in_all_max_cliques = list(reduce(lambda x, y: x.intersection(y), max_clique_sets))

print 'Num cliques:', num_cliques
print 'Avg clique size:',avg_clique_size
print 'Max clique size:', max_clique_size
print 'Num max cliques:', num_max_cliques
print
print 'Frieds in all max cliques:'
print json.dumps(friends_in_all_max_cliques, indent=1)
print
print 'Max cliques:'
print json.dumps(max_cliques, indent=1)

RESULT:

Num cliques: 11
Avg clique size: 2
Max clique size: 3
Num max cliques: 6

Frieds in all max cliques:
[
 "me"
]

Max cliques:
[
 [
  "me",
  "Soofi Cat",
  "Nanu Soria"
 ],
 [
  "me",
  "Jorge Rodriguez",
  "Ayel\u00e9n Basualdo"
 ],
 [
  "me",
  "Jorge Rodriguez",
  "\u5434\u57ce"
 ],
 [
  "me",
  "Jorge Rodriguez",
  "Wanda Bianchi"
 ],
 [
  "me",
  "Jorge Rodriguez",
  "Jeniifer Mendez"
 ],
 [
  "me",
  "Jorge Rodriguez",
  "Karen Csr"
 ]
]

Python 对Facebook用户Friendship的聚类分析

时间: 2024-10-21 14:22:11

Python 对Facebook用户Friendship的聚类分析的相关文章

Python 获取Facebook用户的Friends的爱好

Code: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-11 @author: guaguastd @name: friends_like_query.py ''' # impot login from login import facebook_login # import helper from helper import pp # access to facebook facebook_api = face

Python 获取Facebook用户与其有共同爱好的Friends

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-13 @author: guaguastd @name: common_friends_finding.py ''' # impot login from login import facebook_login # import helper #from helper import pp # import PrettyTable from prettytab

Python 获取Facebook用户的Friends的爱好中的Top10

CODE; #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: friends_popular_likes.py ''' # impot login from login import facebook_login # import helper from helper import pp # calculating the most popular likes

Python 获取Facebook用户Friends的爱好类别中的Top10

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: friends_popular_category.py ''' # impot login from login import facebook_login # import helper #from helper import pp # calculating the most popular ca

Python 统计Facebook用户爱好的个数

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: friends_likes_number.py ''' # impot login from login import facebook_login # import helper #from helper import pp # import itemgetter from operator imp

Python 获取Facebook特定用户的粉丝数

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-9 @author: guaguastd @name: fan_pages_compare.py ''' # impot login from login import facebook_login # import helper from helper import pp, int_format # access to facebook facebook_

Python 获取Facebook特定用户的feed和link

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-9 @author: guaguastd @name: facebook_connection_query.py ''' # impot login from login import facebook_login # import helper from helper import pp # access to facebook facebook_api

Python 爬行Twitter用户的Friendship图

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-1 @author: guaguastd @name: crawing_friendship_graph.py ''' if __name__ == '__main__': # import json #import json # import search from search import search_for_tweet # import get_f

Python 对LinkedIn用户联系人的地址进行地理编码

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-20 @author: guaguastd @name: geocode_connection_bing.py ''' from geopy import geocoders import json GEO_APP_KEY = '' g = geocoders.Bing(GEO_APP_KEY) # access to linkedin api from l