如何引用 numpy.ufunc.at 中的所有行
how to refer to all rows in numpy.ufunc.at
如何引用 numpy.ufunc.at
中给定列的所有行或切片
这适用于列:
c = np.array([[1,2,4],[5,3,4]])
np.add.at(c, [[0,1],], 999)
print(c)
out:
[[1000 1001 1003]
[1004 1002 1003]]
但是,这些都失败了
c = np.array([[1,2,4],[5,3,4]])
np.add.at(c, [,[0,1]], 999)
print(c)
out:
File "<ipython-input-164-ebab6f97aa81>", line 2
np.add.at(c, [,[0,1]], 999)
^
SyntaxError: invalid syntax
或
c = np.array([[1,2,4],[5,3,4]])
np.add.at(c, [:,[0,1]], 999)
print(c)
out:
File "<ipython-input-165-cef6394c4870>", line 2
np.add.at(c, [:,[0,1]], 999)
^
SyntaxError: invalid syntax
您可以像这样使用 np.s_
:
>>> c = np.array([[1,2,4],[5,3,4]])
>>> np.add.at(c, np.s_[:, [0,1]], 999)
>>> c
array([[1000, 1001, 4],
[1004, 1002, 4]])
虽然@Paul 有正确的修复,但可能需要一些解释。
np.add.at(c, [[0,1],], 999)
列表中不需要尾随逗号;它在元组中用于将单个项目元组与更简单的 () 分组区分开来。
In [197]: [[0,1],]
Out[197]: [[0, 1]]
In [198]: ([0,1],)
Out[198]: ([0, 1],)
In [199]: ([0,1])
Out[199]: [0, 1]
所以尾随逗号有意义;但是第一个产生错误
In [200]: [,[0,1]]
SyntaxError: invalid syntax
列表中的:
也是一个错误。 :
在索引表达式中被接受,但会立即转换为 slice(None)
。索引使用 []
就像一个列表,但不是一回事。 .at
的第二个参数被 Python 评估为列表或元组,而不是索引表达式。
np.add.at(c, [:,[0,1]], 999)
In [213]: [:,[0,1]]
SyntaxError: invalid syntax
In [215]: np.add.at(c, [slice(None),[0,1]],999)
In [216]: c
Out[216]:
array([[1000, 1001, 4],
[1004, 1002, 4]])
@Paul 建议的 s_
有效,因为它是一个带有自定义索引方法 (__getitem__
) 的 class。这是一个可爱又方便的技巧,但我刚才描述的基本 Python 语法仍然适用。
In [217]: np.s_[:, [0,1]]
Out[217]: (slice(None, None, None), [0, 1])
与索引 .at
一样,根据需要使用尾随 slice(None)
填充,这就是为什么您的第一个示例适用于行。
In [224]: np.add.at(c, ([0,1,0], slice(None)),100)
In [225]: c
Out[225]:
array([[201, 202, 204],
[105, 103, 104]])
如何引用 numpy.ufunc.at
中给定列的所有行或切片这适用于列:
c = np.array([[1,2,4],[5,3,4]])
np.add.at(c, [[0,1],], 999)
print(c)
out:
[[1000 1001 1003]
[1004 1002 1003]]
但是,这些都失败了
c = np.array([[1,2,4],[5,3,4]])
np.add.at(c, [,[0,1]], 999)
print(c)
out:
File "<ipython-input-164-ebab6f97aa81>", line 2
np.add.at(c, [,[0,1]], 999)
^
SyntaxError: invalid syntax
或
c = np.array([[1,2,4],[5,3,4]])
np.add.at(c, [:,[0,1]], 999)
print(c)
out:
File "<ipython-input-165-cef6394c4870>", line 2
np.add.at(c, [:,[0,1]], 999)
^
SyntaxError: invalid syntax
您可以像这样使用 np.s_
:
>>> c = np.array([[1,2,4],[5,3,4]])
>>> np.add.at(c, np.s_[:, [0,1]], 999)
>>> c
array([[1000, 1001, 4],
[1004, 1002, 4]])
虽然@Paul 有正确的修复,但可能需要一些解释。
np.add.at(c, [[0,1],], 999)
列表中不需要尾随逗号;它在元组中用于将单个项目元组与更简单的 () 分组区分开来。
In [197]: [[0,1],]
Out[197]: [[0, 1]]
In [198]: ([0,1],)
Out[198]: ([0, 1],)
In [199]: ([0,1])
Out[199]: [0, 1]
所以尾随逗号有意义;但是第一个产生错误
In [200]: [,[0,1]]
SyntaxError: invalid syntax
列表中的:
也是一个错误。 :
在索引表达式中被接受,但会立即转换为 slice(None)
。索引使用 []
就像一个列表,但不是一回事。 .at
的第二个参数被 Python 评估为列表或元组,而不是索引表达式。
np.add.at(c, [:,[0,1]], 999)
In [213]: [:,[0,1]]
SyntaxError: invalid syntax
In [215]: np.add.at(c, [slice(None),[0,1]],999)
In [216]: c
Out[216]:
array([[1000, 1001, 4],
[1004, 1002, 4]])
@Paul 建议的 s_
有效,因为它是一个带有自定义索引方法 (__getitem__
) 的 class。这是一个可爱又方便的技巧,但我刚才描述的基本 Python 语法仍然适用。
In [217]: np.s_[:, [0,1]]
Out[217]: (slice(None, None, None), [0, 1])
与索引 .at
一样,根据需要使用尾随 slice(None)
填充,这就是为什么您的第一个示例适用于行。
In [224]: np.add.at(c, ([0,1,0], slice(None)),100)
In [225]: c
Out[225]:
array([[201, 202, 204],
[105, 103, 104]])