dimanche 3 février 2019

Create a matrix of sample : issue with np.apply_along_axis additional arguments

I stumbled into a little issue trying to create a Matrix of randomly selected sample amongst a population using numpy.

What I want to create is a matrix, each line representing a sample from my population, so that I can do operations on each sample at a time more efficiently.

I tried doing the following:

import numpy as np

l = ["L"] * 36 + ["T1"]*20 + [0]*43

pop = np.array(l)

ech = np.random.choice(a = pop, size=7, replace=False)  # This is what I want my sample to be

A = np.full((30,1),7)  # Here I want 30 samples in my matrix

np.apply_along_axis(arr=A, func1d=np.random.choice, axis=1, a = pop)

However, I get the following error:

TypeError: choice() got multiple values for keyword argument 'a'

As the np.random.choice function takes several argument, I created the matrix A filed with sevens (because I want samples of 7 elements from pop) so that the size arguments comes from the value of each line of A, and the a argument is given at the end by a=pop.

I was wondering if anyone knew what the mistake was here, and how to fix it.

I used the used the np.apply_along_axis to create this matrix of samples because I would have done it in a similar way with the apply function in R, but maybe there's a better way to do this?




Aucun commentaire:

Enregistrer un commentaire