简体   繁体   English

Python 中特定行的零

[英]Zeroes on specific rows in Python

I have an array Pe .我有一个数组Pe I want to exclude certain rows mentioned in the list J and ensure the other rows have all zero elements.我想排除列表J中提到的某些行,并确保其他行的元素全为零。 For example, for Pe[0] , J[0]=[0,1] which means 0,1 rows of Pe[0] are to be excluded but 2 row of Pe[0] should contain all zero elements.例如,对于Pe[0]J[0]=[0,1]这意味着要排除Pe[0]0,1行,但Pe[0] [0] 的2行应包含所有零元素。 Similarly, for Pe[1] .同样,对于Pe[1] But I am getting an error.但是我收到一个错误。 I also present the expected output.我还提出了预期的 output。

import numpy as np

Pe = [np.array([[402.93473651,   0.        , 230.97804127, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        , 423.81345923,   0.        , 407.01354328,
        419.14952534,   0.        , 316.58460442,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [402.93473651,   0.        , 230.97804127, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ]]),
np.array([[402.93473651,   0.        , 230.97804127, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        , 423.81345923,   0.        , 407.01354328,
        419.14952534,   0.        , 316.58460442,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [402.93473651,   0.        , 230.97804127, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ]])]  #Entry pressure

J = [[0,1],[2]]



for i in range(0,len(Pe)):
    out = np.zeros_like(Pe[i])
    for j in range(0,len(J)):
        out[i][J[j]] = Pe[i][J[j]]
    print([out])

The error is错误是

in <module>
    out[i][J[j]] = Pe[i][J[j]]

ValueError: shape mismatch: value array of shape (2,12)  could not be broadcast to indexing result of shape (2,)

The expected output is预期的 output 是

[np.array([[402.93473651,   0.        , 230.97804127, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        , 423.81345923,   0.        , 407.01354328,
        419.14952534,   0.        , 316.58460442,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [0. ,            0.        ,   0. ,          0. ,
          0.        , 0. ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ]]),
np.array([[0. ,   0.        , 0. , 0. ,
          0.        , 0. ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        , 0. ,   0.        , 0. ,
        0. ,   0.        , 0.,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [402.93473651,   0.        , 230.97804127, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ]])]
out = []
for arr, ind in zip(Pe, J):
    x = np.zeros_like(arr)
    x[ind] = arr[ind]
    out.append(x)

Using lists and loops in Numpy is often an anti-pattern, and that is the case here.在 Numpy 中使用列表和循环通常是一种反模式,这里就是这种情况。 You should be using vectorised operations throughout.您应该始终使用矢量化操作。 J is jagged so you need to reinterpret it as a boolean indexer. J是锯齿状的,因此您需要将其重新解释为 boolean 索引器。 Also, Pe should not start with repeated dimensions;此外, Pe不应以重复维度开头; it should start as a single two-dimensional array without a list.它应该从一个没有列表的二维数组开始。

import numpy as np

Pe = np.array([[402.93473651,   0.        , 230.97804127, 407.01354328,
                  0.        , 414.17017965,   0.        ,   0.        ,
                  0.        ,   0.        ,   0.        ,   0.        ],
               [  0.        , 423.81345923,   0.        , 407.01354328,
                419.14952534,   0.        , 316.58460442,   0.        ,
                  0.        ,   0.        ,   0.        ,   0.        ],
               [402.93473651,   0.        , 230.97804127, 407.01354328,
                  0.        , 414.17017965,   0.        ,   0.        ,
                  0.        ,   0.        ,   0.        ,   0.        ]])

J = np.ones((2, Pe.shape[0]), dtype=bool)
J[0, 0:2] = 0
J[1, 2] = 0

Pe_indexed = np.tile(Pe, (J.shape[0], 1, 1))
Pe_indexed[J, :] = 0

Pe_indexed will now be a proper three-dimensional matrix, no lists. Pe_indexed现在将是一个适当的三维矩阵,没有列表。

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

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