布尔索引在数组中

huangapple go评论67阅读模式
英文:

Boolean Indexing in Array

问题

# 返回所有名字为 "Bob" 的数据值。
data[names == "Bob"]

# 返回名字为 "Bob" 对应的数据的第二列及以后的值。
data[names == "Bob", 1:]

# 返回名字为 "Bob" 对应的数据的第二列的值。
data[names == "Bob", 1]
英文:
names = np.array(["Bob", "Joe", "Will", "Bob", "Will", "Joe", "Joe"])
data = np.array([[4, 7], [0, 2], [-5, 6], [0, 0], [1, 2], [-12, -4], [3, 4]])

In: data[names == "Bob"]
Out: array([[4, 7], [0, 0]])

In: data[names = "Bob", 1:]
Out: array([[7], [0]])

In: data[names = "Bob", 1]
Out: array([7, 0])

I have 2 arrays and 3 Python statements here. I know that the first one would return all data values that matched with the name "Bob". But I don't know yet about the last 2 statements. The second one returned the second values of each paid of data, and the final one retunred a combination of that two values? Can anyone explain this to me, please? Thanks for your help!

答案1

得分: 1

这是切片操作。

Numpy的ndarrays支持在单个方括号内进行多维索引。每个值表示一个维度。

由于你的数组是二维的,你可以提供两个表达式 data[a, b]

  • a 将过滤第一维度,选择一些子数组,例如 data[names == "Bob"]

  • b 将在第二维度中进行过滤,在每个子数组中选择一些值


使用普通列表

>>> values = [0,8,6,4,2,3,1,5,7]

>>> values[4]
2

>>> values[4:7] # 从索引4(包括)到7(不包括)的范围
[2, 3, 1]

在多维中相同

>>> data = np.array([[4, 7], [0, 2], [-5, 6], [0, 0], [1, 2], [-12, -4], [3, 4]])

# 选择每个子数组,但仅选择每个子数组的第一个值
>>> data[:, 0]
array([  4,   0,  -5,   0,   1, -12,   3])

# 选择每个子数组,但仅选择每个子数组的第二个值
>>> data[:, 1]
array([ 7,  2,  6,  0,  2, -4,  4])

# 选择每个子数组,但仅选择从第一个值开始的部分,因此每个都是一个数组
>>> data[:, 1:]
英文:

That is slicing.

Numpy ndarrays supports multi-dimension indexing in single brackets. Each value means a dimension.

As your array is in 2 dimension, you can provide 2 expressions data[a, b]

  • a will filter the 1st dimension, select some subarrays like data[names == "Bob"]

  • b will filter in the 2nd dimension, select some values on each subarrays


With simple lists

>>> values = [0,8,6,4,2,3,1,5,7]

>>> values[4]
2

>>> values[4:7] # from index 4 included to 7 excluded
[2, 3, 1]

<!-- -->

Same in multiple dimensions

&gt;&gt;&gt; data = np.array([[4, 7], [0, 2], [-5, 6], [0, 0], [1, 2], [-12, -4], [3, 4]])

# select every subarrays, but only each first value
&gt;&gt;&gt; data[:, 0]
array([  4,   0,  -5,   0,   1, -12,   3])

# select every subarrays, but only each second value
&gt;&gt;&gt; data[:, 1]
array([ 7,  2,  6,  0,  2, -4,  4])

# select every subarrays, but only FROM first value, so each is an array
&gt;&gt;&gt; data[:, 1:]
array([[ 7],
       [ 2],
       [ 6],
       [ 0],
       [ 2],
       [-4],
       [ 4]])

huangapple
  • 本文由 发表于 2023年5月21日 17:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定