SlideShare a Scribd company logo
1 of 41
Download to read offline
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
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

Viewers also liked

Communities and dynamics in social networks
Communities and dynamics in social networksCommunities and dynamics in social networks
Communities and dynamics in social networksFrancisco Restivo
 
Aviation Service Lifecycle Management
Aviation Service Lifecycle ManagementAviation Service Lifecycle Management
Aviation Service Lifecycle ManagementMichael Denis
 
Network Analysis with networkX : Fundamentals of network theory-1
Network Analysis with networkX : Fundamentals of network theory-1Network Analysis with networkX : Fundamentals of network theory-1
Network Analysis with networkX : Fundamentals of network theory-1Kyunghoon Kim
 
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...Xiaohan Zeng
 
Big Data Analysis With RHadoop
Big Data Analysis With RHadoopBig Data Analysis With RHadoop
Big Data Analysis With RHadoopDavid Chiu
 
Social Network Analysis With R
Social Network Analysis With RSocial Network Analysis With R
Social Network Analysis With RDavid Chiu
 
Machine Learning With R
Machine Learning With RMachine Learning With R
Machine Learning With RDavid Chiu
 
Community Detection with Networkx
Community Detection with NetworkxCommunity Detection with Networkx
Community Detection with NetworkxErika Fille Legara
 
Using Social Network Analysis to Assess Organizational Development Initiatives
Using Social Network Analysis to Assess Organizational Development InitiativesUsing Social Network Analysis to Assess Organizational Development Initiatives
Using Social Network Analysis to Assess Organizational Development InitiativesStephanie Richter
 
A Fast and Dirty Intro to NetworkX (and D3)
A Fast and Dirty Intro to NetworkX (and D3)A Fast and Dirty Intro to NetworkX (and D3)
A Fast and Dirty Intro to NetworkX (and D3)Lynn Cherny
 
A comparative study of social network analysis tools
A comparative study of social network analysis toolsA comparative study of social network analysis tools
A comparative study of social network analysis toolsDavid Combe
 
Facebook Brand Analysis - Strategic Brand Management
Facebook Brand Analysis - Strategic Brand ManagementFacebook Brand Analysis - Strategic Brand Management
Facebook Brand Analysis - Strategic Brand ManagementNuwan Ireshinie
 
Social Media Mining - Chapter 8 (Influence and Homophily)
Social Media Mining - Chapter 8 (Influence and Homophily)Social Media Mining - Chapter 8 (Influence and Homophily)
Social Media Mining - Chapter 8 (Influence and Homophily)SocialMediaMining
 
Famissy_final presentation(chinese)_by Nina Wei
Famissy_final presentation(chinese)_by Nina WeiFamissy_final presentation(chinese)_by Nina Wei
Famissy_final presentation(chinese)_by Nina WeiNina (Zhuxiaona) Wei
 
R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 
Subscriber Churn Prediction Model using Social Network Analysis In Telecommun...
Subscriber Churn Prediction Model using Social Network Analysis In Telecommun...Subscriber Churn Prediction Model using Social Network Analysis In Telecommun...
Subscriber Churn Prediction Model using Social Network Analysis In Telecommun...BAINIDA
 
Social network analysis course 2010 - 2011
Social network analysis course 2010 - 2011Social network analysis course 2010 - 2011
Social network analysis course 2010 - 2011guillaume ereteo
 
Social network analysis & Big Data - Telecommunications and more
Social network analysis & Big Data - Telecommunications and moreSocial network analysis & Big Data - Telecommunications and more
Social network analysis & Big Data - Telecommunications and moreWael Elrifai
 
Hidden Markov Model & Stock Prediction
Hidden Markov Model & Stock PredictionHidden Markov Model & Stock Prediction
Hidden Markov Model & Stock PredictionDavid Chiu
 
Modeling and mining complex networks with feature-rich nodes.
Modeling and mining complex networks with feature-rich nodes.Modeling and mining complex networks with feature-rich nodes.
Modeling and mining complex networks with feature-rich nodes.Corrado Monti
 

Viewers also liked (20)

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

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

