Create numpy matrix filled with NaNs

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

    Yet another possibility not yet mentioned here is to use NumPy tile:

    a = numpy.tile(numpy.nan, (3, 3))
    

    Also gives

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

    I don't know about speed comparison.

    0 讨论(0)
  • 2020-12-04 08:32

    Another alternative is numpy.broadcast_to(val,n) which returns in constant time regardless of the size and is also the most memory efficient (it returns a view of the repeated element). The caveat is that the returned value is read-only.

    Below is a comparison of the performances of all the other methods that have been proposed using the same benchmark as in Nico Schlömer's answer.

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