pylecture2 -networkx-

16

Upload: yoshiki-satotani

Post on 08-Aug-2015

54 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: PyLecture2 -NetworkX-
Page 2: PyLecture2 -NetworkX-

Basic Python

› Data types(Integer, String, List)

› Control flow statements

› List comprehension

Page 3: PyLecture2 -NetworkX-

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”)])

Page 4: PyLecture2 -NetworkX-

import matplotlib.pyplot as plt

nx.draw(G)

plt.show()

Page 5: PyLecture2 -NetworkX-

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)

Page 6: PyLecture2 -NetworkX-
Page 7: 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)

Page 8: PyLecture2 -NetworkX-

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()

Page 9: PyLecture2 -NetworkX-
Page 10: 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

Page 11: PyLecture2 -NetworkX-

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/

Page 12: PyLecture2 -NetworkX-

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)

Page 13: PyLecture2 -NetworkX-

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)

Page 14: PyLecture2 -NetworkX-

#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()

Page 15: PyLecture2 -NetworkX-
Page 16: PyLecture2 -NetworkX-

Graph manipulating functions

› Adding nodes, edges

Many types of network

Many network characteristics