Python数据分析(中英对照)·Slicing NumPy Arrays 切片 NumPy 数组

2022-12-01 15:18:48 浏览数 (1)

2.2.2: Slicing NumPy Arrays 切片 NumPy 数组

It’s easy to index and slice NumPy arrays regardless of their dimension,meaning whether they are vectors or matrices. 索引和切片NumPy数组很容易,不管它们的维数如何,也就是说它们是向量还是矩阵。 With one-dimension arrays, we can index a given element by its position, keeping in mind that indices start at 0. 使用一维数组,我们可以根据给定元素的位置对其进行索引,记住索引从0开始。 With two-dimensional arrays, the first index specifies the row of the array and the second index 对于二维数组,第一个索引指定数组的行,第二个索引指定行 specifies the column of the array. 指定数组的列。 This is exactly the way we would index elements of a matrix in linear algebra. 这正是我们在线性代数中索引矩阵元素的方法。 We can also slice NumPy arrays. 我们还可以切片NumPy数组。 Remember the indexing logic. 记住索引逻辑。 Start index is included but stop index is not,meaning that Python stops before it hits the stop index. 包含开始索引,但不包含停止索引,这意味着Python在到达停止索引之前停止。 NumPy arrays can have more dimensions than one of two. NumPy数组的维度可以多于两个数组中的一个。 For example, you could have three or four dimensional arrays. 例如,可以有三维或四维数组。 With multi-dimensional arrays, you can use the colon character in place of a fixed value for an index, which means that the array elements corresponding to all values of that particular index will be returned. 对于多维数组,可以使用冒号字符代替索引的固定值,这意味着将返回与该特定索引的所有值对应的数组元素。 For a two-dimensional array, using just one index returns the given row which is consistent with the construction of 2D arrays as lists of lists, where the inner lists correspond to the rows of the array. 对于二维数组,只使用一个索引返回给定的行,该行与二维数组作为列表的构造一致,其中内部列表对应于数组的行。 Let’s then do some practice. 然后让我们做一些练习。 I’m first going to define two one-dimensional arrays,called lower case x and lower case y. 我首先要定义两个一维数组,叫做小写x和小写y。 And I’m also going to define two two-dimensional arrays,and I’m going to denote them with capital X and capital Y. Let’s first see how we would access a single element of the array. 我还将定义两个二维数组,我将用大写字母X和大写字母Y表示它们。让我们先看看如何访问数组中的单个元素。 So just typing x square bracket 2 gives me the element located at position 2 of x. 所以只要输入x方括号2,就得到了位于x的位置2的元素。 I can also do slicing. 我也会做切片。 So I can specify the start index and the end index, in which case I get two elements here from the x array, the numbers 1 and 2. 所以我可以指定开始索引和结束索引,在这种情况下,我从x数组中得到两个元素,数字1和2。 If you look at the sizes of x and y, each of them has exactly three elements. 如果你看x和y的大小,它们都有三个元素。 That means that we can add those two arrays up. 这意味着我们可以将这两个数组相加。 So I can type x plus y, which gives me a new array called z. 所以我可以输入x加y,这给了我一个新的数组,称为z。 In this case, the elements of z will be element-wise additions from the vectors x and y. 在这种情况下,z的元素将是向量x和y的元素相加。 So the first element of x is added to the first element of y, and so on. 因此,x的第一个元素被添加到y的第一个元素,依此类推。 Now moving on to two-dimensional arrays,we can also investigate individual rows or columns of arrays. 现在转到二维数组,我们还可以研究数组的单个行或列。 Typing X square bracket colon comma 1 gives me access to the first column of the table X. I can do the same for Y,and now I have to first column of the two-dimensional array, Y. 键入X方括号冒号逗号1可以访问表X的第一列。我可以对Y执行相同的操作,现在我必须访问二维数组的第一列Y。 I can also add these two up. 我也可以把这两个加起来。 So I can type X plus Y, again colon comma 1. 所以我可以输入X加Y,再次输入冒号逗号1。 In this case, I have added together the first columns of these two arrays. 在本例中,我将这两个数组的前几列相加。 To extract the first row of X, I type, within square brackets,1 comma colon which gives me all of the elements in the first row. 要提取X的第一行,我在方括号内键入1个逗号冒号,它将给出第一行中的所有元素。 In this case, these are numbers 4, 5, and 6. 在本例中,这些是数字4、5和6。 I can take also the first row of Y, and I can then add these two arrays up. 我也可以取Y的第一行,然后我可以把这两个数组相加。 Because two-dimensional arrays are defined as nested rows,I can use a shorthand notation to access the first row of X, which in this case 因为二维数组被定义为嵌套行,所以我可以使用简写符号来访问X的第一行,在本例中是这样的 would be just X square brackets 1, and this gives me the same exact output as typing X square bracket 1 comma colon. 将是X方括号1,这给了我与键入X方括号1逗号冒号相同的精确输出。 One word of caution — what happens if we take two lists and put a plus sign between them? 警告一句——如果我们拿两个列表并在它们之间加一个加号,会发生什么? Well, we can give it a try. 嗯,我们可以试一试。 I can define a list which consists of elements 2 and 4. 我可以定义一个由元素2和4组成的列表。 I have a plus sign followed by another list with elements 6 and 8. 我有一个加号,后面是另一个包含元素6和8的列表。 Remember, putting a plus sign between two lists concatenates those two lists, resulting in a new list which is longer than the two lists that were added together. 请记住,在两个列表之间放置加号将连接这两个列表,从而生成一个新列表,该列表比添加在一起的两个列表长。 Now let’s look at a different example. 现在让我们看一个不同的例子。 What happens if we first turn those lists into NumPy arrays,and then have a plus sign between them? 如果我们首先将这些列表转换为NumPy数组,然后在它们之间加上一个加号,会发生什么? I’m going to take my previous line here. 我将在这里接受我的前一行。 I’ll just turn this into a NumPy array. 我会把它变成一个小数组。 So my first NumPy array has two elements, 2 and 4. 所以我的第一个NumPy数组有两个元素,2和4。 I’m going to add that to another NumPy array, which has elements 6 and 8. 我将把它添加到另一个NumPy数组中,它包含元素6和8。 In this case, what’s happening is we have two one-dimensional arrays. 在这种情况下,我们有两个一维数组。 And what we’ve accomplished here is an element-wise addition between these two arrays. 我们在这里完成的是这两个数组之间的元素加法。

0 人点赞