-1

Can someone help me create a 2D empty array of fixed dimension? I am using python 2.7 and numpy. The following code should work, but gives me weird output. I could also use help adding elements to a specific location in this list/array.

Code:

import numpy as np
cap = np.empty((10,2))

print(cap)

Output:

[[  5.87518338e-291   2.84727934e-291]
 [  2.88745078e-291   2.84747506e-291]
 [  2.84757292e-291   2.84767078e-291]
 [  2.86057604e-291   2.84443225e-293]
 [  2.77304046e-291   2.84786650e-291]
 [  2.84801329e-291   7.21189910e-292]
 [  2.86106534e-291   2.86118767e-291]
 [  7.05492591e-293   2.84825794e-291]
 [  2.52065398e-293   1.06489794e-292]
 [  2.52073043e-293   9.05496584e-292]]

Process finished with exit code 0

Haris
  • 27
  • 1
  • 1
  • 4
  • 1
    That is an `empty` array, at least as defined by `numpy`. The values are random, what happened to be in those memory slots. If you wanted all 0s, use `np.zeros`. `np.empty` is fine if you are going to fill all slots with your own values. `np.zeros` is better if you just fill some slots. Read the docs. – hpaulj Nov 05 '17 at 21:04

1 Answers1

1

You can create empty (with random data or filled with zeros) with numpy like this, read more at the numpy docs:

import numpy as np

arr = np.empty([2, 2])
print(arr)

arr2 = np.zeros([2, 2])
print(arr2)
bastelflp
  • 9,362
  • 7
  • 32
  • 67