Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FutureWarning on using np.full to create array

I used the following to create a contact array in python using numpy.

import numpy as np
a = np.full((2,2), 7) 
print(a) 

It did print the expected array.

[[ 7.  7.]
 [ 7.  7.]]

I got the following warning also after the value was printed :

FutureWarning: in the future, full((2, 2), 7) will return an array of dtype('int64').

Can someone please explain what this means? Also is this something important or can be ignored (which we generally do with warnings :P).

like image 654
cassandra Avatar asked Feb 28 '26 14:02

cassandra


1 Answers

I don't think it's wise to ignore this FutureWarning because what until now created a float array will change to an int array in the future. Note that you specified an integer 7 as fill-value but the result was a float-array. I assume the numpy-developers considered that this behaviour is inconsistent and want to change the behaviour in the future.

If you want an int array you should explicitly specify the dtype=int:

>>> np.full((2, 2), 7, dtype=int)
array([[7, 7],
       [7, 7]])

If you want a float array, just change 7 to a float: 7.:

>>> np.full((2, 2), 7.)
array([[ 7.,  7.],
       [ 7.,  7.]])

It's also possible to specify the dtype explicitly dtype=float:

>>> np.full((2,2), 7, dtype=float)
array([[ 7.,  7.],
       [ 7.,  7.]])

In all three cases the FutureWarning goes away without the need to explicitly ignore the FutureWarning.

I wouldn't recommend it but if you don't care if it's an integer or float array and don't like that Warning you can explicitly suppress it:

import warnings
import numpy as np

with warnings.catch_warnings():
    warnings.simplefilter('ignore', FutureWarning)
    arr = np.full((2,2), 7)
like image 168
MSeifert Avatar answered Mar 03 '26 03:03

MSeifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!