python-论文学习-基于元胞的企业负面口碑传播模型
用到的库:
import time
import networkx as nx #导入复杂网络分析库
import matplotlib.pyplot as plt #导入科学绘图库
import random
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
获取多次结果平均值函数,以机事件按概率发生函数:
def getAv(li):
new = []
for i in range(len(li)):
n = 0
for j in range(len(li)):
n += li
new.append(round(n/len(li),3))
return(new)
#概率事件
def ran_happen(p):
n = random.random()
if n < p:
return(1)
else:
return(0)
异质元胞自动机 (HCA) 的负面口碑扩散模型类:
class HCA(object):
def __init__(self,h,l,Vc,Va,Vb,Vy,Vo):
super(HCA, self).__init__()
self.h = h #行
self.l = l #列
self.Vc = Vc #初期抱怨比例
self.Va = Va #易感转化抱怨概率
self.Vb = Vb #负面口碑传播概率
self.Vy = Vy #二次负面概率
self.Vo = Vo #负面口碑解除概率
self.cell = np.random.randint(0,1,size=(self.h,self.l))
for i in range(self.h):
for j in range(self.l):
if ran_happen(self.Vc):
self.cell = 1 #0易感1抱怨2康复
def lin(self,i,j):#元胞邻域历遍
ii =
jj =
for x in range(len(ii)):
if ii < self.h and ii >= 0:
if jj < self.l and jj >= 0:
if self.cell]] == 0:
if ran_happen(self.Vb):
self.cell]] = 1
if self.cell]] == 2:
if ran_happen(self.Vy):
self.cell]] = 1
def process(self):#每一步
num = 0
for i in range(self.h):
for j in range(self.l):
if self.cell == 0:
if ran_happen(self.Va):
self.cell = 1
elif self.cell == 1:
num += 1
self.lin(i,j)
if ran_happen(self.Vo):
self.cell = 2
else:
if ran_happen(self.Vy):
self.cell = 1
return(num)
主函数:
def main(Vc):
ZN = []
ZNN = []
for j in range(2):
hh = HCA(128,128,Vc,0.05,0.1,0.1,0.5)
N = []
NN = []
old = 0
for i in range(20):
n = hh.process()
N.append(round(n/(128*128),3))
NN.append(round((n-old)/(128*128),3))
old = n
ZN.append(N)
ZNN.append(NN)
return(getAv(ZN),getAv(ZNN))
绘图代码:
if __name__ == '__main__':
ZNav = []
ZNNav = []
for i in range(4):
Vc = round((i+1)*0.05,2)
print('初始抱怨比例:%s'%Vc)
nav,nnav = main(Vc)
ZNav.append(nav)
ZNNav.append(nnav)
plt.figure(figsize=(12,5), dpi=100)
plt.figure(1)
ax1 = plt.subplot(121)
xx = np.arange(0,20,1)
plt.plot(xx,ZNav,'-',label='5%')
plt.plot(xx,ZNav,'-.',label='10%')
plt.plot(xx,ZNav,'--',label='15%')
plt.plot(xx,ZNav,'--.',label='20%')
xt = np.arange(0,20,5)
yt = np.arange(0,0.7,0.1)
plt.xticks(xt) # 这里传入的x就是x轴的列表
plt.yticks(yt) # 这里传入的x就是x轴的列表
plt.xlabel('stage')
plt.ylabel('Percent of I')
plt.legend()
ax1 = plt.subplot(122)
plt.plot(xx,ZNNav,'-',label='5%')
plt.plot(xx,ZNNav,'-.',label='10%')
plt.plot(xx,ZNNav,'--',label='15%')
plt.plot(xx,ZNNav,'--.',label='20%')
xt = np.arange(0,20,5)
yt = np.arange(-0.05,0.3,0.05)
plt.xticks(xt) # 这里传入的x就是x轴的列表
plt.yticks(yt) # 这里传入的x就是x轴的列表
plt.xlabel('stage')
plt.ylabel('Rate of I')
plt.legend()
#保存图表
print('图表保存')
plt.savefig("./图4.png")
#显示图表
plt.show()
附件解压密码:www.52pojie.cn
不错,学习一下 三人行必有我师!值得学习!
页:
[1]