Created
September 2, 2012 16:14
-
-
Save millionsmile/3601019 to your computer and use it in GitHub Desktop.
拡散モデルのエージェントのネットワーク
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# オープンソースで学ぶ社会ネットワーク分析 | |
# 第6章 バイラルへ!- 情報の拡散 | |
# 拡散モデルのエージェントのネットワーク(図6-5) | |
import networkx as net | |
import matplotlib.pyplot as plot | |
import matplotlib.colors as colors | |
import random as r | |
class Person(object): | |
def __init__(self, id): | |
self.id=id | |
self.i = r.random() | |
self.a = self.i | |
self.alpha=0.8 | |
def __str__(self): | |
return(str(self.id)) | |
density=0.9 | |
g=net.Graph() | |
#Personオブジェクトのネットワークを作る | |
for i in range(10): | |
p=Person(i) | |
g.add_node(p) | |
#こうすると、単純なランダムグラフが作られ、すべてのノードペアは同じ確率で接続される | |
for x in g.nodes(): | |
for y in g.nodes(): | |
if r.random()<=density: g.add_edge(x,y) | |
#得られたグラフを描画し、値によってノードに色を塗る | |
col=[n.a for n in g.nodes()] | |
pos=net.spring_layout(g) | |
net.draw_networkx(g,pos=pos, node_color=col) | |
#plot.savefig('diffusion.png') | |
plot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment