【发布时间】:2018-12-11 18:13:46
【问题描述】:
我定义了一个带有 2 个参数的函数:一个对称矩阵 M 和一个概率值 p。我想定义一个循环遍历我的参数的函数。循环以 0.01 的概率开始,并在到达 p 时停止。在每一步,该函数根据概率从矩阵M 中选择随机行和列并将它们删除。然后以增加的概率对新的M 执行相同的操作。我无法使用我的代码获得结果
支持小数的范围函数
def frange(start, end, step):
tmp = start
while tmp < end:
yield tmp
tmp += step
循环函数(从矩阵中随机选择行和列并删除它们)
def loop(M, p):
for i in frange(0.01, p, 0.01):
indices = random.sample(range(np.shape(M)[0]),
int(round(np.shape(M)[0] * i)))
M = np.delete(M, indices, axis=0) # removes rows
M = np.delete(M, indices, axis=1) # removes columns
return M, indices
【问题讨论】:
标签: python loops numpy matrix iteration