Python数据分析(中英对照)·Indexing NumPy Arrays 索引 NumPy 数组
2.2.3: Indexing NumPy Arrays 索引 NumPy 数组
NumPy arrays can also be indexed with other arrays or other sequence-like objects like lists.
NumPy数组也可以与其他数组或其他类似于序列的对象(如列表)建立索引。
Let’s take a look at a few examples.
让我们来看几个例子。
I’m first going to define my array z1.
我首先要定义我的数组z1。
And let’s put in a few elements in there– 1, 3, 5, 7, and 9, for example.
让我们把一些元素放进去,比如1,3,5,7和9。
I can then define a new array called z2, which is just z1 with one added to every single element of the array.
然后我可以定义一个名为z2的新数组,它只是z1,数组的每个元素都添加了一个。
We can now look at these two arrays to see what their contents are.
现在我们可以看看这两个数组,看看它们的内容是什么。
I can now define a list called n, which I will be using to index my z1 and z2.
我现在可以定义一个名为n的列表,我将使用它来索引我的z1和z2。
So let me put in the elements 0, 2 and 3 in my index.
让我把元素0,2和3放在索引中。
I can now type z1, square bracket, ind, which gives me access to the elements that are located within z1 at the locations that
我现在可以输入z1,方括号,ind,这使我能够访问位于z1内的元素,这些元素位于
are specified by ind.
由ind指定。
In this example, index or ind, was defined as a Python list,but we could also have defined that as a NumPy array.
在本例中,index或ind被定义为Python列表,但我们也可以将其定义为NumPy数组。
So I can take my previous list, 0, 2, 3, turn that into a NumPy array,and I can still do my indexing.
所以我可以把我以前的列表,0,2,3,变成一个NumPy数组,我仍然可以做我的索引。
In other words, we can index NumPy arrays
换句话说,我们可以索引NumPy数组
using either lists or other NumPy arrays.
使用列表或其他NumPy数组。
NumPy arrays can also be indexed using logical indices,but what does that actually mean?
NumPy数组也可以使用逻辑索引进行索引,但这实际上意味着什么?
Just as we can have an array of numbers, we can have an array consisting of true and false, which are two Boolean elements.
正如我们可以有一个数字数组一样,我们也可以有一个由true和false组成的数组,它们是两个布尔元素。
Let’s first construct a Boolean array.
让我们首先构造一个布尔数组。
I’m going to define my z1 again here as before.
我将像以前一样在这里再次定义我的z1。
I can now type something like z1 greater than 6.
我现在可以输入z1大于6的值。
The element 0 of z1 is not greater than 6,and therefore element 0 of the Boolean array that Python returns to you is false.
z1的元素0不大于6,因此Python返回给您的布尔数组的元素0为false。
The same is true for elements 1 and 2 of z1, so we get a false there as well.
z1的元素1和2也是如此,所以我们也得到了一个false。
Now element 3 of Z1 is greater than 6.
现在Z1的元素3大于6。
Its value happens to be 7, so therefore, the corresponding element in the Boolean array is true.
它的值正好是7,因此,布尔数组中的对应元素为true。
And in fact, all subsequent elements are also greater than 6,so therefore all subsequent elements in the Boolean array are also true.
事实上,所有后续元素也都大于6,因此布尔数组中的所有后续元素也是真的。
We can use the Boolean array, also called a logical array, to index another vector.
我们可以使用布尔数组,也称为逻辑数组,来索引另一个向量。
But what are the elements that are returned in that case?
但是在这种情况下返回的元素是什么?
As you may have guessed, Python returns those elements of the array for which the corresponding value in the Boolean vector is true.
正如您可能已经猜到的,Python返回布尔向量中对应值为true的数组元素。
Let’s look at an example.
让我们看一个例子。
Let’s first use the logical index vector to access elements of z1.
让我们首先使用逻辑索引向量访问z1的元素。
I’m interested in accessing elements of z1, I therefore start with z1.
我对访问z1的元素感兴趣,因此我从z1开始。
I need my square brackets as usual.
我像往常一样需要方括号。
Inside the square brackets, I need to put in my logical index vector, which is, in this case, z1 greater than 6.
在方括号内,我需要放入逻辑索引向量,在本例中,z1大于6。
In this case, Python returns to me those elements of the z1 array that happened to be greater than 6 in value.
在本例中,Python向我返回z1数组中那些值恰好大于6的元素。
But I can also do the same to my array z2.
但是我也可以对我的数组z2做同样的处理。
Now let’s think about this, what’s happening here.
现在让我们想想,这里发生了什么。
My index vector is still identifying those elements of z1 that happened to be greater than six.
我的索引向量仍然在识别z1中碰巧大于6的元素。
Now in this case, I am however, using that index vector to access elements in my array z2.
现在在这个例子中,我使用这个索引向量来访问数组z2中的元素。
I can run this and I get an output.
我可以运行它并得到一个输出。
I could also define my logical vector in a slightly different way.
我也可以用稍微不同的方式定义我的逻辑向量。
I could explicitly construct a logical vector.
我可以显式地构造一个逻辑向量。
In this case, I’ve typed ind equals z1 greater than 6.
在本例中,我输入的ind等于大于6的z1。
If we inspect the content of ind, we’ll see that it’s a logical vector.
如果我们检查ind的内容,就会发现它是一个逻辑向量。
I can now type z1, square bracket, ind, and I get the same output as before.
我现在可以输入z1,方括号,ind,得到和以前一样的输出。
Similarly, I can type z2, ind, and the output is the same.
类似地,我可以输入z2,ind,并且输出是相同的。
One final word about indexing NumPy arrays– and this is really important because it can easily lead to subtle programming errors.
关于索引NumPy数组的最后一句话——这非常重要,因为它很容易导致微妙的编程错误。
When you slice an array using the colon operator, you get a view of the object.
使用冒号操作符切片数组时,将获得对象的视图。
This means that if you modify it, the original array will also be modified.
这意味着如果修改它,原始数组也将被修改。
This is in contrast with what happens when you index an array, in which case what is returned to you is a copy of the original data.
这与索引数组时的情况相反,在这种情况下,返回给您的是原始数据的副本。
Let’s clarify this distinction with examples.
让我们用例子来阐明这一区别。
I’m going to define my z2 as before.
我将像以前一样定义我的z2。
Then, I will define a new vector "w" in this case.
然后,在这种情况下,我将定义一个新的向量“w”。
I’m just going to slice that, let’s say, from 0 to 3.
我将把它从0切到3。
I can look at the contents of my w.
我可以看看我的w。
In this case, I choose to modify the element at location zero.
在本例中,我选择修改位置为零的元素。
So I’m just going to type w square brackets 0, is equal to 3.
所以我要输入w,方括号0,等于3。
I can now inspect the content of w, and instead of having 1, 3, and 5,I have 3, 3, and 5, as we would have expected.
我现在可以检查w的内容,而不是1,3和5,我有3,3和5,正如我们所期望的。
However, if I now type z1, you will see that the first element at location is 0 of that array has also been modified.
但是,如果我现在键入z1,您将看到该数组位置0处的第一个元素也已修改。
Let’s see what happens if we use indexing and not slicing to access an array.
让我们看看如果我们使用索引而不是切片来访问数组会发生什么。
As before, I’m going to define my z1 vector.
和前面一样,我将定义z1向量。
Then I need to define my index vector, which I’m going to call 0, 1, and 2.
然后我需要定义我的索引向量,我将其称为0、1和2。
In this case, I’ve defined this as a list,but if I wanted to define that as an array,I simply place NumPy.array outside of my list.
在本例中,我将其定义为一个列表,但如果我想将其定义为一个数组,我只需将NumPy.array放在列表之外。
Now I have two elements defined.
现在我定义了两个元素。
I have my z1 and I have my index.
我有我的z1和我的索引。
I can now do the following– I’m going to create w by taking my object z1 and indexing that using my index vector.
我现在可以做以下事情了——我将通过获取对象z1并使用索引向量对其进行索引来创建w。
The contents of w here are 1, 3, and 5.
这里w的内容是1、3和5。
I can now look at the element 0 of w and I can set that to be equal to 3.
我现在可以看w的元素0,我可以把它设为等于3。
If I now look at w, I I’ll see that the element, the first element,located at position 0 has been modified to 3.
如果我现在看w,我会看到位于位置0的元素,第一个元素,被修改为3。
If I now go back to my array z1, I see that the first element, number 1,has not been modified.
如果我现在回到我的数组z1,我看到第一个元素,数字1,没有被修改。
In summary, for all cases of indexed arrays, what is returned is a copy of the original data, not a view as one gets for slices.
总之,对于索引数组的所有情况,返回的都是原始数据的副本,而不是切片的视图。
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Indexing NumPy Arrays 索引 NumPy 数组
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
- 是否提供免费更新服务?
- 持续更新,永久免费
- 是否经过安全检测?
- 安全无毒,放心食用