Suppose I have the following numpy array:
Space = np.arange(7)
Question: How could I generate a set of N samples from Space such that:
- Each sample consist only of increasing or decreasing consecutive numbers
- The sampling is done with replacement so the sample need not be monotonically increasing or decreasing.
- Each sample ends with a 6 or 0, and
- There is no limitation on the length of the samples (however each sample terminates once a 6 or 0 has been selected).
In essence I'm creating a markov reward process via numpy sampling (There is probably a more efficient packet for this, but i'm not sure what it would be.) For example if N = 3, a possible sampled set would look something like this.
Sample = [[1,0],[4, 3, 4, 5, 6],[4, 3, 2, 1, 2, 1, 0]]
I can accomplish this with something not very elegant like this:
N = len(Space)
Set = []
for i in range(3):
X = np.random.randint(N)
if (X == 0) | (X==6):
Set.append(X)
else:
Sample = []
while (X !=0) & (X != 6):
Next = np.array([X-1, X+1])
X = np.random.choice(Next)
Sample.append(X)
Set.append(Sample)
return(Set)
But I was wondering what a more efficient/pythonic way to go about this type of sampling, perhaps without so many loops? Or alternatively if there are better python libraries for this sort of thing? Thanks.
Aucun commentaire:
Enregistrer un commentaire