Create numpy matrix filled with NaNs

后端 未结 8 1287
灰色年华
灰色年华 2020-12-04 07:40

I have the following code:

r = numpy.zeros(shape = (width, height, 9))

It creates a width x height x 9 matrix filled with zero

8条回答
  •  情话喂你
    2020-12-04 08:13

    As said, numpy.empty() is the way to go. However, for objects, fill() might not do exactly what you think it does:

    In[36]: a = numpy.empty(5,dtype=object)
    In[37]: a.fill([])
    In[38]: a
    Out[38]: array([[], [], [], [], []], dtype=object)
    In[39]: a[0].append(4)
    In[40]: a
    Out[40]: array([[4], [4], [4], [4], [4]], dtype=object)
    

    One way around can be e.g.:

    In[41]: a = numpy.empty(5,dtype=object)
    In[42]: a[:]= [ [] for x in range(5)]
    In[43]: a[0].append(4)
    In[44]: a
    Out[44]: array([[4], [], [], [], []], dtype=object)
    

提交回复
热议问题