numpy中更新特定列的最有效方法

时间:2023-01-01 22:23:39

Whats the most efficient way to update a specific column in numpy? For simplicity, say i have:

什么是更新numpy中特定列的最有效方法?为简单起见,我说:

[[0 2]
 [1 2]
 [2 2]]

Then I would like to add 2 on every element on the second column.

然后我想在第二列的每个元素上添加2。

[[0 4]
 [1 4]
 [2 4]]

This can be simply done with a loop, but I was wondering if theres a numpy function I could use that would be more efficient. Assuming the array is quite large.

这可以简单地完成一个循环,但我想知道是否有一个我可以使用的numpy函数会更有效。假设阵列非常大。

1 个解决方案

#1


1  

You can reference a column using:

您可以使用以下方式引用列:

a = np.array([[0 2]
              [1 2]
              [2 2]]

a[:,1] += 2

#1


1  

You can reference a column using:

您可以使用以下方式引用列:

a = np.array([[0 2]
              [1 2]
              [2 2]]

a[:,1] += 2