Skip to content Skip to sidebar Skip to footer

How To Speed Up Scipy.stats.truncnorm For 3d Array?

For 1D array, I found that the generation of rsv by truncnorm is at least 1 order of magnitude faster than using norm and np.where. The 1D test code is shown below. Timing result i

Solution 1:

truncnorm.rvs is so much slower in the 3d case because the parameters you feed in for a, b, loc, and scale are arrays the same shape as your desired output. So, for the numbers you show, you need to create four 270,000-element arrays, which will clog your memory (which on its own slows the code), plus scipy needs to check every element of the arrays for every element it generates. It's perfectly fine to use scalars for those parameters, as you do in the 1d case; the shape of the output array is determined by the size argument.

A side note: because of this checking-every-element behavior, you won't get the same output using arrays vs. using scalars in the function call, even if you use the same SeedSequence.

Post a Comment for "How To Speed Up Scipy.stats.truncnorm For 3d Array?"