python随机抽样_掌握python中的随机抽样

python随机抽样

Python provides many useful tools for random sampling as well as functions for generating random numbers. Random sampling has applications in statistics where often times a random subset of a population is observed and used to make inferences about the overall population. Further, random number generation has many application in the sciences. For example, in chemistry and physics Monte Carlo simulations require random number generation. In this post, we will discuss how to randomly sample items from lists as well as how to generate pseudorandom numbers in python.

Python提供了许多有用的工具来进行随机采样以及生成随机数的功能。 随机抽样在统计中有应用,在这种情况下,通常会观察到总体的随机子集并用于推断总体。 此外,随机数生成在科学中具有许多应用。 例如,在化学和物理学中,蒙特卡洛模拟需要随机数生成。 在这篇文章中,我们将讨论如何从列表中随机抽样项目以及如何在python中生成伪随机数。

Let’s get started!

让我们开始吧!

The random module in python has many functions that are useful for generating random numbers and random sampling.

python中的random模块具有许多函数,可用于生成随机数和随机采样。

使用“ random.choice()”在列表中选择随机项目 (Picking Random Items in a List using ‘random.choice()’)

Consider a list of BMI values for people living in a rural area:

考虑一个生活在农村地区的人的BMI值列表:

bmi_list = [29, 18, 20, 22, 19, 25, 30, 28,22, 21, 18, 19, 20, 20, 22, 23]

Let’s use the ‘random.choice()’ method to randomly select individual BMI values from this list:

让我们使用“ random.choice()”方法从此列表中随机选择单个BMI值:

import random 
print("First random choice:", random.choice(bmi_list))
print("Second random choice:", random.choice(bmi_list))
print("Third random choice:", random.choice(bmi_list))
Image for post

If we run this code once more, we should get another set of randomly selected BMIs:

如果再次运行此代码,则应获得另一组随机选择的BMI:

Image for post

使用“ random.sample()”在列表中选择随机项目 (Picking Random Items in a List using ‘random.sample()’)

The ‘random.sample()’ method is useful for randomly sampling N items from a list. For example, if we’d like to sample N=5 items from our BMI list we do the following:

“ random.sample()”方法对于从列表中随机抽样N个项目很有用。 例如,如果我们想从BMI列表中抽样N = 5个项目,请执行以下操作:

print("Random sample, N = 5 :", random.sample(bmi_list, 5))
Image for post

Let’s try sampling 10 items:

让我们尝试采样10个项目:

print("Random sample, N = 10:", random.sample(bmi_list, 10))
Image for post

使用“ random.shuffle()”随机重排列表中的项目 (Randomly Shuffling Items in a List using ‘random.shuffle()’)

In addition to random selection and sampling, the random module has a function for shuffling items in a list. Let’s print our BMI list and then print the result of shuffling our BMI list:

除了随机选择和采样外,随机模块还具有对列表中的项目进行混排的功能。 让我们打印BMI列表,然后打印改组BMI列表的结果:

print("BMI list: ", bmi_list)
random.shuffle(bmi_list)
print("Shuffled BMI list: ", bmi_list)
Image for post

使用'random.randint()'生成随机整数 (Generating Random Integers using ‘random.randint()’)

The random module has a function for generating a random integer provided a range of values. Let’s generate a random integer in the range from 1 to 5:

随机模块具有用于生成提供一定范围值的随机整数的功能。 让我们生成一个介于1到5之间的随机整数:

print("Random Integer: ", random.randint(1,5))
Image for post

Using this function, we can easily generate a list of random integers in a for-loop:

使用此函数,我们可以轻松地在for循环中生成随机整数列表:

random_ints_list = []
for i in range(1,50):
n = random.randint(1,5)
random_ints_list.append(n)
print("My random integer list: ", random_ints_list)
Image for post

生成随机浮点值 (Generating Random Floating Point Values)

The random module also has a function for generating a random floating point value between 0 and 1:

