def print_func(coefs,interprets,decimals=5):
'''
:param coefs: array对象
:param interprets: array对象
:decimals 系数保留的小数点数
:return: string 神经网络表达式字符串列表
'''
y_num=len(interprets[1])
x_num=len(coefs[0])
hind_slice=len(coefs[1])
y_str_ls=[]
for i in range(y_num):
y_str="y"+str(i+1)+"="
for j in range(hind_slice):
y_str += "\n+"+str(round(coefs[1][j][i],decimals))+"*"+"tansig("
for m in range(x_num+1):
if m!=x_num:
y_str+=str(round(coefs[0][m][j],decimals))+f"*X{m+1}"+"+"
else:
y_str += str(round(interprets[0][j],decimals))
y_str += ")"
y_str+="\n+"+str(round(interprets[1][i],decimals))
y_str=y_str.replace("+-","-")
print(y_str)
y_str_ls.append(y_str)
# print(y_str_ls)
return y_str_ls
在这里插入代码片
首先用sklearn包的MLPRegressor求出BP神经网络模型model,获取model.coef_和model.interpret_俩属性传入上面函数,即可成功获取BP神经网络的公式。
注意:本公式一般用于隐藏层不多的情况。