我遇到一个函数,但不太理解它。我不确定这是一个惯例或有什么意义。_p
是什么,_p
在哪里进入函数的。如果你能给我一些关于for循环的解释,我将不胜感激。在
def contraction_mapping(S, p, MF, params, beta=0.75, threshold=1e-6, suppr_output=False):
'''
Initialization of the state-transition matrices:
describe the state-transition probabilities if the maintenance cost is incurred,
and regenerate the state to 0 if the replacement cost is incurred.
'''
ST_mat = np.zeros((S, S))
p = np.array(p)
for i in range(S):
for j, _p in enumerate(p):
if i + j < S-1:
ST_mat[i+j][i] = _p
elif i + j == S-1:
ST_mat[S-1][i] = p[j:].sum()
else:
pass
R_mat = np.vstack((np.ones((1, S)),np.zeros((S-1, S))))
有关许多python样式约定的详细信息,请参见PEP8。特别是,您可以在此处找到单个前导下划线的说明:
https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles
在上面的循环中,这有点误用,因为他们只使用
_p
来避免与现有名称p
冲突。这些变量名显然不是很好。_p
是enumerate提供的数组项,而p
也是整个数组(本地重写传入的p
参数)。在顺便说一句,循环本身有点笨拙,可以简化和优化(主要是由于使用更好的范围而不是
pass
,并且避免重复地重新计算总和)。在相关问题 更多 >
编程相关推荐