简体   繁体   English

有没有一种 Pythonic 方法来转置 numpy 矩阵的 1 行/列?

[英]Is there a Pythonic way to transpose 1 row/column of a numpy matrix?

Imagine I have an 8x8 matrix:想象一下我有一个 8x8 矩阵:
[ [
0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1
0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1
0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
] ]

I'd like to transpose one row/column of it, between 2 points.我想在 2 点之间转置其中的一行/列。 For example, if i wanted to transpose between 2,2 and 6,6 (where 1,1 is the upper leftmost value), the new matrix should look like this例如,如果我想在 2,2 和 6,6 之间转置(其中 1,1 是最左上角的值),新矩阵应该如下所示
[ [
0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
0 1 0 1 1 0 1 1 0 1 0 1 1 0 1 1
0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1
0 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1
0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
] ]

Is there a nice way of doing this.有没有这样做的好方法。 I've tried copying rows into columns and columns into rows but it gets ugly when I start using variable names as the two points to transpose between我尝试将行复制到列中,将列复制到行中,但是当我开始使用变量名作为两个点之间进行转置时,它变得很难看

Thanks谢谢

You could do:你可以这样做:

import numpy as np

arr = np.array([
    [0, 1, 1, 1, 1, 1, 1, 1],
    [0, 0, 1, 1, 1, 1, 1, 1],
    [0, 0, 0, 1, 1, 1, 1, 1],
    [0, 0, 0, 0, 1, 1, 1, 1],
    [0, 0, 0, 0, 0, 1, 1, 1],
    [0, 0, 0, 0, 0, 0, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 1],
    [0, 0, 0, 0, 0, 0, 0, 0]
])

arr[1:6, 1:6] = arr[1:6, 1:6].T
arr[2:5, 2:5] = arr[2:5, 2:5].T

print(arr)

Output Output

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

The idea is to transpose the sub-matrix and then transpose the interior part of the sub-matrix once again.这个想法是转置子矩阵,然后再次转置子矩阵的内部部分

You can simply swap the columns and then the rows:您可以简单地交换列,然后交换行:

x = np.arange(16).reshape((4, 4))

r0, r1 = 0, 1
c0, c1 = 2, 3

x[:, (c0, c1)] = x[:, (c1, c0)]
x[(r0, r1), :] = x[(r1, r0), :]

x
array([[ 4,  5,  7,  6],
       [ 0,  1,  3,  2],
       [ 8,  9, 11, 10],
       [12, 13, 15, 14]])

Specifically for your example (and, sorry, indices start at 0, as Nature intended):专门针对您的示例(并且,对不起,索引从 0 开始,正如 Nature 所期望的那样):

# setup
x = np.triu(np.ones((8,8), dtype=int), 1)

p0 = 1, 1
p1 = 5, 5
r0, c0, r1, c1 = p0 + p1
# operation
x[:, (c0, c1)] = x[:, (c1, c0)]
x[(r0, r1), :] = x[(r1, r0), :]

print(x)
[[0 1 1 1 1 1 1 1]
 [0 0 0 0 0 0 1 1]
 [0 1 0 1 1 0 1 1]
 [0 1 0 0 1 0 1 1]
 [0 1 0 0 0 0 1 1]
 [0 1 1 1 1 0 1 1]
 [0 0 0 0 0 0 0 1]
 [0 0 0 0 0 0 0 0]]
import numpy as np

matrix = np.array([[1,2,3,4],
                   [5,6,7,8],
                   [9,10,11,12],
                   [13,14,15,16]])

p1 = (1,1)
p2 = (3,3)
sub_matrix = matrix[p1[0]:p2[0], p1[1]:p2[1]]
matrix[p1[0]:p2[0], p1[1]:p2[1]] = np.transpose(sub_matrix)
print(matrix) # returns [[ 1  2  3  4]
              #          [ 5  6 10  8]
              #          [ 9  7 11 12]
              #          [13 14 15 16]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM