简体   繁体   English

删除空的 numpy 数组

[英]remove empty numpy array

I have a numpy array:我有一个 numpy 数组:

array([], shape=(0, 4), dtype=float64)

How can I remove this array in a multidimensional array?如何在多维数组中删除此数组? I tried我试过

import numpy as np

if array == []:
    np.delete(array)

But, the multidimensional array still has this empty array.但是,多维数组仍然有这个空数组。

EDIT: The input is编辑:输入是

new_array = [array([], shape=(0, 4), dtype=float64), 
   array([[-0.97,  0.99, -0.98, -0.93 ],
   [-0.97, -0.99,  0.59, -0.93 ],
   [-0.97,  0.99, -0.98, -0.93 ],
   [ 0.70 ,  1,  0.60,  0.65]]), array([[-0.82,  1,  0.61, -0.63],
   [ 0.92, -1,  0.77,  0.88],
   [ 0.92, -1,  0.77,  0.88],
   [ 0.65, -1,  0.73,  0.85]]), array([], shape=(0, 4), dtype=float64)]

The expected output after removing the empty arrays is:删除空数组后的预期输出是:

new array = [array([[-0.97,  0.99, -0.98, -0.93 ],
   [-0.97, -0.99,  0.59, -0.93 ],
   [-0.97,  0.99, -0.98, -0.93 ],
   [ 0.70 ,  1,  0.60,  0.65]]), 
   array([[-0.82,  1,  0.61, -0.63],
   [ 0.92, -1,  0.77,  0.88],
   [ 0.92, -1,  0.77,  0.88],
   [ 0.65, -1,  0.73,  0.85]])]

new_array , as printed, looks like a list of arrays.打印出来的new_array看起来像一个数组列表。 And even if it were an array, it would be a 1d array of dtype=object.即使它是一个数组,它也将是一个 dtype=object 的一维数组。

==[] is not the way to check for an empty array: ==[]不是检查空数组的方法:

In [10]: x=np.zeros((0,4),float)
In [11]: x
Out[11]: array([], shape=(0, 4), dtype=float64)
In [12]: x==[]
Out[12]: False
In [14]: 0 in x.shape  # check if there's a 0 in the shape
Out[14]: True

Check the syntax for np.delete .检查np.delete的语法。 It requires an array, an index and an axis, and returns another array.它需要一个数组、一个索引和一个轴,并返回另一个数组。 It does not operate in place.它没有就地运行。

If new_array is a list, a list comprehension would do a nice job of removing the [] arrays:如果new_array是一个列表,则列表new_array可以很好地删除[]数组:

In [33]: alist=[x, np.ones((2,3)), np.zeros((1,4)),x]

In [34]: alist
Out[34]: 
[array([], shape=(0, 4), dtype=float64), array([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]), array([[ 0.,  0.,  0.,  0.]]), array([], shape=(0, 4), dtype=float64)]

In [35]: [y for y in alist if 0 not in y.shape]
Out[35]: 
[array([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]), array([[ 0.,  0.,  0.,  0.]])]

It would also work if new_array was a 1d array:如果new_array是一new_array数组,它也可以工作:

new_array=np.array(alist)
newer_array = np.array([y for y in new_array if 0 not in y.shape])

To use np.delete with new_array , you have to specify which elements:要将np.deletenew_array np.delete使用,您必须指定哪些元素:

In [47]: np.delete(new_array,[0,3])
Out[47]: 
array([array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]]),
       array([[ 0.,  0.,  0.,  0.]])], dtype=object)

to find [0,3] you could use np.where :找到[0,3]你可以使用np.where

np.delete(new_array,np.where([y.size==0 for y in new_array]))

Better yet, skip the delete and where and go with a boolean mask更好的是,跳过deletewhere并使用布尔掩码

new_array[np.array([y.size>0 for y in new_array])]

I don't think there's a way of identifying these 'emtpy' arrays without a list comprehension, since you have to check the shape or size property, not the element's data.我不认为有一种方法可以在没有列表理解的情况下识别这些 'emtpy' 数组,因为您必须检查 shape 或 size 属性,而不是元素的数据。 Also there's a limit as to what kinds of math you can do across elements of an object array.此外,您可以跨对象数组的元素进行何种数学运算也有限制。 It's more like a list than a 2d array.它更像是一个列表而不是一个二维数组。

I had initially an array (3,11,11) and after a multprocessing using pool.map my array was transformed in a list like this:我最初有一个数组 (3,11,11),在使用 pool.map 进行多处理后,我的数组被转换成这样的列表:

[array([], shape=(0, 11, 11), dtype=float64),
array([[[ 0.35318114,  0.36152024,  0.35572945,  0.34495254,  0.34169853,
       0.36553977,  0.34266126,  0.3492261 ,  0.3339431 ,  0.34759375,
       0.33490712],...

if a convert this list in an array the shape was (3,), so I used:如果将此列表转换为数组,则形状为 (3,),因此我使用了:

myarray = np.vstack(mylist)

and this returned my first 3d array with the original shape (3,11,11).这返回了我的第一个具有原始形状 (3,11,11) 的 3d 数组。

Delete takes the multidimensional array as a parameter. Delete 将多维数组作为参数。 Then you need to specify the subarray to delete and the axis it's on.然后您需要指定要删除的子数组及其所在的轴。 See http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html请参阅http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html

np.delete(new_array,<obj indicating subarray to delete (perhaps an array of integers in your case)>, 0)

Also, note that the deletion is not in-place.另请注意,删除不是就地的。

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

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