简体   繁体   English

如何使用现有二维数组的条件创建新的 numpy 二维数组

[英]How to create a new numpy 2d array with conditions from the existing 2d array

I have 2d array with 3 columns and N rows.我有 3 列和 N 行的二维数组。 In the third column there are only 0 or 1. I need to create 2 numpy arrays.在第三列中只有 0 或 1。我需要创建 2 个 numpy arrays。 They both contains first 2 columns of the given matrix, but first array has only rows corresponding to 0 from the third column, and second array has only rows to 1.它们都包含给定矩阵的前 2 列,但第一个数组只有对应于第三列中的 0 的行,而第二个数组只有对应于 1 的行。

I've tried but it failed with dimension problems.我试过了,但因尺寸问题而失败。 I haven't used this kind of format before.我以前没有使用过这种格式。 onlyNormal_Xtest = np.vstack((onlyNormal_Xtest, Xy[Xy[N_train:, 2] == 0]))

Is it possible to do it faster than following?有可能比跟随更快吗?

onlyNormal_Xtest = np.array([])
Xy_test = Xy[N_train:, :]

    for i in range(np.size(Xy_test, 0)):
        if (Xy_test[i, 2] == 0):
            onlyNormal_Xtest = np.append(onlyNormal_Xtest, Xy_test[i, :2])

Actually it still doesn't work due to dimension problems.实际上由于尺寸问题它仍然不起作用。

Not sure if I understood your question but here is the code i think you were trying to do不确定我是否理解你的问题,但这是我认为你试图做的代码

a = np.array([[1,2,0],
             [3,4,1],
             [5,6,0],
             [7,8,1]])

Gives

a[a[:,2]==1][:,:2]
array([[3, 4],
       [7, 8]])
a[a[:,2]==0][:,:2]
array([[1, 2],
       [5, 6]])

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

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