[Numpy-discussion] ndarray: How to create and initialize with a value other than ones or zeros?

Robert Kern robert.kern@gmail....
Thu Jun 6 15:03:57 CDT 2013


On Thu, Jun 6, 2013 at 8:56 PM, James Adams <monocongo@gmail.com> wrote:
> I would like to create an array object and initialize the array's
> values with an arbitrary fill value, like you can do using the ones()
> and zeros() creation routines to create and initialize arrays with
> ones or zeros.  Is there an easy way to do this?  If this isn't
> possible then what is the most efficient way to initialize a numpy
> array with an arbitrary fill value?
>
> In order to provide such an array creation routine I can imagine that
> it'd be as simple as taking the code for ones() and/or zeros() and
> modifying that code so that it provides an additional fill value
> argument and then within the section which does the initialization of
> the array it could use that fill value instead of 1 or 0.  Is this a
> naive assumption?

Basically, yes. They both boil down to this:

  x = np.empty(...)
  x.fill(arbitrary_fill_value)

With just a little bit of extra help for structured dtypes (which is
relevant for zeros() but not much for you, I don't think).

--
Robert Kern

On Thu, Jun 6, 2013 at 8:56 PM, James Adams <monocongo@gmail.com> wrote:
> I would like to create an array object and initialize the array's
> values with an arbitrary fill value, like you can do using the ones()
> and zeros() creation routines to create and initialize arrays with
> ones or zeros.  Is there an easy way to do this?  If this isn't
> possible then what is the most efficient way to initialize a numpy
> array with an arbitrary fill value?
>
> In order to provide such an array creation routine I can imagine that
> it'd be as simple as taking the code for ones() and/or zeros() and
> modifying that code so that it provides an additional fill value
> argument and then within the section which does the initialization of
> the array it could use that fill value instead of 1 or 0.  Is this a
> naive assumption?
>
> Thanks in advance for your help with this issue.
>
> --James
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



-- 
Robert Kern


More information about the NumPy-Discussion mailing list