Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millionsmile/3652042 to your computer and use it in GitHub Desktop.
Save millionsmile/3652042 to your computer and use it in GitHub Desktop.
拡散モデルのエージェントのネットワーク
# -*- coding: utf-8 -*-
# オープンソースで学ぶ社会ネットワーク分析
# 第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