Python数据分析(中英对照)·Simulating Randomness 模拟随机性
2.4.1: Simulating Randomness 模拟随机性
Many processes in nature involve randomness in one form or another.
自然界中的许多过程都以这样或那样的形式涉及随机性。
Whether we investigate the motions of microscopic molecules or study the popularity of electoral candidates,we see randomness, or at least apparent randomness, almost everywhere.
无论我们研究微观分子的运动,还是研究候选人的受欢迎程度,我们几乎处处都能看到随机性,或者至少是明显的随机性。
In addition to phenomena that are genuinely random,we often use randomness when modeling complicated systems
除了真正随机的现象外,我们在建模复杂系统时经常使用随机性
to abstract away those aspects of the phenomenon for which we do not have useful simple models.
将我们没有有用的简单模型的现象的那些方面抽象出来。
In other words, we try to model those parts of a process that we can explain in relatively simple terms,and we assume, true or not, that the rest is noise.
换句话说,我们试图对过程中那些我们可以用相对简单的术语解释的部分进行建模,并且我们假设,不管是真是假,其余部分都是噪音。
To put this differently, we model what we can,and whatever it happens to be left out, we attribute to randomness.
换一种说法,我们对我们能做的事情进行建模,不管发生什么,我们都将其归因于随机性。
These are just some of the reasons why it’s important to understand how to simulate random numbers and random processes using Python.
这些只是理解如何使用Python模拟随机数和随机进程很重要的一些原因。
We have already seen the random module.
我们已经看到了随机模块。
We will be using that to simulate simple random processes,but we’ll also take a look at some other tools the Python has to generate random numbers.
我们将使用它来模拟简单的随机过程,但我们还将看看Python生成随机数的其他一些工具。
Let’s see how we can use the random choice function to carry out perhaps the simplest random process – the flip of a single coin.
让我们看看如何使用随机选择函数来执行可能是最简单的随机过程——抛一枚硬币。
I’m first going to import the random library.
我首先要导入随机库。
So I type import random.
所以我输入import random。
Then we’ll use the random choice function.
然后我们将使用随机选择函数。
We first need parentheses.
我们首先需要括号。
And in this case, we need some type of a sequence, here a list,to contain the elements of the sequence.
在这种情况下,我们需要某种类型的序列,这里是一个列表,来包含序列的元素。
I’m going to go with two strings, H for heads and T for tails.
我要用两根弦,H代表正面,T代表反面。
If I now run this code, Python will pick one of the two elements, head or tail,from the sequence.
如果我现在运行这段代码,Python将从序列中选择两个元素中的一个,head或tail。
If I run this several times, occasionally I get a head.
如果我运行几次,偶尔会有一个头部。
Occasionally I get a tail, as expected.
正如预期的那样,偶尔我会得到一条尾巴。
Often it’s more useful, however, to re-label the side of the coin with 0 to 1.
然而,通常更有用的方法是将硬币的一面重新标记为0到1。
So instead of using list of strings, as we did in the above example,we will be using a list of two numbers, two integers,as our argument of the random choice function.
因此,我们将使用两个数字、两个整数的列表作为随机选择函数的参数,而不是使用字符串列表,就像我们在上面的示例中所做的那样。
I’m going to take this previous example.
我将以前面的例子为例。
I’m going to replace the two strings with the numbers 0 and 1.
我将用数字0和1替换这两个字符串。
And everything works just the same as before.
一切都和以前一样。
The only difference is that now the outcomes are either zeros or ones.
唯一的区别是现在的结果不是0就是1。
How could we use this approach to simulate the role of a die?
我们如何使用这种方法来模拟模具的角色?
Since the outcome of a die is an integer between 1 and 6,we can use the same approach here.
因为骰子的结果是1到6之间的整数,所以我们可以在这里使用相同的方法。
I’m going to look at my previous example, wherewe picked a number, either 0 or 1.
我将看我的上一个示例,其中我们选择了一个数字,0或1。
Instead of having numbers 0 and 1, all I need to do is update the list.
我需要做的就是更新列表,而不是使用数字0和1。
In this case, I have the numbers 1, 2, 3, 4, 5, 6,which are the six possible outcomes of a standard die.
在本例中,我有数字1、2、3、4、5、6,这是标准模具的六种可能结果。
I can now roll this die simply by running this line several times.
我现在可以通过运行这条线几次来滚动这个模具。
In this case, we’re choosing one number out of six,so this is how we can simulate a die.
在本例中,我们从六个数字中选择一个,因此这就是我们可以模拟模具的方式。
We could also implement this using a range object.
我们还可以使用范围对象来实现这一点。
And in that case, we just have to be careful when specifying the start and stop values.
在这种情况下,我们只需在指定开始和停止值时小心。
So instead of a list here, we insert a range.
因此,这里不是列表,而是插入一个范围。
The first value has to be 1, and the stopping value is going to be 7.
第一个值必须是1,停止值将是7。
Remember that many Python functions, like range,stop before they hit the stop value.
请记住,许多Python函数(如range)在达到停止值之前就停止了。
In this case, we specified the range object that will include the numbers from 1 to 6.
在本例中,我们指定了范围对象,该对象将包括从1到6的数字。
Let’s try running this example now a few times,and we’ll see that it works just the same way as our list example above.
现在让我们试着运行这个示例几次,我们将看到它的工作方式与上面的列表示例相同。
When you explore the documentation for random choice,you’ll find that you don’t necessarily need to provide a list.
当您浏览随机选择的文档时,您会发现不一定需要提供列表。
Instead, any sequence object will do.
相反,任何序列对象都可以。
And because range is a sequence object, you can provide that as an argument to the choice function.
因为range是一个序列对象,所以可以将它作为参数提供给choice函数。
But it’s worth taking a moment to think about what you’re asking Python to do if you had used the following line.
但是,如果您使用了下面这行代码,那么花点时间思考一下您要求Python做什么是值得的。
Taking the previous example, I’m just going to insert my range object inside a list object.
在前面的例子中,我将把我的range对象插入一个list对象中。
I’m then going to run this line. See what’s happening?
然后我来运行这条线路。看看发生了什么?
We know that random choice expects a sequence, which is what you have provided, in this case, a list.
我们知道随机选择需要一个序列,这就是您提供的序列,在本例中是一个列表。
But that list contains only one object.
但该列表只包含一个对象。
What is that object?
那是什么东西?
It’s a range object.
这是一个范围对象。
So when you run this line, Python will always return a range 1,7 object to you because that’s the only object or sequence the list contains.
因此,当您运行这一行时,Python将始终向您返回一个范围为1,7的对象,因为这是列表中包含的唯一对象或序列。
I mention this here because it could easily lead to a programming error,although you can always try running your code in the interactive mode to make sure it does what you would like it to do.
我在这里提到这一点是因为它很容易导致编程错误,尽管您可以始终尝试在交互模式下运行代码,以确保它执行您希望它执行的操作。
Let’s build on this idea to explore a somewhat harder example.
让我们基于这个想法来探索一个更难的例子。
Imagine a situation where you have three dice, one of them having six faces,one of them having eight faces, and one of them having ten faces.
想象一个场景,你有三个骰子,其中一个有六张脸,一个有八张脸,一个有十张脸。
How could you simulate one outcome for a process, where one of these dice,chosen uniformly at random, is rolled just one time?
你怎么能为一个过程模拟一个结果,其中一个骰子是均匀随机选择的,只掷一次?
Here’s what I would do.
这是我要做的。
First, I would think about choosing a die,and then second, I would think about how to roll the die I just chose.
首先,我会考虑选择一个模具,然后,我会考虑如何滚动我刚才选择的模具。
Let’s first implement all three die and then the selection among them,and then we’ll finally simulate the role of the chosen die.
让我们首先实现所有三个骰子,然后在它们之间进行选择,最后模拟所选骰子的角色。
Let’s first construct a list of range object.
让我们首先构造一个范围对象列表。
We have our first object, our second object, and our third object.
我们有第一个物体,第二个物体,第三个物体。
The first one needs to be arranged 1,7 object, as we saw before.
第一个需要安排1,7个对象,正如我们之前看到的。
The second one is the range 1, 9 object.
第二个是范围为1,9的对象。
And the third one is range 1, 11 object.
第三个是范围1,11物体。
What we have now here is a list of three different range objects.
我们现在看到的是三个不同范围对象的列表。
If we’d like to pick one of these, we will use, again,the random choice function.
如果我们想选择其中一个,我们将再次使用随机选择函数。
So we need parentheses.
所以我们需要括号。
Now let’s try running this code.
现在让我们试着运行这段代码。
This time we got the range 1, 11 object.
这次我们得到了范围为1,11的物体。
Now we get a different object and so on.
现在我们得到一个不同的对象,等等。
So what this line of code does so far is it picks one to three range objects uniformly at random.
到目前为止,这行代码所做的是随机均匀地拾取一到三个范围对象。
But as before, we can pick one of the numbers that’s contained within a range object.
但和以前一样,我们可以选择范围对象中包含的一个数字。
So we can embed the code we have inside another random choice function.
所以我们可以把我们的代码嵌入到另一个随机选择函数中。
In this case, we’re first picking one range object.
在本例中,我们首先拾取一个范围对象。
And then we’re calling random choice on whichever range object we happened to pick.
然后我们调用随机选择来选择我们碰巧选择的任何一个范围对象。
And in this case, we get an outcome that corresponds to the process that we wanted.
在这种情况下,我们得到的结果与我们想要的过程相对应。
Why does this example work?
为什么这个例子有效?
Remember, everything is an object in Python.
记住,在Python中,一切都是一个对象。
Here, the innermost random choice first chooses one object from a sequence, here a list of range objects.
在这里,最里面的随机选择首先从序列中选择一个对象,这里是范围对象列表。
The outermost random choice chooses one of the numbers from the given range object.
最外层的随机选择从给定的范围对象中选择一个数字。
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Simulating Randomness 模拟随机性
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
- 是否提供免费更新服务?
- 持续更新,永久免费
- 是否经过安全检测?
- 安全无毒,放心食用