vendredi 25 juin 2021

Scipy: Speeding Up Sampling

I have developed the following program for generating a list of subsamples, in chunks, for a specific task. It is, however, very slow, since I am doing random samples in a loop.

import scipy.stats as stats
import numpy as np

#GENERATE SOME RANDOMLY CHUNKED COUNT DATA
N_chunks=250
idx_chunks = np.random.randint(20, size=N_chunks)
idx_cumsum = np.cumsum(idx_chunks)
data_sample = stats.poisson(mu=5).rvs(size=np.sum(idx_chunks))
data_sample_split = np.split(data_sample, idx_cumsum)[:-1]

#GENERATE SUBSAMPLES OF THE LENGTH GIVEN BY EACH ELEMENT OF THE LIST
f = stats.poisson(mu=2)
output = []
total = 0
for _i1 in data_sample_split:
    temp = []
    for _ii1 in _i1:
        temp.append(f.rvs(_ii1))
    output.append(temp)

Is there a way I can I speed up the program, while obtaining exactly the same output?

I am specifically looking to presample all the samples I need, before reshaping to the list of lists. However, I do not know how to do this.




Aucun commentaire:

Enregistrer un commentaire