Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用条件从多维numpy数组中删除子数组?_Python_Arrays_Numpy_For Loop_Conditional Statements - Fatal编程技术网

Python 如何使用条件从多维numpy数组中删除子数组?

Python 如何使用条件从多维numpy数组中删除子数组?,python,arrays,numpy,for-loop,conditional-statements,Python,Arrays,Numpy,For Loop,Conditional Statements,我试图使用条件从多维numpy数组中删除子数组。在这个例子中,我想删除所有包含值999的子数组。以下是我失败的尝试之一: a = np.array([[[1,2,3], [1,2,3]], [[999,5,6], [4,5,6]], [[999,8,9], [7,999,9]] ]) for i in range(0,len(a)): if 999 in a[i]: np.delete(

我试图使用条件从多维numpy数组中删除子数组。在这个例子中,我想删除所有包含值999的子数组。以下是我失败的尝试之一:

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

for i in range(0,len(a)):
    if 999 in a[i]:
        np.delete(a, i, 0)
我想要的结果是:

array([[1,2,3], [1,2,3]])
这只是一个小的例子,它应该能帮助我理解一个更大的问题,就像这样:

# win_list_hyper.shape -> (1449168, 233)
# win_list_multi.shape -> (1449168, 12, 5, 5)

win_list_hyper = np.where(win_list_hyper <= 0, -3.40282e+38, win_list_hyper)
win_list_multi = np.where(win_list_multi <= 0, -3.40282e+38, win_list_multi)


# fail!:
for i in range(0,len(win_list_multi)):
    
    if -3.40282e+38 in win_list_multi[i] or -3.40282e+38 in win_list_hyper[i]:
        
        np.delete(win_list_multi, i, 0)
        np.delete(win_list_hyper, i, 0)
#win_list_hyper.shape->(1449168233)
#win_list_multi.shape->(1449168,12,5,5)

win\u list\u hyper=np。其中(win\u list\u hyper您的第一次尝试失败,因为
np.delete
不在原地操作(即,它不修改数组,返回一个新数组)。此外,在迭代数组时从数组中删除元素通常不是一个好主意(除非您知道自己在做什么)

您可以只使用
np。其中
如下所示:

inds = np.where(a == 999)  # get indices where value equals 999
np.delete(a, inds[0], axis=0)   # delete along first dimension
结果:

array([[[1, 2, 3],
        [1, 2, 3]]])

您的第一次尝试失败,因为
np.delete
不在原地运行(即,它不修改数组,而是返回一个新数组)。此外,在迭代数组时从数组中删除元素通常不是一个好主意(除非您知道自己在做什么)

您可以只使用
np。其中
如下所示:

inds = np.where(a == 999)  # get indices where value equals 999
np.delete(a, inds[0], axis=0)   # delete along first dimension
结果:

array([[[1, 2, 3],
        [1, 2, 3]]])

Jussi Nurminen解决方案在我的示例中运行良好,但我必须意识到我的示例并不好。我无法将给定的解决方案轻松地转换为我的数据。Jussi Nurminen解决方案对我帮助很大,因为它给了我屏蔽数组的想法。我希望我的解决方案不会弄乱我的数据(例如,洗牌数据).对于那些感兴趣的人

…这是针对我的(坏)示例的解决方案:

…这就是我的数据的translatet的外观:


# win_list_multi.shape -> (1449168, 12, 5, 5)
# win_list_hyper.shape -> (1449168, 233


win_list_multi = np.where(win_list_multi <= 0, -1, win_list_multi)

win_list_hyper = np.where(win_list_hyper <= 0, -1, win_list_hyper)


win_list_multi_mask = []

for i in range(0,len(win_list_multi)):
    if -1 in win_list_multi[i]:
        x = 0
    else: x = 1
    
    win_list_multi_mask.append(x)

win_list_multi_mask = np.asarray(win_list_multi_mask)



win_list_hyper_mask = []

for i in range(0,len(win_list_hyper)):
    if -1 in win_list_hyper[i]:
        x = 0
    else: x = 1
    
    win_list_hyper_mask.append(x)

win_list_hyper_mask = np.asarray(win_list_hyper_mask)



inds = np.where((win_list_multi_mask == 0) | (win_list_hyper_mask == 0))


win_list_multi_nd = np.delete(win_list_multi, inds, axis=0) 
win_list_hyper_nd = np.delete(win_list_hyper, inds, axis=0) 

# win_list_multi_nd.shape -> (9679, 12, 5, 5)
# win_list_hyper_nd.shape -> (9679, 233)


#win_list_multi.shape->(1449168,12,5,5)
#win_list_hyper.shape->(1449168233)
win_list_multi=np.where(win_list_multi(9679233))

Jussi Nurminen解决方案在我的示例中效果很好,但我必须意识到我的示例并不好。我不能那么容易地将给定的解决方案转换为数据。Jussi Nurminen解决方案对我帮助很大,因为它给了我屏蔽数组的想法。我希望我的解决方案不会弄乱我的数据(例如,洗牌数据).对于那些感兴趣的人

…这是针对我的(坏)示例的解决方案:

…这就是我的数据的translatet的外观:


# win_list_multi.shape -> (1449168, 12, 5, 5)
# win_list_hyper.shape -> (1449168, 233


win_list_multi = np.where(win_list_multi <= 0, -1, win_list_multi)

win_list_hyper = np.where(win_list_hyper <= 0, -1, win_list_hyper)


win_list_multi_mask = []

for i in range(0,len(win_list_multi)):
    if -1 in win_list_multi[i]:
        x = 0
    else: x = 1
    
    win_list_multi_mask.append(x)

win_list_multi_mask = np.asarray(win_list_multi_mask)



win_list_hyper_mask = []

for i in range(0,len(win_list_hyper)):
    if -1 in win_list_hyper[i]:
        x = 0
    else: x = 1
    
    win_list_hyper_mask.append(x)

win_list_hyper_mask = np.asarray(win_list_hyper_mask)



inds = np.where((win_list_multi_mask == 0) | (win_list_hyper_mask == 0))


win_list_multi_nd = np.delete(win_list_multi, inds, axis=0) 
win_list_hyper_nd = np.delete(win_list_hyper, inds, axis=0) 

# win_list_multi_nd.shape -> (9679, 12, 5, 5)
# win_list_hyper_nd.shape -> (9679, 233)


#win_list_multi.shape->(1449168,12,5,5)
#win_list_hyper.shape->(1449168233)
win_list_multi=np.where(win_list_multi(9679233))

感谢您的帮助。我必须意识到我的示例并不好。我不能那么容易地将您的解决方案转换为我的数据。尽管您帮了我很多。您的解决方案让我想到了屏蔽阵列。我希望我的解决方案不会弄乱我的数据(例如,洗牌)。感谢您的帮助。我必须意识到我的示例并不好。我无法那么容易地将您的解决方案转换为我的数据。尽管您帮了我很多。您的解决方案让我想到了屏蔽阵列。我希望我的解决方案不会弄乱我的数据(例如,洗牌)。