3

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!

2 Answers 2

4

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 or empty.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.