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
  • 1
    Does this answer your question? [What is the difference between ndarray and array in numpy?](https://stackoverflow.com/questions/15879315/what-is-the-difference-between-ndarray-and-array-in-numpy) – Trenton McKinney Jun 30 '20 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. – ryabchenko-a Jun 30 '20 at 16:24
  • 1
    Probably floating point precision. They are all equivalently 0 – Trenton McKinney Jun 30 '20 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. – hpaulj Jun 30 '20 at 16:28
  • [Why are floating point numbers inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Trenton McKinney Jun 30 '20 at 16:30

4 Answers4

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]
Mark Bakker
  • 799
  • 1
  • 6
  • 15
  • 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? – ryabchenko-a Jun 30 '20 at 16:22
  • 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. – Mark Bakker Jul 02 '20 at 20:34
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.

Rambatino
  • 4,716
  • 1
  • 33
  • 56
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))

Youyoun
  • 338
  • 1
  • 5
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,)
Ian
  • 99
  • 11