如何按一行对矩阵排序-Python
有这样一个矩阵:如何按一行对矩阵排序-Python,python,python-3.x,numpy,Python,Python 3.x,Numpy,有这样一个矩阵: [[0. 1. 0. 0. 0.] [1. 0. 0. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.] [1. 0. 3. 4. 4.]] [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.] [0. 1. 3. 4. 4.]] num_seq = 10 seq_len = 5 seq_width = 5 con = np.random.randint(0
[[0. 1. 0. 0. 0.]
[1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[1. 0. 3. 4. 4.]]
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
num_seq = 10
seq_len = 5
seq_width = 5
con = np.random.randint(0, seq_width,size=seq_len)
seq = np.zeros((seq_len, seq_width))
seq[np.arange(seq_len), con] = 1
seq[seq_len-1, np.arange(seq_width)] = con
out = np.sort(seq, axis=1)
[[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
如何按最后一行排序,使列的内容保持不变,如下所示:
[[0. 1. 0. 0. 0.]
[1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[1. 0. 3. 4. 4.]]
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
num_seq = 10
seq_len = 5
seq_width = 5
con = np.random.randint(0, seq_width,size=seq_len)
seq = np.zeros((seq_len, seq_width))
seq[np.arange(seq_len), con] = 1
seq[seq_len-1, np.arange(seq_width)] = con
out = np.sort(seq, axis=1)
[[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
现在我是这样做的:
[[0. 1. 0. 0. 0.]
[1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[1. 0. 3. 4. 4.]]
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
num_seq = 10
seq_len = 5
seq_width = 5
con = np.random.randint(0, seq_width,size=seq_len)
seq = np.zeros((seq_len, seq_width))
seq[np.arange(seq_len), con] = 1
seq[seq_len-1, np.arange(seq_width)] = con
out = np.sort(seq, axis=1)
[[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
并获得如下输出:
[[0. 1. 0. 0. 0.]
[1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[1. 0. 3. 4. 4.]]
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
num_seq = 10
seq_len = 5
seq_width = 5
con = np.random.randint(0, seq_width,size=seq_len)
seq = np.zeros((seq_len, seq_width))
seq[np.arange(seq_len), con] = 1
seq[seq_len-1, np.arange(seq_width)] = con
out = np.sort(seq, axis=1)
[[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1.]
[0. 1. 3. 4. 4.]]
你可以做一些小的切片。使用示例阵列:
arr = np.array([[0, 1, 0, 0, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
[1, 0, 3, 4, 4]])
arr[:, np.argsort(arr[-1, :])]
# array([[1, 0, 0, 0, 0],
# [0, 1, 0, 0, 0],
# [0, 0, 0, 1, 0],
# [0, 0, 0, 0, 1],
# [0, 1, 3, 4, 4]])
基本上,np.argsort(arr[-1,:])
按最后一行内容的升序返回arr
最后一行的索引。这将返回您给出的示例的数组([1,0,2,3,4])
然后,我们只需使用arr[:,np.argsort(arr[-1,:])]
以该顺序获取列中的所有行,然后尝试:
arr[:,arr[-1,:].argsort()]
这满足了您的要求,尽管在我看来,numpy方法更干净、更好。这取决于您的矩阵是真正的矩阵,还是数组或列表。如果您出于任何原因不能使用numpy,那么这个解决方案应该独立工作,即使它有点黑
your_matrix = [[0, 1., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.,],
[0., 0., 0., 0., 1.],
[1., 0., 3., 4., 4.]]
your_last_row = your_matrix[-1]
#add indices
reference_row = [(idx, value) for (idx, value) in enumerate(your_last_row)]
ref_row_sorted = sorted(reference_row, key=lambda x: x[1])
#extract indices
indices = [x[0] for x in ref_row_sorted]
new_matrix = []
for row in your_matrix:
new_row = [row[x] for x in indices]
new_matrix.append(new_row)
使用argsort选择con作为索引:
import numpy as np
num_seq = 10
seq_len = 5
seq_width = 5
con = np.random.randint(0, seq_width,size=seq_len)
seq = np.zeros((seq_len, seq_width))
seq[np.arange(seq_len), con] = 1
seq[seq_len-1, np.arange(seq_width)] = con
i = np.argsort(con)
seq = seq[:,i]
print(con)
print(seq)
我将获取最后一行的副本,并制作一对索引为元组的值。然后按值排序,提取索引,并将其作为基数对其余列进行排序