GSoC 2017 Proposal - Chatbot for DBpedia
GSoC 2017 Proposal - Chatbot for DBpedia GSoC 2017 Proposal - Chatbot for DBpedia
GSoC 2017 Proposal - Chatbot for DBpedia Ram G Athreya
 
Buiding application for social networks
Buiding application for social networksBuiding application for social networks
Buiding application for social networksĐỗ Duy Trung
 
SRS Of Social Networking
SRS Of Social NetworkingSRS Of Social Networking
SRS Of Social Networkingmaaano786
 
Unleashing Twitter Data for Fun and Insight
Unleashing Twitter Data for Fun and InsightUnleashing Twitter Data for Fun and Insight
Unleashing Twitter Data for Fun and InsightMatthew Russell
 
Unleashing twitter data for fun and insight
Unleashing twitter data for fun and insightUnleashing twitter data for fun and insight
Unleashing twitter data for fun and insightDigital Reasoning
 
IEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia Communities
IEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia CommunitiesIEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia Communities
IEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia CommunitiesKalman Graffi
 
Live Social Semantics @ ESWC2010
Live Social Semantics @ ESWC2010Live Social Semantics @ ESWC2010
Live Social Semantics @ ESWC2010Martin Szomszor
 
Measureable Knowledge Management
Measureable Knowledge ManagementMeasureable Knowledge Management
Measureable Knowledge ManagementPeter H. Reiser
 
OSINT using Twitter & Python
OSINT using Twitter & PythonOSINT using Twitter & Python
OSINT using Twitter & Python37point2
 
Defrag: Pulling the Threads on User Data
Defrag: Pulling the Threads on User DataDefrag: Pulling the Threads on User Data
Defrag: Pulling the Threads on User Datadaniela barbosa
 
myExperiment - Defining the Social Virtual Research Environment
myExperiment - Defining the Social Virtual Research EnvironmentmyExperiment - Defining the Social Virtual Research Environment
myExperiment - Defining the Social Virtual Research EnvironmentDavid De Roure
 
Goodle Developer Days Munich 2008 - Open Social Update
Goodle Developer Days Munich 2008 - Open Social UpdateGoodle Developer Days Munich 2008 - Open Social Update
Goodle Developer Days Munich 2008 - Open Social UpdatePatrick Chanezon
 
How to build the Open Mesh
How to build the Open MeshHow to build the Open Mesh
How to build the Open MeshMarc Canter
 

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

GSoC 2017 Proposal - Chatbot for DBpedia
GSoC 2017 Proposal - Chatbot for DBpedia GSoC 2017 Proposal - Chatbot for DBpedia
GSoC 2017 Proposal - Chatbot for DBpedia
 
tweet segmentation
tweet segmentation tweet segmentation
tweet segmentation
 
Buiding application for social networks
Buiding application for social networksBuiding application for social networks
Buiding application for social networks
 
SRS Of Social Networking
SRS Of Social NetworkingSRS Of Social Networking
SRS Of Social Networking
 
Unleashing Twitter Data for Fun and Insight
Unleashing Twitter Data for Fun and InsightUnleashing Twitter Data for Fun and Insight
Unleashing Twitter Data for Fun and Insight
 
Unleashing twitter data for fun and insight
Unleashing twitter data for fun and insightUnleashing twitter data for fun and insight
Unleashing twitter data for fun and insight
 
Project
Project Project
Project
 
IEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia Communities
IEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia CommunitiesIEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia Communities
IEEE ISM 2008: Kalman Graffi: A Distributed Platform for Multimedia Communities
 
final ppt.pptx
final ppt.pptxfinal ppt.pptx
final ppt.pptx
 
final ppt.pptx
final ppt.pptxfinal ppt.pptx
final ppt.pptx
 
Enabling Citizen-empowered Apps over Linked Data
Enabling Citizen-empowered Apps over Linked DataEnabling Citizen-empowered Apps over Linked Data
Enabling Citizen-empowered Apps over Linked Data
 
Live Social Semantics @ ESWC2010
Live Social Semantics @ ESWC2010Live Social Semantics @ ESWC2010
Live Social Semantics @ ESWC2010
 
