Can we have a faster way to create an array?

Is there a faster way to create the following array? I need to create an array for further calculation. It takes a long time to start up the array. Basically I need to create a series or array with the values โ€‹โ€‹of the norm.ppf function of size 4000.

 nrow = 4000
 lts = pandas.Series(numpy.zeros(nrow))
 lts = lts.apply(lambda x : norm.ppf(random.random(),10),5))
 lts = np.asarray(lts, int)

      

+3


source to share


1 answer


You are not using Pandas for any purpose other than Series being a container, so it would be faster to cut Pandas out of this calculation.

Presumably norm

- scipy.stats.norm

. If so, the norm.ppf

first argument could be a numpy array. So it's much faster to call norm.ppf

once on a 4000 NumPy array than to call norm.ppf

4000 times on a float:

lts = norm.ppf(np.random.random(4000), 10, 5).astype('int')

      




In [120]: %timeit lts = norm.ppf(np.random.random(4000), 10, 5).astype('int')
100 loops, best of 3: 2.51 ms per loop

In [121]: %%timeit
   .....: lts_orig = pd.Series(np.zeros(nrow))
   .....: lts_orig = lts_orig.apply(lambda x : norm.ppf(random.random(),10, 5))
   .....: lts_orig = np.asarray(lts_orig, int)
   .....: 
1 loops, best of 3: 572 ms per loop

      

+6


source







All Articles