SlideShare a Scribd company logo
Social Network Analysis With
Python
@ PyCon APAC 2014David Chiu
About Me
Co-founder of
Ex-Trend Micro Engineer
NumerInfo
ywchiu.com
Social Network
http://libeltyseo.com/wp-content/uploads/2013/03/social-networking.png
Human Nature
http://cdn.macado.com/assets/2010/03/peeping-tom.gif
What do we want to know?
Who knows whom, and which people are common to their social
networks?
How frequently are particular people communicating with one
another?
Which social network connections generate the most value for a
particular niche?
How does geography affect your social connections in an online
world?
Who are the most influential/popular people in a social network?
What are people chatting about (and is it valuable)?
What are people interested in based upon the human language that
they use in a digital world?
Explore Facebook
OAuth2 Flow
Open standard for authorization. OAuth provides a method for clients to
access server resources on behalf of a resource owner
Connect to Facebook
https://developers.facebook.com/
Get Access Token
https://developers.facebook.com/tools/explorer/
User Permission
Permission List
User Data Permissions:
user_hometown
user_location
user_interests
user_likes
user_relationships
Friends Data Permissions:
friends_hometown
friends_location
friends_interests
friends_likes
friends_relationships
Extended Permissions:
read_friendlists
Copy Token
Social Network Analysis With
Python
Let's Hack
Get Information From Facebook
Test On API Explorer
Required Packages
requests
Sending HTTP Request to Retrieve Data From Facebook
json
For Parsing JSON Format
Facebook Connect
import requests
import json
access_token="<access_token>"
url = "https://graph.facebook.com/me?access_token=%s"
response = requests.get(url%(access_token))
fb_data = json.loads(response.text)
print fb_data
Question:
Who Likes My Post The Most?
Get Likes Count of Posts
access_token = '<access_token>'
url="https://graph.facebook.com/me/posts?access_token=%s"
response = requests.get(url%(access_token))
fb_data = json.loads(response.text)
count_dic = {}
for post in fb_data['data']:
if 'likes' in post:
for rec in post['likes']['data']:
if rec['name'] in count_dic:
count_dic[rec['name']] += 1
else:
count_dic[rec['name']] = 1
Simple Ha!
Ask Harder Question!
Question:
What's People Talking About
Take Cross-Strait Agreement
As Example
keyword_dic = {}
posts_url = 'https://graph.facebook.com/%s/posts?access_token=%s'
post_response = rs.get(posts_url%(userid, access_token))
post_json = json.loads(post_response.text)
for post in post_json['data']:
if 'message' in post:
m = re.search('服貿', post['message'].encode('utf-8'))
if m:
if userid not in keyword_dic:
keyword_dic[userid] = 1
else:
keyword_dic[userid] += 1
PyCon APAC 2014 - Social Network Analysis Using Python (David Chiu)
Text Mining
NLTK!
Sorry! My Facebook Friends
Speak In Mandarin
Jieba!
Using Jieba For Word
Tokenization
import jieba
data = post_json['data']
dic = {}
for rec in post_json['data']:
if 'message' in rec:
seg_list = jieba.cut(rec['message'])
for seg in seg_list:
if seg in dic:
dic[seg] = dic[seg] + 1
else:
dic[seg] = 1
Question:
How to Identify Social Groups?
Required Packages
networkx
Analyze Social Network
community
Community Detection Using Louvain Method
Social Network
Man As , Connection AsNode Edge
Build Friendship Matrix
import networkx as nx
mutual_friends = {}
for friend in friends_obj['data']:
mutual_url = "https://graph.facebook.com/%s/mutualfriends?access_token=%s"
res = requests.get( mutual_url % (friend['id'], access_token) )
response_data = json.loads(res.text)['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] ]
Draw Network Plot
nx.draw(nxg)
Calculate Network Property
betweenness_centrality(nxg)
degree_centrality(nxg)
closeness_centrality(nxg)
Community Detection
import community
def find_partition(graph):
g = graph
partition = community.best_partition(g)
return partition
new_G = find_partition(nxg)
Draw Social Network
Communities
import matplotlib.pyplot as plt
size = float(len(set(new_G.values())))
pos = nx.spring_layout(nxg)
count = 0.
for com in set(new_G.values()) :
count = count + 1.
list_nodes = [nodes for nodes in new_G.keys()
if new_G[nodes] == com]
nx.draw_networkx_nodes(nxg, pos, list_nodes, node_size = 20,
node_color = str(count / size))
nx.draw_networkx_edges(nxg,pos, alpha=0.5)
plt.show()
Community Partitioned Plot
Gephi
Gephi, an open source graph visualization and manipulation software
One More Thing
To build your own data service
jsnetworkx
A JavaScript port of the NetworkX graph library.
juimee.com
THANK YOU

More Related Content

PPTX
Inference on the Semantic Web
PDF
Linked Data Technology and Status
PDF
新聞 X 謊言 用文字探勘挖掘財經新聞沒告訴你的真相(丘祐瑋)
PDF
Data Analysis - Making Big Data Work
PDF
(Tentative) Network Analysis with networkX : Fundamentals of network theory-2
PDF
Network Analysis with networkX : Real-World Example-2
PDF
Network Analysis with networkX : Real-World Example-1
PPTX
Network theory - PyCon 2015
Inference on the Semantic Web
Linked Data Technology and Status
新聞 X 謊言 用文字探勘挖掘財經新聞沒告訴你的真相(丘祐瑋)
Data Analysis - Making Big Data Work
(Tentative) Network Analysis with networkX : Fundamentals of network theory-2
Network Analysis with networkX : Real-World Example-2
Network Analysis with networkX : Real-World Example-1
Network theory - PyCon 2015

Viewers also liked (20)

PPTX
Communities and dynamics in social networks
PDF
Aviation Service Lifecycle Management
PDF
Network Analysis with networkX : Fundamentals of network theory-1
PPTX
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
PPTX
Big Data Analysis With RHadoop
PDF
Social Network Analysis With R
PDF
Machine Learning With R
PDF
Community Detection with Networkx
PPTX
Using Social Network Analysis to Assess Organizational Development Initiatives
PPTX
A Fast and Dirty Intro to NetworkX (and D3)
PPTX
A comparative study of social network analysis tools
PPTX
Facebook Brand Analysis - Strategic Brand Management
PPTX
Social Media Mining - Chapter 8 (Influence and Homophily)
PDF
Famissy_final presentation(chinese)_by Nina Wei
PPTX
R language tutorial
PDF
Subscriber Churn Prediction Model using Social Network Analysis In Telecommun...
PPT
Social network analysis course 2010 - 2011
PDF
Social network analysis & Big Data - Telecommunications and more
PPT
Hidden Markov Model & Stock Prediction
PDF
Modeling and mining complex networks with feature-rich nodes.
Communities and dynamics in social networks
Aviation Service Lifecycle Management
Network Analysis with networkX : Fundamentals of network theory-1
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Big Data Analysis With RHadoop
Social Network Analysis With R
Machine Learning With R
Community Detection with Networkx
Using Social Network Analysis to Assess Organizational Development Initiatives
A Fast and Dirty Intro to NetworkX (and D3)
A comparative study of social network analysis tools
Facebook Brand Analysis - Strategic Brand Management
Social Media Mining - Chapter 8 (Influence and Homophily)
Famissy_final presentation(chinese)_by Nina Wei
R language tutorial
Subscriber Churn Prediction Model using Social Network Analysis In Telecommun...
Social network analysis course 2010 - 2011
Social network analysis & Big Data - Telecommunications and more
Hidden Markov Model & Stock Prediction
Modeling and mining complex networks with feature-rich nodes.
Ad

Similar to PyCon APAC 2014 - Social Network Analysis Using Python (David Chiu) (20)

PPTX
Facebook and its development
PDF
Introduction to Facebook Python API
PDF
Facebook Python SDK - Introduction
PDF
Facebook data analysis using r
PDF
Mining Social Web APIs with IPython Notebook (Strata 2013)
PPTX
Social Media Data Collection & Analysis
PPTX
Accessing and analysing your own social media data.pptx
PDF
Statistical analysis of facebook using r
PDF
Facebook Open Graph API and How To Use It
PDF
Unleashing Twitter Data for Fun and Insight
PDF
Unleashing twitter data for fun and insight
PDF
Barcelona presentation reduced
PDF
Mining Social Web APIs with IPython Notebook (Data Day Texas 2015)
PDF
Q046049397
KEY
MTSW-Facebook-1
PPTX
CML's Presentation at FengChia University
PPTX
Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and ...
Facebook and its development
Introduction to Facebook Python API
Facebook Python SDK - Introduction
Facebook data analysis using r
Mining Social Web APIs with IPython Notebook (Strata 2013)
Social Media Data Collection & Analysis
Accessing and analysing your own social media data.pptx
Statistical analysis of facebook using r
Facebook Open Graph API and How To Use It
Unleashing Twitter Data for Fun and Insight
Unleashing twitter data for fun and insight
Barcelona presentation reduced
Mining Social Web APIs with IPython Notebook (Data Day Texas 2015)
Q046049397
MTSW-Facebook-1
CML's Presentation at FengChia University
Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and ...
Ad

Recently uploaded (20)

PDF
Live Echo Boost on TikTok_ Double Devices, Higher Ranks
PDF
A copy of a Medium article wishing Merry Christmas To All My Followers
PDF
The Edge You’ve Been Missing Get the Sociocosmos Edge
PPTX
Developing lesson plan gejegkavbw gagsgf
PDF
Mastering Social Media Marketing in 2025.pdf
PDF
StarNetCafeSB2012D3POYNagaworld2-Hotel-Casino-Phnom Entertainment
PPTX
Strategies for Social Media App Enhancement
PPTX
Result-Driven Social Media Marketing Services | Boost ROI
PPTX
Office Administration Courses in Trivandrum That Employers Value.pptx
PDF
Medium @mikehydes The Cryptomaster Home page
PDF
Presence That Pays Off Activate My Social Growth
PDF
Medium @mikehydes The Cryptomaster Story Stats
PDF
Medium @mikehydes The Cryptomaster Audience Stats
PDF
Medium @mikehydes The Cryptomaster About page
PDF
FINAL-Content-Marketing-Made-Easy-Workbook-Guied-Editable.pdf
PDF
Instagram Reels Growth Guide 2025.......
PDF
11111111111111111111111111111111111111111111111
PPTX
Types of Social Media Marketing for Business Success
PPTX
Table Top Exercise (TTEx) on Emergency.pptx
PDF
The Fastest Way to Look Popular Buy Reactions Today
Live Echo Boost on TikTok_ Double Devices, Higher Ranks
A copy of a Medium article wishing Merry Christmas To All My Followers
The Edge You’ve Been Missing Get the Sociocosmos Edge
Developing lesson plan gejegkavbw gagsgf
Mastering Social Media Marketing in 2025.pdf
StarNetCafeSB2012D3POYNagaworld2-Hotel-Casino-Phnom Entertainment
Strategies for Social Media App Enhancement
Result-Driven Social Media Marketing Services | Boost ROI
Office Administration Courses in Trivandrum That Employers Value.pptx
Medium @mikehydes The Cryptomaster Home page
Presence That Pays Off Activate My Social Growth
Medium @mikehydes The Cryptomaster Story Stats
Medium @mikehydes The Cryptomaster Audience Stats
Medium @mikehydes The Cryptomaster About page
FINAL-Content-Marketing-Made-Easy-Workbook-Guied-Editable.pdf
Instagram Reels Growth Guide 2025.......
11111111111111111111111111111111111111111111111
Types of Social Media Marketing for Business Success
Table Top Exercise (TTEx) on Emergency.pptx
The Fastest Way to Look Popular Buy Reactions Today

PyCon APAC 2014 - Social Network Analysis Using Python (David Chiu)