社交网络分析——信息传播模型(附带三个模型的python实现)
生活随笔
收集整理的這篇文章主要介紹了
社交网络分析——信息传播模型(附带三个模型的python实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘要:主要講解一些基本的信息傳播模型,以及IC模型、SI模型和SIR模型的python實現及可視化。
- 2021.10.06更新
- 有需要的可以點擊傳送門
- 2020.09.26更新
- 更新了SIR模型的實現,請點擊傳送門,就不放在這篇博客里了
- 2020.09.03更新
- 更新了SI模型和IC模型用不同顏色表示每次激活的節點,在本文最后
author:xiao黃
緩慢而堅定的生長
信息傳播模型
影響力模型
- IC模型
- LT模型
傳染模型
- SI模型
- SIR模型
- SIS模型
- SIRS模型
Influence Models
- 影響模型可模擬用戶如何影響網絡中的每個人
- 節點有兩個狀態
未激活:節點未收到信息
激活:節點收到信息并且能夠傳播給他們的鄰居
Independent Cascade (IC) Model
- 在 t 時刻被激活的節點在 t+1 時刻僅有一次機會去激活其鄰居
- 假設節點 v 在 t 時刻被激活,對于 v 的任何鄰居 w ,w 在 t+1 時刻被激活的概率是PvwPvwPvw
- 如果是無權重的話,可以都設置為0.5,那么這樣傳播與不傳播的概率就是一半一半了
例子
Liner Threshold (LT) Model
- 在任意時刻,激活的點都可以影響未被激活的點
- 每個節點都有激活閾值
- 如果影響程度超過該節點的閾值,則這節點被激活
Infection Models
- 傳染模型也叫流行病模型,用于描述個人傳播傳染病的方式
- 節點有兩種狀態
易感人群:易感節點可能會感染疾病
感染人群:感染節點有機會去感染易感人群
免疫人群:感染節點被治愈后不會再得疾病的人群
Susceptible Infected (SI) Model
- 節點有兩個狀態
易感節點(S)
感染節點(I) - 如何傳染
一個節點被感染,它將持續傳染周圍的節點
在每個離散時間中,每個被感染的節點嘗試以概率p去感染它的易感(未感染)鄰居
Susceptible Infected Recovered (SIR) Model
- Intuition:一些被感染的節點以一定的概率成為免疫節點,免疫節點不能被感染疾病或者傳播疾病
- 節點有三種狀態:
易感節點;感染節點;免疫節點 - 節點的變化
β:易感節點被成功感染的概率
γ:感染節點被治愈的概率
Susceptible Infected Susceptible (SIS) Model
- Intuition:感染節點以一定的概率變成易感節點,又有一定的概率被感染成感染節點
- 節點有兩種狀態
易感節點;感染節點 - 節點的變化
β:易感節點被成功感染的概率
γ:感染節點被治愈成易感節點的概率
Susceptible Infected Recovered Susceptible (SIRS) Model
- Intuition:免疫節點有一定的概率變成易感節點
- 節點的變化
β:易感節點被成功感染的概率
γ:感染節點被治愈成免疫節點的概率
λ:免疫節點有一定的概率轉化成易感節點
IC模型的python實現
先上代碼,這里我就不解釋了,代碼里面的注釋我覺得很詳細了,不懂可以評論或者私信。
import randomimport matplotlib.pyplot as plt import networkx as nx import numpy as npmax_iter_num = 10 # 模擬的次數 G = nx.karate_club_graph() # 空手道俱樂部for edge in G.edges:G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作為權值 for node in G:G.add_node(node, state = 0) # 用state標識狀態 state=0 未激活,state=1 激活seed = 33 # 選定33作為初始激活節點 G.node[seed]['state'] = 1 # 表示33被激活activated_graph = nx.Graph() # 被激活的圖 activated_graph.add_node(seed)all_active_nodes = [] # 所有被激活的節點放在這里 all_active_nodes.append(seed)start_influence_nodes = [] # 剛被激活的節點 即有影響力去影響別人的節點 start_influence_nodes.append(seed)for i in range(max_iter_num):new_active = list()t1 = '%s time' % i + ' %s nodes' % len(all_active_nodes)print(t1) # 當前有多少個節點激活# 畫圖plt.title(t1)nx.draw(activated_graph, with_labels=True)plt.show()for v in start_influence_nodes:for nbr in G.neighbors(v): if G.node[nbr]['state'] == 0: # 如果這個鄰居沒被激活edge_data = G.get_edge_data(v, nbr)if random.uniform(0, 1) < edge_data['weight']:G.node[nbr]['state'] = 1new_active.append(nbr)activated_graph.add_edge(v, nbr) # 畫圖 添加邊start_influence_nodes.clear() # 將原先的有個影響力的清空start_influence_nodes.extend(new_active) # 將新被激活的節點添加到有影響力all_active_nodes.extend(new_active) # 將新被激活的節點添加到激活的列表中print('all_active_nodes',all_active_nodes) # 打印程序運行結果圖:
節點圖,這里我就放了9張,第十張不放了,你們運行的時候會看到的
SI模型的python實現
先上代碼,這里我就不解釋了,代碼里面的注釋我覺得很詳細了,不懂可以評論或者私信。
import randomimport matplotlib.pyplot as plt import networkx as nx import numpy as npmax_iter_num = 5 # 模擬的次數 G = nx.karate_club_graph() # 空手道俱樂部for edge in G.edges:G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作為權值 病毒的感染能力 for node in G:G.add_node(node, state = 0) # 用state標識狀態 state=0 未激活,state=1 激活seed = 33 # 選定33作為傳染源 G.node[seed]['state'] = 1 # 表示33是感染的all_infect_nodes = [] # 所有被感染的節點放在這里 all_infect_nodes.append(seed)infected_graph = nx.Graph() # 被激活的圖 infected_graph.add_node(seed)for i in range(max_iter_num):new_infect = list() # 新被感染的t1 = '%s time' % i + ' %s nodes' % len(all_infect_nodes)print(t1) # 當前有多少個節點被感染# 畫圖plt.title(t1)nx.draw(infected_graph, with_labels=True)plt.show()# 感染的機會不止一次for v in all_infect_nodes:for nbr in G.neighbors(v):if G.node[nbr]['state'] == 0: # 如果這個鄰居節點沒被感染edge_data = G.get_edge_data(v, nbr)if random.uniform(0, 1) < edge_data['weight']:G.node[nbr]['state'] = 1new_infect.append(nbr)infected_graph.add_edge(v, nbr) # 畫圖 添加邊all_infect_nodes.extend(new_infect) # 將新感染的添加到print('all_active_nodes:', all_infect_nodes)運行結果:
節點效果圖:
IC模型的python實現更新版(2020.09.03)
import randomimport matplotlib.pyplot as plt import networkx as nx import numpy as npmax_iter_num = 10 # 模擬的次數 G = nx.karate_club_graph() # 空手道俱樂部for edge in G.edges:G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作為權值 for node in G:G.add_node(node, state = 0) # 用state標識狀態 state=0 未激活,state=1 激活seed = 33 # 選定33作為初始激活節點 G.node[seed]['state'] = 1 # 表示33被激活# activated_graph = nx.Graph() # 被激活的圖 # activated_graph.add_node(seed)all_active_nodes = [] # 所有被激活的節點放在這里 all_active_nodes.append(seed)start_influence_nodes = [] # 剛被激活的節點 即有影響力去影響別人的節點 start_influence_nodes.append(seed)color_list = ['brown','orange','r','g','b','y','m','gray','black','c','pink','brown','orange','r','g','b','y','m','gray','black','c','pink'] res = [[seed]] for i in range(max_iter_num):new_active = list()t1 = '%s time' % i + ' %s nodes' % len(all_active_nodes)print(t1) # 當前有多少個節點激活# 畫圖# plt.title(t1)# nx.draw(activated_graph, with_labels=True,node_color=color_list[i])# plt.show()for v in start_influence_nodes:for nbr in G.neighbors(v): if G.node[nbr]['state'] == 0: # 如果這個鄰居沒被激活edge_data = G.get_edge_data(v, nbr)if random.uniform(0, 1) < edge_data['weight']:G.node[nbr]['state'] = 1new_active.append(nbr)# activated_graph.add_edge(v, nbr) # 畫圖 添加邊print('激活',new_active)start_influence_nodes.clear() # 將原先的有個影響力的清空start_influence_nodes.extend(new_active) # 將新被激活的節點添加到有影響力all_active_nodes.extend(new_active) # 將新被激活的節點添加到激活的列表中res.append(new_active)print('all_active_nodes',all_active_nodes) # 打印 # print(res)res = [c for c in res if c] pos = nx.spring_layout(G) # 節點的布局為spring型 nx.draw(G,pos, with_labels=True, node_color='w', node_shape = '.') color_list = ['brown','orange','r','g','b','y','m','gray','black','c','pink','brown','orange','r','g','b','y','m','gray','black','c','pink'] for i in range(len(res)):nx.draw_networkx_nodes(G, pos, with_labels=True, node_color=color_list[i], nodelist=res[i]) plt.show()結果圖:
SI模型的python實現更新版(2020.09.03)
import randomimport matplotlib.pyplot as plt import networkx as nx import numpy as npmax_iter_num = 5 # 模擬的次數 G = nx.karate_club_graph() # 空手道俱樂部for edge in G.edges:G.add_edge(edge[0], edge[1], weight=random.uniform(0,1)) # 可不可以作為權值 病毒的感染能力 for node in G:G.add_node(node, state = 0) # 用state標識狀態 state=0 未感染,state=1 激活seed = 33 # 選定33作為傳染源 G.node[seed]['state'] = 1 # 表示33是感染的all_infect_nodes = [] # 所有被感染的節點放在這里 all_infect_nodes.append(seed) res = [[seed]]# infected_graph = nx.Graph() # 被激活的圖 # infected_graph.add_node(seed)for i in range(max_iter_num):new_infect = list() # 新被感染的t1 = '%s time' % i + ' %s nodes' % len(all_infect_nodes)print(t1) # 當前有多少個節點被感染# 畫圖# plt.title(t1)# nx.draw(infected_graph, with_labels=True)# plt.show()# 感染的機會不止一次for v in all_infect_nodes:for nbr in G.neighbors(v):if G.node[nbr]['state'] == 0: # 如果這個鄰居節點沒被感染edge_data = G.get_edge_data(v, nbr)if random.uniform(0, 1) < edge_data['weight']:G.node[nbr]['state'] = 1new_infect.append(nbr)# infected_graph.add_edge(v, nbr) # 畫圖 添加邊res.append(new_infect)all_infect_nodes.extend(new_infect) # 將新感染的添加到print('all_active_nodes:', all_infect_nodes)res = [c for c in res if c] pos = nx.spring_layout(G) # 節點的布局為spring型 nx.draw(G,pos, with_labels=True, node_color='w', node_shape = '.') color_list = ['brown','orange','r','g','b','y','m','gray','black','c','pink','brown','orange','r','g','b','y','m','gray','black','c','pink'] for i in range(len(res)):nx.draw_networkx_nodes(G, pos, with_labels=True, node_color=color_list[i], nodelist=res[i]) plt.show()結果:
- 說在最后,因為是隨機的,所以每次運行的結果可能是不一樣的
總結
以上是生活随笔為你收集整理的社交网络分析——信息传播模型(附带三个模型的python实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV阈值分割
- 下一篇: DSP实验报告—实验1