python - 如何在 Python 中从总体中生成随机样本?

标签 python random sample

<分区>

我正在尝试解决这个问题:

Generate 1,000 random samples of size 50 from population. Calculate the mean of each of these samples (so you should have 1,000 means) and put them in a list norm_samples_50.

我的猜测是我必须使用 randn 函数,但我不能完全猜测如何根据上述问题形成语法。我已完成研究,但找不到合适的答案。

最佳答案

使用 Numpy非常有效的解决方案。

import numpy


sample_list = []

for i in range(50): # 50 times - we generate a 1000 of 0-1000random - 
    rand_list = numpy.random.randint(0,1000, 1000)
    # generates a list of 1000 elements with values 0-1000
    sample_list.append(sum(rand_list)/50) # sum all elements

Python 一行代码

from numpy.random import randint


sample_list = [sum(randint(0,1000,1000))/50 for _ in range(50)]

为什么要使用 Numpy?它非常高效且非常准确(十进制)。这个库就是为这些类型的计算和数字而创建的。使用标准库中的 random 很好,但速度和可靠性都不尽如人意。

关于python - 如何在 Python 中从总体中生成随机样本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50091999/

相关文章:

ios - iOS 采样器支持的示例/文件扩展名

google-app-engine - 是否有任何 Google 的 App Engine 示例应用程序是用 Java 编写的?

javascript - 浏览器中的特殊鼠标事件 : wheel, 右键单击​​?

python - NDB 查询返回零结果。数据存储显示结果

python - 更改文件行 - Python

python - django ldap auth 无法从应用程序运行

JavaScript - 将随机数/变量插入动态超链接?

algorithm - 了解均匀随机数生成

java - 如何在数组中生成唯一的随机数

python - 如何将 wiris 添加到 django-ckeditor