Reputation: 1051
I am working with numpy for the first time and am experiencing some very strange issues with floating-point arrays.
This is extremely basic and I am probably missing something very obvious here -- can anyone tell me what the problem is?
These two lines of code
arr1 = numpy.ndarray([1.0, 2.0])
print "arr1: ", arr1
produce this output:
arr1: [[ 1.49166815e-154 -1.32750723e-315]]
That's... not right. What am I doing wrong?
Thank you for any help!
Upvotes: 3
Views: 434
Reputation: 250951
You should use numpy.array
to create an array not numpy.ndarray
. numpy.ndarray
is a low-level interface, and in most cases numpy.array
should be used to create an array.
In [5]: arr1 = numpy.array([1.0, 2.0])
In [6]: arr1
Out[6]: array([ 1., 2.])
Signature of numpy.ndarray
:
ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)
So, the first argument is shape
not the array. So, numpy filled your array with some random data.
From the docstring of numpy.ndarray
:
Arrays should be constructed using
array
,zeros
orempty
.
Upvotes: 4
Reputation: 9894
The argument that you are specifying is the shape. To fill with data you need to specify the buffer argument.
np.ndarray(shape=(1,2), buffer=np.array([1,2]), dtype=float)
Upvotes: 2