Create numpy matrix filled with NaNs

后端 未结 8 1291
灰色年华
灰色年华 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:29

    Are you familiar with numpy.nan?

    You can create your own method such as:

    def nans(shape, dtype=float):
        a = numpy.empty(shape, dtype)
        a.fill(numpy.nan)
        return a
    

    Then

    nans([3,4])
    

    would output

    array([[ NaN,  NaN,  NaN,  NaN],
           [ NaN,  NaN,  NaN,  NaN],
           [ NaN,  NaN,  NaN,  NaN]])
    

    I found this code in a mailing list thread.

提交回复
热议问题