Issue converting list to NumPy array

前端 未结 2 1113
一整个雨季
一整个雨季 2021-01-21 09:14

I have a list that consits of 2000 rows and 88200 columns:

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

deb

相关标签:
2条回答
  • 2021-01-21 09:58

    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:

    array_sizes = set(map(len, A))
    
    0 讨论(0)
  • 2021-01-21 10:04

    The problem is testlist is a list of different size arrays. 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. 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.

    0 讨论(0)
提交回复
热议问题