Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代函数';Python中的s参数(使用2d numpy数组和小数列表)_Python_Loops_Numpy_Matrix_Iteration - Fatal编程技术网

迭代函数';Python中的s参数(使用2d numpy数组和小数列表)

迭代函数';Python中的s参数(使用2d numpy数组和小数列表),python,loops,numpy,matrix,iteration,Python,Loops,Numpy,Matrix,Iteration,我定义了一个包含两个参数的函数:对称矩阵M和概率值p。我想定义一个在我的参数上循环的函数。循环以概率0.01开始,当达到p时停止。在每一步中,函数根据概率从矩阵M中随机选取行和列,并将其删除。然后以递增的概率对新的M执行相同的操作。我无法使用我的代码获得结果 支持小数的范围函数 这样,对于第一个索引,i=0.01,只返回M和p,这是因为一旦返回某个内容,循环就会停止。此外,由于可以使用python中给定的range,因此第一个函数是多余的。我建议您使用列表返回矩阵和索引(您也可以使用np.arr

我定义了一个包含两个参数的函数:对称矩阵
M
和概率值
p
。我想定义一个在我的参数上循环的函数。循环以概率0.01开始,当达到
p
时停止。在每一步中,函数根据概率从矩阵
M
中随机选取行和列,并将其删除。然后以递增的概率对新的
M
执行相同的操作。我无法使用我的代码获得结果

支持小数的范围函数
这样,对于第一个索引,
i=0.01
,只返回
M
p
,这是因为一旦返回某个内容,循环就会停止。此外,由于可以使用python中给定的
range
,因此第一个函数是多余的。我建议您使用列表返回矩阵和索引(您也可以使用np.array)

如果您还希望包含概率
p
,则必须循环执行
范围(0.01,p+0.01,0.01)

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
def loop(M, p):
  mat_list  = []
  indices_list = []
  for i in range(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
      mat_list.append(M)
      indices_list.append(indices)
  return mat_list, indices_list