Measureable Knowledge Management
Measureable Knowledge ManagementMeasureable Knowledge Management
Measureable Knowledge Management
 
SDoW2010 keynote
SDoW2010 keynoteSDoW2010 keynote
SDoW2010 keynote
 
OSINT using Twitter & Python
OSINT using Twitter & PythonOSINT using Twitter & Python
OSINT using Twitter & Python
 
Yahoo Open Platform Stack
Yahoo Open Platform StackYahoo Open Platform Stack
Yahoo Open Platform Stack
 
Defrag: Pulling the Threads on User Data
Defrag: Pulling the Threads on User DataDefrag: Pulling the Threads on User Data
Defrag: Pulling the Threads on User Data
 
myExperiment - Defining the Social Virtual Research Environment
myExperiment - Defining the Social Virtual Research EnvironmentmyExperiment - Defining the Social Virtual Research Environment
myExperiment - Defining the Social Virtual Research Environment
 
Goodle Developer Days Munich 2008 - Open Social Update
Goodle Developer Days Munich 2008 - Open Social UpdateGoodle Developer Days Munich 2008 - Open Social Update
Goodle Developer Days Munich 2008 - Open Social Update
 
How to build the Open Mesh
How to build the Open MeshHow to build the Open Mesh
How to build the Open Mesh
 

Recently uploaded

GRACE ANDREWS the future of podcasting.pdf
GRACE ANDREWS the future of podcasting.pdfGRACE ANDREWS the future of podcasting.pdf
GRACE ANDREWS the future of podcasting.pdfDIGGIT
 
This is a test presentation to see if this works
This is a test presentation to see if this worksThis is a test presentation to see if this works
This is a test presentation to see if this worksjonnygharris
 
The Colossal Studio Events Packages.pdf
The Colossal Studio  Events Packages.pdfThe Colossal Studio  Events Packages.pdf
The Colossal Studio Events Packages.pdfshybree
 
MalluProgrammers. Careeradvice.Webinarppt.pdf
MalluProgrammers. Careeradvice.Webinarppt.pdfMalluProgrammers. Careeradvice.Webinarppt.pdf
MalluProgrammers. Careeradvice.Webinarppt.pdfgouravsarath45
 
Social Media Marketing Company Coimbatore
Social Media Marketing Company CoimbatoreSocial Media Marketing Company Coimbatore
Social Media Marketing Company Coimbatorecogniitec
 
Maximize Your Pinterest Reach....................
Maximize Your Pinterest Reach....................Maximize Your Pinterest Reach....................
Maximize Your Pinterest Reach....................SocioCosmos
 
Unlock Facebook Success with Sociocosmos
Unlock Facebook Success with SociocosmosUnlock Facebook Success with Sociocosmos
Unlock Facebook Success with SociocosmosSocioCosmos
 

Recently uploaded (7)

GRACE ANDREWS the future of podcasting.pdf
GRACE ANDREWS the future of podcasting.pdfGRACE ANDREWS the future of podcasting.pdf
GRACE ANDREWS the future of podcasting.pdf
 
This is a test presentation to see if this works
This is a test presentation to see if this worksThis is a test presentation to see if this works
This is a test presentation to see if this works
 
The Colossal Studio Events Packages.pdf
The Colossal Studio  Events Packages.pdfThe Colossal Studio  Events Packages.pdf
The Colossal Studio Events Packages.pdf
 
MalluProgrammers. Careeradvice.Webinarppt.pdf
MalluProgrammers. Careeradvice.Webinarppt.pdfMalluProgrammers. Careeradvice.Webinarppt.pdf
MalluProgrammers. Careeradvice.Webinarppt.pdf
 
Social Media Marketing Company Coimbatore
Social Media Marketing Company CoimbatoreSocial Media Marketing Company Coimbatore
Social Media Marketing Company Coimbatore
 
Maximize Your Pinterest Reach....................
Maximize Your Pinterest Reach....................Maximize Your Pinterest Reach....................
Maximize Your Pinterest Reach....................
 
Unlock Facebook Success with Sociocosmos
Unlock Facebook Success with SociocosmosUnlock Facebook Success with Sociocosmos
Unlock Facebook Success with Sociocosmos
 

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