[英]Python, Numpy: How to change two vectors at once
I'm trying to change two vectors of a NumPy matrix at once, but I'm losing one vector components:我正在尝试一次更改 NumPy 矩阵的两个向量,但我丢失了一个向量分量:
import numpy as np
data = np.array([[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]])
last = data[:, -1]
print(last)
data[:, 1:] = data[:, :-1]
data[:, 0] = last
print(data)
Gives this result:给出这个结果:
[4 4 4 4]
[[3 1 2 3]
[3 1 2 3]
[3 1 2 3]
[3 1 2 3]]
But I want to maintain the 4s in the first column.但我想保持第一列中的4s。 Is there any form to accomplish that?有什么形式可以实现吗?
Use:利用:
>>> np.roll(data, 1, 1)
array([[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.