[英]What is the meaning of “a[:,1]” in python slicing
I was reading about python slicing but I didn't able to figure this out.我正在阅读有关 python 切片的信息,但我无法弄清楚这一点。
clf.predict_proba(X_test)[:,1]
Then I tried to test myself with simple list.然后我尝试用简单的列表来测试自己。
a = [2,4,6,7,7,8]
>>> a[:,1]
-----> TypeError: list indices must be integers or slices, not tuple
There is a big difference when working with numpy arrays as is the result of clf.predict_proba(X_test)[:,1]
and a list
:使用 numpy arrays 时存在很大差异,因为
clf.predict_proba(X_test)[:,1]
和list
的结果是:
As it has been mentioned in the comments, lists can be sliced with single values, not comma separated because of their structure, whereas a numpy array with might be n-dimensional, can be sliced within with the comma separated value to indicate number of rows and columns, just like pd.DataFrame.iloc[] does.正如评论中提到的那样,列表可以用单个值切片,而不是因为它们的结构而用逗号分隔,而 numpy 数组可能是 n 维的,可以用逗号分隔的值切片以指示行数和列,就像pd.DataFrame.iloc[]一样。
np.array([1,1],[2,3],[4,3])
ex_list = [[1,1],[2,3],[4,3]]
But how does this actually look like?但这实际上看起来如何? Well in the case of lists, they are 1-dimensial or
flat
whereas this array is not.那么在列表的情况下,它们是一维或
flat
的,而这个数组不是。
1 arr_example
Has 3 rows and 2 columns: 1
arr_example
有 3 行 2 列:
array([[1, 1],
[2, 3],
[4, 3]])
2 ex_list
: 2
ex_list
:
[[1,1],[2,3],[4,3]]
If you want to access the inner value of the nested list, then the indexing must be done outside the first slicer as you can see in the example below:如果要访问嵌套列表的内部值,则必须在第一个切片器之外完成索引,如下例所示:
arr_example[:1,0] # arr_example[rows,columns]
list_example[:1][0][0]
In this case in arr_example
we are selecting the rows from start up to,but not including 1 (position 1, therefore only the first row) and the first column (position 0).在这种情况下,在
arr_example
中,我们选择从 start 到的行,但不包括 1(位置 1,因此只有第一行)和第一列(位置 0)。 Looking at the the structure of the data and understanding how the slicing works, the following outputs make sense:查看数据的结构并了解切片的工作原理,以下输出是有意义的:
array([1])
1
Hy!嗨! @martin
a[:,1:]
is used to slice 2-dimensional NumPy array for example. @martin
a[:,1:]
例如用于对二维 NumPy 数组进行切片。
a = [[1,2,3,4,5], [6,5,3,2,6]]
represent as表示为
a = [[1, [ 6,
2, 5,
3, 3,
4, 2,
5 6
], ]]
than a[:,1]
== a[col[start]: col[end], row[start]: row[end]]
比
a[:,1]
== a[col[start]: col[end], row[start]: row[end]]
will be [[2,5]]
means take both column and row at 1st index. will be
[[2,5]]
表示在第一个索引处同时获取列和行。
x[:,1]
is translated by the interpreter into x[:,1]
被解释器翻译成
x.__getitem__((slice(None), 1))
That is, it calls the __getitem__
method of the x
object, passing it (in this case) a tuple argument.也就是说,它调用
x
object 的__getitem__
方法,向它(在这种情况下)传递一个元组参数。 The :
is translated into a slice
object. :
被翻译成一个slice
object。 It's the comma in the indexing that creates a tuple.索引中的逗号创建了一个元组。
If x
is a numpy array, indexing with a tuple makes sense (subject to its own rules).如果
x
是 numpy 数组,则使用元组进行索引是有意义的(受其自身规则的约束)。 But as your error indicates, indexing with a tuple
does not work for a list.但是正如您的错误所示,使用
tuple
进行索引不适用于列表。 The error says what's allowed.错误说明了允许的内容。
So while python syntax allows this kind of indexing in general, the details are class dependent.因此,虽然 python 语法通常允许这种索引,但细节取决于 class。
For a 2d array, [:,1]
means select the 2nd column.对于二维数组,
[:,1]
表示 select 第 2 列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.