SlideShare a Scribd company logo
PyLecture2 -NetworkX-
 Basic Python
› Data types(Integer, String, List)
› Control flow statements
› List comprehension
 Import NetworkX
import networkx as nx
 Generating a graph
G = nx.Graph()
 Adding a node
G.add_node(“one”)
 Adding nodes
G.add_nodes_from([“two”, “three”])
 Adding an edge
G.add_edge(“one”, “two”)
 Adding edges
G.add_edges_from([(“two”, “three”), (“three”, “one”)])
import matplotlib.pyplot as plt
nx.draw(G)
plt.show()
 Path graphs
› G = nx.path_graph(40)
 Cycle graphs
› G = nx.cycle_graph(40)
 Complete graphs
› G = nx.complete_graph(40)
 2D grid graphs
› G = nx.grid_2d_graph(5, 4)
 Random graphs
› G = nx.erdos_renyi_graph(20, 0.3)
› G = nx.barabasi_albert_graph(100, 5)
PyLecture2 -NetworkX-
 The number of degrees
› len(G.nodes())
 The number of edges
› len(G.edges())
 Average degree
› 2 * len(G.edges()) / len(G.nodes())
 Average path length
› Gsub = nx.connected_component_subgraphs(G)[0]
› nx.average_shortest_path_length(G)
 Network density
› nx.density(G)
deg = [nx.degree(G,n) for n in G.nodes()]
hist = [0 for i in range(max(deg)+1)]
for d in deg:
hist[d] += 1
plt.plot(range(len(hist)), hist, "or")
plt.show()
PyLecture2 -NetworkX-
 Go to http://apps.twitter.com and create your app .
› You will get API Key, API Secret, Access Token, and Access Token Secret.
 Installing tweepy
› pip install tweepy
 Next, many, many lines of python code
 You can get the python script(named
Tweepy1.py) at
https://codebreak.com/git/arity/TweepyTutori
al/tree/master/
import time
import tweepy
# Copy the api key, the api secret, the access token and the access
# token secret from the relevant page on your Twitter app
api_key = ‘your_api_key_here'
api_secret = ‘your_api_secret_here’
access_token = ‘your_access_token_here'
access_token_secret = ‘your_access_token_secret_here'
# You don't need to make any changes below here
# This bit authorises you to ask for information from Twitter
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
# The api object gives you access to all of the http calls that Twitter accepts
api = tweepy.API(auth)
def fib(n,user,network):
if n>0:
time.sleep(40)
try:
users=api.followers(user)
except tweepy.TweepError:
return
for follower in users:
network.add_node(follower.screen_name)
network.add_edge(follower.screen_name, user)
fib(n-1,follower.screen_name,network)
#Then, There is a directed graph
G = nx.DiGraph()
#User we want to use as initial node
user='GitHub‘
G.add_node(user)
n=2
fib(n,user,G)
nx.draw(G)
plt.show()
PyLecture2 -NetworkX-
 Graph manipulating functions
› Adding nodes, edges
 Many types of network
 Many network characteristics

More Related Content

PPTX
IGraph a tool to analyze your network
PPTX
Frequency count of the Element in an array
PPTX
3. basics of python
PDF
The Ring programming language version 1.6 book - Part 49 of 189
DOCX
Practical no 4
DOC
Computer hw1
PPT
Computer notes - Hashing
PPTX
Butterfly Counting in Bipartite Networks
IGraph a tool to analyze your network
Frequency count of the Element in an array
3. basics of python
The Ring programming language version 1.6 book - Part 49 of 189
Practical no 4
Computer hw1
Computer notes - Hashing
Butterfly Counting in Bipartite Networks

What's hot (20)

PPTX
Insert element position
PPTX
Insertion operation
PPT
Grand Central Dispatch
PDF
Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...
PPTX
Dynamically linked queues
PPTX
C# 8 and Beyond
DOCX
Bubble in link list
PPT
Number
PPTX
Favor composition over inheritance
PPT
Stack linked list
PPTX
DevFM #20 : SqlDatabaseCommand, un Simple Object Mapping Toolkit
PPTX
Deletion of an element at a given particular position
PPTX
Software Design Thinking
PDF
Brief Introduction to Cython
PPTX
Crystal Ball Event Prediction and Log Analysis with Hadoop MapReduce and Spark
PDF
Why is a[1] fast than a.get(1)
PDF
Stack push pop
PPT
A useful tools in windows py2exe(optional)
Insert element position
Insertion operation
Grand Central Dispatch
Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...
Dynamically linked queues
C# 8 and Beyond
Bubble in link list
Number
Favor composition over inheritance
Stack linked list
DevFM #20 : SqlDatabaseCommand, un Simple Object Mapping Toolkit
Deletion of an element at a given particular position
Software Design Thinking
Brief Introduction to Cython
Crystal Ball Event Prediction and Log Analysis with Hadoop MapReduce and Spark
Why is a[1] fast than a.get(1)
Stack push pop
A useful tools in windows py2exe(optional)
Ad

Viewers also liked (11)

PDF
Brochure 2016 for email
PPTX
ADVENTURES IN MISSION
PPT
Stephen Joseph Racik
PPTX
EL CORAZÓN DELATOR Carmen Martínez
PDF
RoadPiece_Freshman
PPT
Global marketing
DOC
Akshat Mathur- Architect
PDF
(S) Managing the risks of using Social Media
PPTX
Bab 9 PEMBENTUKAN NEGARA PERSEKUTUAN AMERIKA SYARIKAT
Brochure 2016 for email
ADVENTURES IN MISSION
Stephen Joseph Racik
EL CORAZÓN DELATOR Carmen Martínez
RoadPiece_Freshman
Global marketing
Akshat Mathur- Architect
(S) Managing the risks of using Social Media
Bab 9 PEMBENTUKAN NEGARA PERSEKUTUAN AMERIKA SYARIKAT
Ad

