带逗号的切片符号,Numpy - Python [英] Slice Notation with Comma , Numpy - Python
问题描述
我遇到过这样的事情:
list(training[:,0])
我搜索并了解到这些正在 Numpy Arrays 中使用.但还是没明白这背后的逻辑.它说 [:,0]
的部分是什么意思?它从训练数组中选择哪些部分?
I searched and learned that these are being used at Numpy Arrays. But still didn't understand the logic behind this. The part where it says [:,0]
what does that mean? Which parts does it choose from the training array?
training = np.array(training)
#create train and test lists.
train_x = list(training[:,0])
train_y = list(training[:,1])
print("Training data created")
推荐答案
考虑 numpy 数组
Consider the numpy array
import numpy as np
myArr = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
对于任何一般数组 arr[a:b, c:d]
意味着我们必须考虑从索引 a 到索引 b-1 的行和从索引 c 到索引 d-1 的列
For any general array arr[a:b, c:d]
means that we have to consider rows from index a to index b-1 and columns from index c to index d-1
myArr[1:2, 1:2]
意味着我们必须考虑从索引 1 到索引 2-1(即 1)的行和从索引 1 到 2-1(即1) 或者换句话说,我们有第 1 行和第 1 列的元素,在我们的例子中是 5
myArr[1:2, 1:2]
means that we have to consider rows from index 1 to index 2-1(i.e 1) and columns from index 1 to 2-1(i.e 1) or in other words, we have the element at row 1 and column 1 which is 5
in our case
记住行索引从0开始,列索引也是从0开始
Remember that row index starts from 0 and the column index also starts from 0
myArr[:,1:3]
在这里你可以看到 ':' 意味着我们必须考虑从索引 1 到 (3-1) 即 2 的所有行和列
myArr[:,1:3]
here you can see that ':' implies that we have to consider all rows and columns from index 1 to (3-1) i.e. 2
在这种情况下,我们的输出将是
in this case our output will be
array([[2, 3],
[5, 6],
[8, 9]])
看到我们在第一个位置使用 ':' 得到了所有行,我们得到了第二列(索引 1)和第三列(索引 2)
See we got all the rows using ':' in the first position and we got 2nd (index 1) and 3rd (index 2) column
同样在列侧使用':'将获取所有列
Likewise using ':' in columns side will fetch you all the columns
这篇关于带逗号的切片符号,Numpy - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!