随机模块还具有生成介于0和1之间的随机浮点值的功能。

print("Random Float: ", random.random())
Image for post

We can also generate a list of random floats between 0 and 1:

我们还可以生成一个介于0到1之间的随机浮动列表:

random_float_list = []
for i in range(1,5):
n = random.random()
random_float_list.append(n)
print("My random float list: ", random_float_list)
Image for post

Further, we can scale the random float numbers. If we want random numbers between 0 and 500 we just multiply our random number by 500:

此外,我们可以缩放随机浮点数。 如果我们想要0到500之间的随机数,我们只需将随机数乘以500:

random_float_list = []
for i in range(1,5):
n = random.random()*500
random_float_list.append(n)
print("My random float list: ", random_float_list)
Image for post

And if we want to add a lower bound as well we can add a conditional statement before appending. For example to generate random numbers between 100 and 500 we do the following:

如果我们也想添加一个下限,我们可以在追加之前添加一个条件语句。 例如,要生成100到500之间的随机数,请执行以下操作:

random_float_list = []
for i in range(1,10):
n = random.random()*500
if n>=100.0:
random_float_list.append(n)
print("My random float list: ", random_float_list)
Image for post

使用“ random.uniform()”计算均匀分布的数字 (Computing Uniformly Distributed Numbers with ‘random.uniform()’)

The random module has a function for computing uniformly distributed numbers. For example, to generate 50 uniformly distributed numbers between -10 and 1 we do the following:

随机模块具有用于计算均匀分布的数字的功能。 例如,要生成介于-10和1之间的50个均匀分布的数字,请执行以下操作:

import numpy as np
uniform_list = np.random.uniform(-10,1,50)
print("Uniformly Distributed Numbers: ", uniform_list)
Image for post

使用“ random.gauss()”计算正态分布数 (Computing Normally Distributed Numbers with ‘random.gauss()’)

Finally, the random module has a function for computing normally distributed numbers. For example, to generate 50 normally distributed numbers between -50 and 0 we do the following:

最后,随机模块具有计算正态分布数的功能。 例如,要生成介于-50和0之间的50个正态分布的数字,请执行以下操作:

normal_list = np.random.uniform(-50,0,50)
print("Normally Distributed Numbers: ", normal_list)
Image for post

I’ll stop here but I encourage you to play around with the code yourself.

我将在这里停止,但我鼓励您自己尝试使用该代码。

结论 (CONCLUSIONS)

To summarize, we discussed how to randomly select and sample items from lists in python. We showed how to use the ‘random.choice()’ method to select a single item randomly from a list. We also used the ‘random.sample()’ method, which allows you to randomly select N items from a list. We also discussed how to shuffle items in a list using the ‘random.shuffle()’ method. Additionally, we showed how to generate random numbers using the random module. We generated random integers using ‘random.randint()’and random floating point values using ‘random.random()’. Finally, we went over how to generate uniformly and normally distributed numbers with ‘random.uniform()’ and ‘random.gauss()’ respectively. I hope you found this post useful/interesting. The code in this post is available on GitHub. Thank you for reading!

总而言之,我们讨论了如何从python列表中随机选择和采样项目。 我们展示了如何使用“ random.choice()”方法从列表中随机选择一个项目。 我们还使用了“ random.sample()”方法,该方法允许您从列表中随机选择N个项目。 我们还讨论了如何使用“ random.shuffle()”方法对列表中的项目进行随机排序。 此外,我们展示了如何使用随机模块生成随机数。 我们使用“ random.randint()”生成随机整数,并使用“ random.random()”生成随机浮点值。 最后,我们讨论了如何分别使用“ random.uniform()”和“ random.gauss()”生成均匀和正态分布的数字。 我希望您发现这篇文章有用/有趣。 这篇文章中的代码可以在GitHub找到 。 感谢您的阅读!

翻译自: https://towardsdatascience.com/mastering-random-sampling-in-python-ac2df84b7d3f

python随机抽样

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值