[英]Python Numpy syntax: what does array index as two arrays separated by comma mean?
I don't understand array as index in Python Numpy. 我不理解数组作为Python Numpy中的索引。 For example, I have a 2d array A in Numpy
例如,我在Numpy中有一个二维数组A
[[1,2,3]
[4,5,6]
[7,8,9]
[10,11,12]]
What does A[[1,3], [0,1]] mean? A [[1,3],[0,1]]是什么意思?
Just test it for yourself! 自己测试一下!
A = np.arange(12).reshape(4,3)
print(A)
>>> array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
By slicing the array the way you did ( docs to slicing ), you'll get the first row, zero-th column element and the third row, first column element. 通过按照您的方式对数组进行切片 ( 切片的文档 ),您将获得第一行第零列的元素和第三行第一列的元素。
A[[1,3], [0,1]]
>>> array([ 3, 10])
I'd highly encourage you to play around with that a bit and have a look at the documentation and the examples. 我强烈鼓励您尝试一下,看看文档和示例。
Your are creating a new array: 您正在创建一个新数组:
import numpy as np
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]]
A = np.array(A)
print(A[[1, 3], [0, 1]])
# [ 4 11]
See Indexing, Slicing and Iterating in the tutorial . 请参阅本教程中的索引,切片和迭代 。
Multidimensional arrays can have one index per axis.
多维数组每个轴可以有一个索引。 These indices are given in a tuple separated by commas
这些索引在以逗号分隔的元组中给出
Quoting the doc: 引用文档:
def f(x,y):
return 10*x+y
b = np.fromfunction(f, (5, 4), dtype=int)
print(b[2, 3])
# -> 23
You can also use a NumPy array as index of an array. 您也可以使用NumPy数组作为数组的索引。 See Index arrays in the doc.
请参阅文档中的索引数组 。
NumPy arrays may be indexed with other arrays (or any other sequence- like object that can be converted to an array, such as lists, with the exception of tuples; see the end of this document for why this is).
NumPy数组可以与其他数组(或可以转换为数组的任何其他类似序列的对象(如元组除外)建立索引;关于元组的原因,请参阅本文档的结尾)。 The use of index arrays ranges from simple, straightforward cases to complex, hard-to-understand cases.
索引数组的使用范围从简单,直接的案例到复杂的,难以理解的案例。 For all cases of index arrays, what is returned is a copy of the original data, not a view as one gets for slices.
对于所有索引数组,返回的都是原始数据的副本,而不是切片的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.