为了账号安全,请及时绑定邮箱和手机立即绑定

当样本长度与总体相同时,Python random.sample 给出“样本大于总体或为负”

当样本长度与总体相同时,Python random.sample 给出“样本大于总体或为负”

茅侃侃 2021-06-08 21:41:57
如果我的总体中的项目数等于我想要的样本数,我会收到错误消息。这是一个最小的例子import randomsubset = random.sample( set([312996, 529565, 312996, 130934]) ,  4)---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-9-b816cd5c3651> in <module>()----> 1 subset = random.sample( set([312996, 529565, 312996, 130934]) ,  4)/opt/conda/lib/python3.6/random.py in sample(self, population, k)    318         n = len(population)    319         if not 0 <= k <= n:--> 320             raise ValueError("Sample larger than population or is negative")    321         result = [None] * k    322         setsize = 21        # size of a small set minus size of an empty listValueError: Sample larger than population or is negative编辑似乎这只发生在这 4 个数字上。我试过了import randomsubset =  random.sample( set([2, 5, 8, 9]) ,  4)我没有收到错误。我无法弄清楚第一个问题是什么。. .
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 281 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号