Python 使用另一个矩阵子集一个NumPy矩阵

Python 使用另一个矩阵子集一个NumPy矩阵,python,arrays,numpy,matrix,Python,Arrays,Numpy,Matrix,我有numpy矩阵X=np.matrix([[1,2],[3,4],[5,6]])和y=np.matrix([1,1,0]),我想基于y矩阵创建两个新矩阵X_pos和X_neg。所以希望我的输出如下:X_pos==matrix([[1,2],[3,4]])和X_neg==matrix([[5,6]])。我该怎么做呢?如果您愿意用y创建一个布尔掩码,这就变得简单了 mask = np.array(y).astype(bool).reshape(-1,) X_pos = X[mask, :] X_n

我有numpy矩阵
X=np.matrix([[1,2],[3,4],[5,6]])
y=np.matrix([1,1,0])
,我想基于y矩阵创建两个新矩阵X_pos和X_neg。所以希望我的输出如下:
X_pos==matrix([[1,2],[3,4]])
X_neg==matrix([[5,6]])
。我该怎么做呢?

如果您愿意用
y
创建一个布尔掩码,这就变得简单了

mask = np.array(y).astype(bool).reshape(-1,)
X_pos = X[mask, :]
X_neg = X[~mask, :]

print(X_pos) 
matrix([[1, 2],
        [3, 4]])

print(X_neg)
matrix([[5, 6]])

如果您愿意用
y
创建布尔掩码,这就变得简单了

mask = np.array(y).astype(bool).reshape(-1,)
X_pos = X[mask, :]
X_neg = X[~mask, :]

print(X_pos) 
matrix([[1, 2],
        [3, 4]])

print(X_neg)
matrix([[5, 6]])
与常规:

x = np.matrix([[1, 2], [3, 4], [5, 6]])
y = np.array([1, 1, 0])

m = np.ma.masked_where(y > 0, y)   # mask for the values greater than 0
x_pos = x[m.mask]                  # applying masking
x_neg = x[~m.mask]                 # negation of the initial mask

print(x_pos)
print(x_neg)
与常规:

x = np.matrix([[1, 2], [3, 4], [5, 6]])
y = np.array([1, 1, 0])

m = np.ma.masked_where(y > 0, y)   # mask for the values greater than 0
x_pos = x[m.mask]                  # applying masking
x_neg = x[~m.mask]                 # negation of the initial mask

print(x_pos)
print(x_neg)

它们是矩阵的具体原因是什么?我很容易在前面和后面的代码中使用矩阵。它们是矩阵的具体原因是什么?我很容易在前面和后面的代码中使用矩阵。