0

I tried to create turn a python list into a numpy array, but got very unintuitive output. Certainly I did something wrong, but out of curiosity I would like to know why I got such an output. Here is my code:

import numpy as np

# Exercise 2
a = [1, 5, 3, 6, 2]
b = np.ndarray(a)
print(b, b.dtype)

the output was

[[[[[0.00000000e+000 6.93284651e-310]
    [6.93284792e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]]

   [[6.93284744e-310 2.20882835e-314]
    [6.93284743e-310 6.93284743e-310]
    [6.93284743e-310 6.93284650e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]]

   ... (12 more blocks similar to ones above)

   [[6.93284871e-310 6.93284871e-310]
    [6.93284745e-310 6.93284871e-310]
    [6.93284651e-310 6.93284871e-310]
    [6.93284745e-310 6.93284871e-310]
    [6.93284871e-310 6.93284745e-310]
    [6.93284727e-310 6.93284871e-310]]]]] float64
5
  • 1
    Does this answer your question? What is the difference between ndarray and array in numpy? Commented Jun 30, 2020 at 16:16
  • Trenton, not exactly, but thank you for the reference. I spotted my mistake, just want to understand why I got this particular output. From the answers I got why the output is of this shape, but now I am also curious about the values. Commented Jun 30, 2020 at 16:24
  • 1
    Probably floating point precision. They are all equivalently 0 Commented Jun 30, 2020 at 16:27
  • With ndarray you did not specify the buffer, just the shape. So those values are 'uninitialized', not even to 0. Use np.zeros(shape) if you want an array of 0s with a given shape. Commented Jun 30, 2020 at 16:28
  • Why are floating point numbers inaccurate? Commented Jun 30, 2020 at 16:30

4 Answers 4

2

You created a five dimensional array.

a = [1, 5, 3, 6, 2]
b = np.ndarray(a)
print(b.shape)

gives

(1, 5, 3, 6, 2)

The first argument of the np.ndarray is the shape of the array. Your probably wanted

b = np.array(a)
print(b)

which gives

[1 5 3 6 2]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this is the clearest answer. Can you also tell me why the most values are 6*10^(-310), while some of them are different?
I think it is an empty array, so it is whatever was in memory at the spot that the array was created. Try it again and you'll likely get different numbers.
1

You've created a n-dimensional array whereby n = 5 which is the length of the array passed (which form the dimensions as explained here).

It's likely you're looking for:

np.array(a)

Those numbers are float 0.

Comments

1

To create an array from a list, use: b = np.array(a). np.ndarray is another numpy class, and the first argument of the function is the shape of the array (hence the shape of your array being b.shape -> (1, 5, 3, 6, 2))

Comments

0

np.ndarray(shape, dtype=float....) the argument require shape, so it will create a new array with shape (1, 5, 3, 6, 2) in your example

a = [1, 5, 3, 6, 2]
b = np.ndarray(a)
print(b.shape)

return

(1, 5, 3, 6, 2)

what you want is np.array

a = [1, 5, 3, 6, 2]
b = np.array(a)
print(b.shape)

return

(5,)

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.