Similar to PyLecture2 -NetworkX- (20)

PDF
Cluj.py Meetup: Extending Python in C
PDF
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
PDF
python lab programs.pdf
PDF
Swift for tensorflow
DOCX
CLASSROOM activity in graph analytics course
PDF
Machine learning with py torch
PDF
The Ring programming language version 1.5.3 book - Part 40 of 184
PDF
How to Build a Telegraf Plugin by Noah Crowley
PDF
Building a Telegraf Plugin by Noah Crowly | Developer Advocate | InfluxData
PPTX
Introduction to Python.Net
PDF
Java Simple Programs
PDF
Computer science-2010-cbse-question-paper
PPTX
Apache Flink: API, runtime, and project roadmap
PPT
Backdoor coding
DOCX
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
PDF
The Ring programming language version 1.10 book - Part 39 of 212
PDF
Functional Programming inside OOP? It’s possible with Python
PDF
Numba: Array-oriented Python Compiler for NumPy
PPTX
Iron python
PDF
Python na Infraestrutura 
MySQL do Facebook

Cluj.py Meetup: Extending Python in C
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
python lab programs.pdf
Swift for tensorflow
CLASSROOM activity in graph analytics course
Machine learning with py torch
The Ring programming language version 1.5.3 book - Part 40 of 184
How to Build a Telegraf Plugin by Noah Crowley
Building a Telegraf Plugin by Noah Crowly | Developer Advocate | InfluxData
Introduction to Python.Net
Java Simple Programs
Computer science-2010-cbse-question-paper
Apache Flink: API, runtime, and project roadmap
Backdoor coding
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
The Ring programming language version 1.10 book - Part 39 of 212
Functional Programming inside OOP? It’s possible with Python
Numba: Array-oriented Python Compiler for NumPy
Iron python
Python na Infraestrutura 
MySQL do Facebook


More from Yoshiki Satotani (7)

PPTX
Basic practice of R
PPTX
Basic use of Python (Practice)
PPTX
Basic use of Python
PDF
Py lecture5 python plots
PDF
PyLecture4 -Python Basics2-
PDF
PyLecture1 -Python Basics-
PDF
PyLecture3 -json-
Basic practice of R
Basic use of Python (Practice)
Basic use of Python
Py lecture5 python plots
PyLecture4 -Python Basics2-
PyLecture1 -Python Basics-
PyLecture3 -json-

PyLecture2 -NetworkX-

  • 2.  Basic Python › Data types(Integer, String, List) › Control flow statements › List comprehension
  • 3.  Import NetworkX import networkx as nx  Generating a graph G = nx.Graph()  Adding a node G.add_node(“one”)  Adding nodes G.add_nodes_from([“two”, “three”])  Adding an edge G.add_edge(“one”, “two”)  Adding edges G.add_edges_from([(“two”, “three”), (“three”, “one”)])
  • 4. import matplotlib.pyplot as plt nx.draw(G) plt.show()
  • 5.  Path graphs › G = nx.path_graph(40)  Cycle graphs › G = nx.cycle_graph(40)  Complete graphs › G = nx.complete_graph(40)  2D grid graphs › G = nx.grid_2d_graph(5, 4)  Random graphs › G = nx.erdos_renyi_graph(20, 0.3) › G = nx.barabasi_albert_graph(100, 5)
  • 7.  The number of degrees › len(G.nodes())  The number of edges › len(G.edges())  Average degree › 2 * len(G.edges()) / len(G.nodes())  Average path length › Gsub = nx.connected_component_subgraphs(G)[0] › nx.average_shortest_path_length(G)  Network density › nx.density(G)
  • 8. deg = [nx.degree(G,n) for n in G.nodes()] hist = [0 for i in range(max(deg)+1)] for d in deg: hist[d] += 1 plt.plot(range(len(hist)), hist, "or") plt.show()
  • 10.  Go to http://apps.twitter.com and create your app . › You will get API Key, API Secret, Access Token, and Access Token Secret.  Installing tweepy › pip install tweepy
  • 11.  Next, many, many lines of python code  You can get the python script(named Tweepy1.py) at https://codebreak.com/git/arity/TweepyTutori al/tree/master/
  • 12. import time import tweepy # Copy the api key, the api secret, the access token and the access # token secret from the relevant page on your Twitter app api_key = ‘your_api_key_here' api_secret = ‘your_api_secret_here’ access_token = ‘your_access_token_here' access_token_secret = ‘your_access_token_secret_here' # You don't need to make any changes below here # This bit authorises you to ask for information from Twitter auth = tweepy.OAuthHandler(api_key, api_secret) auth.set_access_token(access_token, access_token_secret) # The api object gives you access to all of the http calls that Twitter accepts api = tweepy.API(auth)
  • 13. def fib(n,user,network): if n>0: time.sleep(40) try: users=api.followers(user) except tweepy.TweepError: return for follower in users: network.add_node(follower.screen_name) network.add_edge(follower.screen_name, user) fib(n-1,follower.screen_name,network)
  • 14. #Then, There is a directed graph G = nx.DiGraph() #User we want to use as initial node user='GitHub‘ G.add_node(user) n=2 fib(n,user,G) nx.draw(G) plt.show()
  • 16.  Graph manipulating functions › Adding nodes, edges  Many types of network  Many network characteristics