Broadcasting error when forming numpy array with elements as two other numpy arrays

前端 未结 1 1367
渐次进展
渐次进展 2021-01-15 21:04

I am trying to generate a numpy array with elements as two other numpy arrays, as below.

W1b1 = np.zeros((256, 161))
W         


        
相关标签:
1条回答
  • 2021-01-15 21:29

    Don't count on np.array(..., object) making the right object array. At the moment we don't have control over how many dimensions it makes. Conceivably it could make a (2,) array, or (2, 256) (with 1d contents). Sometimes it works, sometimes raises an error. There's something of a pattern, but I haven't seen an analysis of the code that shows exactly what is happening.

    For now it is safer to allocate the array, and fill it:

    In [57]: arr = np.empty(2, object)
    In [58]: arr[:] = [W1b1, W2b2]
    

    np.array([np.zeros((3,2)),np.ones((3,4))], object) also raises this error. So the error arises when the first dimensions match, but the later ones don't. Now that I think about, I've seen this error before.

    Earlier SO questions on the topic

    numpy array 1.9.2 getting ValueError: could not broadcast input array from shape (4,2) into shape (4)

    Creation of array of arrays fails, when first size of first dimension matches

    Creating array of arrays in numpy with different dimensions

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