大numpy数组内的列表 [英] Lists inside big numpy arrays
问题描述
我想使用一个大型Numpy数组的某些部分.但是,Python陷入困境是因为数组的这一较小部分实际上不是Numpy数组,而是一个列表.因此,我调查了一下,发现Numpy实际上在大型Numpy数组中存储了多个列表.仅当Numpy数组具有特定大小时才会发生这种情况.所以,我的问题是:
I want to use some parts of a big Numpy array. However, Python nagged because this smaller part of the array wasn't actually a Numpy array, but a list. So I looked into it and found out that Numpy actually stored multiple list within the big Numpy array. This only happens when a Numpy array has a certain size. So, my questions are:
- 为什么numpy会这样做?
- 有没有办法防止这种情况,因此只有一个很大的Numpy数组?
- Numpy开始执行此操作后,阈值大小是多少?
以下是两个不同的Numpy数组,以澄清我的问题.第一个是标准的Numpy数组,第二个采用上面解释的内部具有多个列表的行为.
Here are two different Numpy arrays to clarify my question. The first one is a standard Numpy array and the second one adopts the behavior with multiple lists inside explained above.
bigger_array = np.array([
[[[0.5, 0.05, 0.05, 0.05, 0.0, 0.0, 0.0, 0.0], [0.05, 0.5, 0.05, 0.05, 0.0, 0.0, 0.0, 0.0],
[0.05, 0.05, 0.5, 0.05, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.05, 0.5, 0.05, 0.05, 0.0, 0.0],
[0.0, 0.0, 0.05, 0.05, 0.5, 0.05, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.05, 0.5, 0.05, 0.05],
[0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.5, 0.05], [0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.05, 0.5]],
[[0.5, 0.05, 0.05, 0.05, 0.0, 0.0, 0.0, 0.0], [0.05, 0.5, 0.05, 0.05, 0.0, 0.0, 0.0, 0.0],
[0.05, 0.05, 0.5, 0.05, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.05, 0.5, 0.05, 0.05, 0.0, 0.0],
[0.0, 0.0, 0.05, 0.05, 0.5, 0.05, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.05, 0.5, 0.05, 0.05],
[0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.5, 0.05], [0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.05, 0.5]]],
[[[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125]],
[[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125]]],
[[[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125]],
[[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125],
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125]]]
])
smaller-array = np.array([
[[[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05], [0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05],
[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05], [0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05]],
[[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05], [0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05],
[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05], [0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05]]],
[[[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05],
[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05]],
[[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05],
[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05]]],
[[[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05],
[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05]],
[[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05],
[0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05], [0.5, 0.5, 0.5, 0.5, 0.05, 0.05, 0.05, 0.05]]]
])
print(type(bigger_array[0][0]))
print(type(smaller_array[0][0]))
推荐答案
构成 bigger_array
的列表的子元素大小不同.如果解决了这个问题,那么numpy将返回一个数组.
The subelements of the list forming bigger_array
are of different sizes. If you fix that, then numpy will return an array.
print([len(x) for y in bigger_array for x in y]) # [8, 8, 6, 6, 6, 6]
如果需要将不同大小的序列存储在numpy数组中,则可以创建一个最大大小的空数组,然后将其填充:
If you need to store sequences of different sizes in a numpy array, you can create an empty array of maximal size and then fill it:
big_array = np.empty(2, 4)
big_array[0] = [1, 2, 3, 4]
big_array[1, :-1] = [1, 2, 3]
您只需要记住尚未设置的元素即可.
You will just need to keep in mind which elements have not been set.
执行此操作的一种方法是保留一个布尔掩码数组,该数组会告诉您是否已设置元素.
One way of doing this is keeping a boolean mask array that tells you whether an element has been set or not.
mask = np.ones_like(big_array, dtype=bool)
mask[-1, -1] = False
mask
返回
array([[ True, True, True, True],
[ True, True, True, False]])
这篇关于大numpy数组内的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!