加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python中的统计引导程序库?

发布时间:2020-12-16 21:53:26 所属栏目:Python 来源:网络整理
导读:Python中是否有统计引导程序库? 我希望功能类似于R bootstrap中提供的功能: http://statistics.ats.ucla.edu/stat/r/library/bootstrap.htm 搜索我发现: http://mjtokelly.blogspot.com/2006/04/bootstrap-statistics-in-python.html(代码的链接被破坏) h

Python中是否有统计引导程序库?

我希望功能类似于R bootstrap中提供的功能:

http://statistics.ats.ucla.edu/stat/r/library/bootstrap.htm

搜索我发现:

http://mjtokelly.blogspot.com/2006/04/bootstrap-statistics-in-python.html(代码的链接被破坏)

http://adorio-research.org/wordpress/?p=9048

https://github.com/cgevans/scikits-bootstrap

但是上面的这些似乎并没有提供所有功能(特别是概率权重).

有什么指针吗?

这最近被添加到numpy.random

谢谢

最佳答案
如果您只是在寻找R的示例函数的python版本,请尝试以下方法:

import collections
import random
import bisect

def sample(xs,sample_size = None,replace=False,sample_probabilities = None):
    """Mimics the functionality of http://statistics.ats.ucla.edu/stat/r/library/bootstrap.htm sample()"""

    if not isinstance(xs,collections.Iterable):
        xs = range(xs)
    if not sample_size:
        sample_size = len(xs)            

    if not sample_probabilities:
        if replace:
            return [random.choice(xs) for _ in range(sample_size)]
        else:
            return random.sample(xs,sample_size)
    else:
        if replace:
            total,cdf = 0,[]
            for x,p in zip(xs,sample_probabilities):
                total += p
                cdf.append(total)

            return [ xs[ bisect.bisect(cdf,random.uniform(0,total)) ] 
                    for _ in range(sample_size) ]
        else:            
            assert len(sample_probabilities) == len(xs)
            xps = list(zip(xs,sample_probabilities))           
            total = sum(sample_probabilities)
            result = []
            for _ in range(sample_size):
                # choose an item based on weights,and remove it from future iterations.
                # this is slow (N^2),a tree structure for xps would be better (NlogN)
                target = random.uniform(0,total)
                current_total = 0                
                for index,(x,p) in enumerate(xps):
                    current_total += p
                    if current_total > target:
                        xps.pop(index)
                        result.append(x)
                        total -= p
                        break
            return result

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读