Python's cluster analysis of Facebook user friendship

Source: Internet
Author: User

CODE:

#!/usr/bin/python #-*-Coding:utf-8-*-"Created on 2014-8-13@author:guaguastd@name:friendship_graph_construct.py '  ' Import networkx as nximport requestsimport jsonaccess_token = ' # impot loginfrom login Import facebook_login# access to Facebookfacebook_api = Facebook_login () friends = [(friend[' id '], friend[' name '],) for friend in Facebook_a Pi.get_connections (' Me ', ' friends ') [' data ']]url = ' https://graph.facebook.com/me/mutualfriends/%s?access_token=% S ' mutual_friends = {}# spawn a separate request for each iterationfor friend_id, friend_name in friends:r = REQUESTS.G ET (url% (friend_id, Access_token,), verify=false) Response_data = Json.loads (r.content) [' Data '] Mutual_friends[fri End_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 M Utual_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 = su M (clique_sizes)/num_cliquesmax_cliques = [C for C in cliques if Len (c) = = Max_clique_size]num_max_cliques = Len (max_cliq UES) 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_cliquesprint ' Avg clique size: ', Avg_clique_sizeprint ' max clique size: ', MA X_clique_sizeprint ' Num max cliques: ', Num_max_cliquesprintprint ' frieds in all max cliques: ' Print json.dumps (friends_in _all_max_cliques, indent=1) Printprint ' max cliques: ' Print json.dumps (max_cliques, Indent=1)

RESULT:

Num Cliques:11avg clique Size:2max clique size:3num Max Cliques:6frieds 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 "]


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.