简体   繁体   English

将转换列表发送到NumPy数组

[英]Issue converting list to NumPy array

I have a list that consits of 2000 rows and 88200 columns: 我有一个包含2000行和88200列的列表:

testlist = list(split_audio_to_parts(audio, self.sample_rate, self.audio_index))

debugging output of testlist gives testlist调试输出给出

[array([-0.00683594, -0.00689697, -0.00708008, ...,  0.        ,
    0.        ,  0.        ]), array([-0.01287842, -0.01269531, -0.01257324, ...,  0.        ,
    0.        ,  0.        ]), array([0.02288818, 0.01940918, 0.01409912, ..., 0.        , 0.        ,
   0.        ]), array([0.00772095, 0.00671387, 0.00695801, ..., 0.        , 0.        ,
   0.        ]),

and so on. 等等。 split_audio_to_parts is a function: split_audio_to_parts是一个函数:

def split_audio_to_parts(x, sample_rate, audio_index):
for i, row in audio_index.iterrows():
    x_part = x[int(row['start_samples']):int(row['end_samples'])]
    yield x_part

When I try to convert it to numpy array using samples = np.array(testlist) or samples = np.asarray(testlist) , it gives me array of shape (2000,), although debugging shows that testlist consits of 2000 entries with 88200 positions. 当我尝试使用samples = np.array(testlist)samples = np.asarray(testlist)将其转换为numpy数组时,它给出了我的形状数组(2000,),尽管调试显示testlist列表包含2000个条目和88200位置。 Why so? 为什么这样? I'm using 64bit numpy and 64bit Python 3.6.5. 我使用64位numpy和64位Python 3.6.5。

The problem is testlist is a list of different size arrays. 问题是testlist是一个不同大小的数组列表。 For example checkout this code: 例如,签出此代码:

>>>import numpy as np
>>>import random 
>>>random.seed(3240324324)
>>> y=[np.array(list(range(random.randint(1,3)))) for _ in range(3)]
>>> y
[array([0, 1, 2]), array([0, 1, 2]), array([0])]
>>> np.array(y)
array([array([0, 1, 2]), array([0, 1, 2]), array([0])], dtype=object)
>>> np.array(y).shape
(3,)

and the array would be of object type instead of float. 并且数组将是object类型而不是float。 the only way for this to work is having same sized arrays. 这种方法的唯一方法是使用相同大小的数组。

If you really need to stuff these rows somehow into an array you can pad with zeros, for example: 如果你真的需要以某种方式将这些行填充到一个数组中,你可以用零填充,例如:

>>> size = y[max(enumerate(y),key=lambda k:k[1].shape)[0]].shape[0]
>>> z=[np.append(x,np.zeros(size-x.shape[0])) for x in y]
>>> z
[array([ 0.,  1.,  2.]), array([0, 1, 2]), array([0, 0, 0])]
>>>np.array(z).shape
(3, 3)

but you would have to decide how you do this padding. 但你必须决定如何进行填充。

You have a list of arrays. 你有一个数组列表。 If each array in your list does not have the same length, your conversion will not be successful. 如果列表中的每个数组的长度不同,则转换将无法成功。

Here is a minimal example. 这是一个最小的例子。

A = [np.array([1, 2]), np.array([4, 5, 6])]

A_2 = np.array(A)
# array([array([1, 2]), array([4, 5, 6])], dtype=object)

A_2.shape
# (2,)

If the lengths of your arrays are aligned, you will find no problem: 如果阵列的长度对齐,您将发现没有问题:

B = [np.array([1, 2, 3]), np.array([4, 5, 6])]

B_2 = np.array(B)
# array([[1, 2, 3],
#        [4, 5, 6]])

B_2.shape
# (2, 3)

To check the sizes of your arrays, you can use set : 要检查数组的大小,可以使用set

array_sizes = set(map(len, A))

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

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