Python 我想返回numpy数组中的随机序列?

Python 我想返回numpy数组中的随机序列?,python,numpy,Python,Numpy,我有一个numpy数组定义为 array = np.random.randint(10, size = (10,10)) 这将返回类似这样的结果 ([[6, 5, 7, 5, 9, 0, 5, 1, 3, 3], [0, 8, 1, 7, 0, 1, 7, 7, 6, 9], [4, 1, 7, 5, 5, 9, 6, 8, 9, 2], [7, 9, 8, 5, 3, 7, 1, 1, 5, 4], [2, 5, 8, 3, 8, 2, 5, 5, 7, 1],

我有一个numpy数组定义为

array = np.random.randint(10, size = (10,10))
这将返回类似这样的结果

 ([[6, 5, 7, 5, 9, 0, 5, 1, 3, 3],
   [0, 8, 1, 7, 0, 1, 7, 7, 6, 9],
   [4, 1, 7, 5, 5, 9, 6, 8, 9, 2],
   [7, 9, 8, 5, 3, 7, 1, 1, 5, 4],
   [2, 5, 8, 3, 8, 2, 5, 5, 7, 1],
   [1, 5, 3, 8, 5, 4, 6, 3, 1, 2],
   [0, 0, 2, 3, 6, 2, 9, 9, 2, 9],
   [0, 8, 7, 5, 1, 5, 4, 0, 6, 8],
   [9, 9, 8, 2, 9, 0, 1, 6, 8, 3],
   [9, 8, 4, 6, 5, 7, 3, 2, 4, 6]])
我想要一个函数,它将接受两个参数,(数组和所需序列的长度),并返回所需长度的序列。这些动作应该是随机选择的,而且应该是完全随机的,这样我就可以在一个10 x 10的数组中返回1000个序列。一个示例序列可以是(从上面的数组中),[6,5,8,1,7,5,5,9…直到这个列表的长度为1000,移动可以重叠,应该是完全随机的]这里是已经尝试过但不采取随机移动的

def generate_all_paths(array, length):
length = length - 1
y_dim = len(array)
x_dim = len(array[0])

paths = [[(y, x)] for y in range(y_dim) for x in range(x_dim)]

directions = [(0, 1), (1, 0), 
              (0, -1), (-1, 0)]
temp_paths = []
for i in range(length):
    random.choice(directions)
    temp_paths.append()
    for direction in directions:
        for p in paths:
            y, x = p[-1]
            y, x = y + direction[0], x + direction[1]
            if -1 < y < y_dim and -1 < x < x_dim and (y, x) not in p:
                temp_paths.append(p + [(y, x)])
            temp_paths.append([y,x])

    paths = deepcopy(temp_paths)
    temp_paths = []
return paths
def生成所有路径(数组、长度):
长度=长度-1
y_dim=len(阵列)
x_dim=len(数组[0])
路径=[[(y,x)]用于范围内的y(y_dim)用于范围内的x(x_dim)]
方向=[(0,1),(1,0),
(0, -1), (-1, 0)]
临时路径=[]
对于范围内的i(长度):
随机选择(方向)
临时路径追加()
对于方向中的方向:
对于路径中的p:
y、 x=p[-1]
y、 x=y+方向[0],x+方向[1]
如果-1
您不能创建一个随机路径,然后在整个函数中遵循它吗

import numpy as np

def random_path(array, length):
    output_array = np.zeros((length,))
    trajectory = np.random.randint(low=0, high=4, size=length) 
    step_map = {0 : (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)} # 0 corresponds to right, 1 to up, etc
    current_index = np.array([0, 0])
    for index, step in enumerate(trajectory):
        current_index = current_index + step_map.get(step)
        new_val = array[current_index[0], current_index[1]]
        output_array[index] = new_val
    return output_array

由于数组可以很好地使用负索引,所以溢出应该很好,这样基本上只需遵循一条随机路径。

您不能创建一条随机路径,然后在整个函数中遵循它吗

import numpy as np

def random_path(array, length):
    output_array = np.zeros((length,))
    trajectory = np.random.randint(low=0, high=4, size=length) 
    step_map = {0 : (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)} # 0 corresponds to right, 1 to up, etc
    current_index = np.array([0, 0])
    for index, step in enumerate(trajectory):
        current_index = current_index + step_map.get(step)
        new_val = array[current_index[0], current_index[1]]
        output_array[index] = new_val
    return output_array

由于数组可以很好地使用负索引,所以溢出应该很好,这样基本上只需遵循随机路径。

数组边界处的预期行为是什么? 如果您不想跨过边界,那么像您这样的循环是最简单的方法,否则@AlwaysMV45显示的矢量化选项可能会更好

这里是一个循环:

将numpy导入为np
def生成所有路径(arr、长度):
y尺寸,x尺寸=阵列形状
方向=[(0,1),(1,0),(0,-1),(-1,0)]
路径=[(0,0)]
而len(路径)<长度:
dir_i=方向[np.random.randint(4)]
y、 x=路径[-1]
y、 x=y+dir\u i[0],x+dir\u i[1]
如果-1
阵列边界处的预期行为是什么? 如果您不想跨过边界,那么像您这样的循环是最简单的方法,否则@AlwaysMV45显示的矢量化选项可能会更好

这里是一个循环:

将numpy导入为np
def生成所有路径(arr、长度):
y尺寸,x尺寸=阵列形状
方向=[(0,1),(1,0),(0,-1),(-1,0)]
路径=[(0,0)]
而len(路径)<长度:
dir_i=方向[np.random.randint(4)]
y、 x=路径[-1]
y、 x=y+dir\u i[0],x+dir\u i[1]
如果-1
请让我们知道其中一个答案是否解决了问题,并接受它。否则,请澄清您在寻找什么,如果其中一个答案解决了问题,请告诉我们,并接受它。否则,请澄清您正在寻找的内容