Python 交换numpy数组中的列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4857927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swapping columns in a numpy array?
提问by audacious ainsley
from numpy import *
def swap_columns(my_array, col1, col2):
temp = my_array[:,col1]
my_array[:,col1] = my_array[:,col2]
my_array[:,col2] = temp
Then
然后
swap_columns(data, 0, 1)
Doesn't work. However, calling the code directly
不起作用。但是直接调用代码
temp = my_array[:,0]
my_array[:,0] = my_array[:,1]
my_array[:,1] = temp
Does. Why is this happening and how can I fix it? The Error says "IndexError: 0-d arrays can only use a single () or a list of newaxes (and a single ...) as an index", which implies the arguments aren't ints? I already tried converting the cols to int but that didn't solve it.
做。为什么会发生这种情况,我该如何解决?错误说“IndexError: 0-d arrays can only use a single () or a list of newaxes (and a single ...) as an index”,这意味着参数不是整数?我已经尝试将 cols 转换为 int 但这并没有解决它。
回答by Sven Marnach
There are two issues here. The first is that the datayou pass to your function apparently isn't a two-dimensional NumPy array -- at least this is what the error message says.
这里有两个问题。第一个是data您传递给函数的显然不是二维 NumPy 数组——至少这是错误消息所说的。
The second issue is that the code does not do what you expect:
第二个问题是代码没有按照您的预期执行:
my_array = numpy.arange(9).reshape(3, 3)
# array([[0, 1, 2],
# [3, 4, 5],
# [6, 7, 8]])
temp = my_array[:, 0]
my_array[:, 0] = my_array[:, 1]
my_array[:, 1] = temp
# array([[1, 1, 2],
# [4, 4, 5],
# [7, 7, 8]])
The problem is that Numpy basic slicingdoes not create copies of the actual data, but rather a view to the same data. To make this work, you either have to copy explicitly
问题在于 Numpy基本切片不会创建实际数据的副本,而是创建相同数据的视图。为了使这项工作,您要么必须明确复制
temp = numpy.copy(my_array[:, 0])
my_array[:, 0] = my_array[:, 1]
my_array[:, 1] = temp
or use advanced slicing
或使用高级切片
my_array[:,[0, 1]] = my_array[:,[1, 0]]
回答by Renaud
Building up on @Sven's answer:
以@Sven 的回答为基础:
import numpy as np
my_array = np.arange(9).reshape(3, 3)
print my_array
[[0 1 2]
[3 4 5]
[6 7 8]]
def swap_cols(arr, frm, to):
arr[:,[frm, to]] = arr[:,[to, frm]]
swap_cols(my_array, 0, 1)
print my_array
[[1 0 2]
[4 3 5]
[7 6 8]]
def swap_rows(arr, frm, to):
arr[[frm, to],:] = arr[[to, frm],:]
my_array = np.arange(9).reshape(3, 3)
swap_rows(my_array, 0, 2)
print my_array
[[6 7 8]
[3 4 5]
[0 1 2]]
回答by blaz
I find the following the fastest:
我发现以下最快:
my_array[:, 0], my_array[:, 1] = my_array[:, 1], my_array[:, 0].copy()
Time analysis of:
时间分析:
import numpy as np
my_array = np.arange(900).reshape(30, 30)
is as follows:
如下:
%timeit my_array[:, 0], my_array[:, 1] = my_array[:, 1], my_array[:, 0].copy()
The slowest run took 15.05 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1.72 μs per loop
The advanced slicing times are:
高级切片时间为:
%timeit my_array[:,[0, 1]] = my_array[:,[1, 0]]
The slowest run took 7.38 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 6.9 μs per loop
回答by Jensun
Suppose you have a numpy array like this:
假设你有一个像这样的 numpy 数组:
array([[ 0., -1., 0., 0.],
[ 0., 1., 1., 1.],
[ 0., 0., -1., 0.],
[ 0., 0., 0., -1.]])
This is a very elegant way to swap the columns:
这是一种非常优雅的列交换方式:
col1 = 0
col2 = 1
my_array.T[[col1, col2]] = my_array.T[[col2, col1]]
Result:
结果:
array([[-1., 0., 0., 0.],
[ 1., 0., 1., 1.],
[ 0., 0., -1., 0.],
[ 0., 0., 0., -1.]])

