码迷,mamicode.com
首页 > 编程语言 > 详细

Python 对Facebook用户Friendship的聚类分析

时间:2014-08-14 08:17:08      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:数据挖掘   facebook   

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的聚类分析,布布扣,bubuko.com

Python 对Facebook用户Friendship的聚类分析

标签:数据挖掘   facebook   

原文地址:http://blog.csdn.net/guaguastd/article/details/38552881

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!