random的随机抽取之间的区别

random的随机抽取之间的区别

本文介绍了来自scipy.stats .... rvs和numpy.random的随机抽取之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎是相同的分布,从numpy.random提取随机样本比从scipy.stats.-.rvs提取随机样本要快.我想知道是什么原因造成了两者之间的速度差异?

It seems if it is the same distribution, drawing random samples from numpy.random is faster than doing so from scipy.stats.-.rvs. I was wondering what causes the speed difference between the two?

推荐答案

scipy.stats.uniform实际上使用numpy,这是统计信息中的对应函数(mtrand是numpy.random的别名)

scipy.stats.uniform actually uses numpy, here is the corresponding function in stats (mtrand is an alias for numpy.random)

class uniform_gen(rv_continuous):
    def _rvs(self):
        return mtrand.uniform(0.0,1.0,self._size)

scipy.stats有一些开销,用于错误检查和使接口更灵活.只要您没有在每次绘制的循环中调用uniform.rvs,速度差异就应该最小.相反,您可以一次获得所有随机抽奖,例如(1000万)

scipy.stats has a bit of overhead for error checking and making the interface more flexible. The speed difference should be minimal as long as you don't call uniform.rvs in a loop for each draw. You can get instead all random draws at once, for example (10 million)

>>> rvs = stats.uniform.rvs(size=(10000, 1000))
>>> rvs.shape
(10000, 1000)

这是我不久前写的很长的答案:

Here is the long answer, that I wrote a while ago:

scipy/numpy中的基本随机数由以下项创建numpy.random中的Mersenne-Twister PRNG.的随机数numpy.random中的分布​​在cython/pyrex中,并且速度非常快.

The basic random numbers in scipy/numpy are created byMersenne-Twister PRNG in numpy.random. The random numbers fordistributions in numpy.random are in cython/pyrex and are pretty fast.

scipy.stats没有随机数生成器,随机数是通过以下三种方式之一获得:

scipy.stats doesn't have a random number generator, random numbers areobtained in one of three ways:

  • 直接来自numpy.random,例如正常,t,...挺快的

  • directly from numpy.random, e.g. normal, t, ... pretty fast

随机数通过转换其他随机数在numpy.random中可用,也非常快,因为它可以在整个数字数组

random numbers by transformation of other random numbers that areavailable in numpy.random, also pretty fast because this operates onentire arrays of numbers

generic:唯一的通用生成随机数生成是通过使用ppf(逆cdf)转换统一随机数.如果有一个明确的表达ppf,但如果必须计算ppf,可能会非常慢间接地.例如,如果仅定义了pdf,则cdf为通过数值积分获得ppp通过方程求解器.所以有些发行版非常慢.

generic: the only generic generation random number generation is byusing the ppf (inverse cdf) to transform uniform random numbers.This is relatively fast if there is an explicit expression for theppf, but can be very slow if the ppf has to be calculatedindirectly. For example if only the pdf is defined, then the cdf isobtained through numerical integration and the ppf is obtained throughan equation solver. So a few distributions are very slow.

这篇关于来自scipy.stats .... rvs和numpy.random的随机抽取之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:56