TypeError: Field elements must be 2- or 3-tuples, got '5.0'

Viewed 23318
import numpy as np 
a = np.array([8.0,7.0,6.0],[5.0,4.0])
print(a)

I am trying to run this code but I get this error. (In Jupiter)


TypeError                                 Traceback (most recent call last)
<ipython-input-3-31ad187a1d2f> in <module>
      1 import numpy as np
----> 2 a = np.array([8.0,7.0,6.0],[5.0,4.0])
      3 print(a)

TypeError: Field elements must be 2- or 3-tuples, got '5.0'
3 Answers

It would help to know what you are trying to achieve with this code.

Assuming you are trying to create a 2D array with numpy - you need to make sure your dimensions are correct (first row with 3 element and second row with 2 element cannot make a 2D array).

And you need another [] around to make 2D array. Something like this -

import numpy as np 
a = np.array([[8.0,7.0,6.0],[5.0,4.0,3.0]]) 
print(a)

There are two mistakes here,

i. another element has to be inserted into the second array / removed from the first array,

ii. both of them have to be passed as first argument i.e. enclosed in braces.

Rewriting the same:

import numpy as np 
a = np.array(([8.0,7.0,6.0],[5.0,4.0,3.0]))
print(a)
  1 import numpy as np

----> 2 a = np.array([8.0,7.0,6.0],[5.0,4.0]) 3 print(a)

ANS : you need the "[ ]" around the Data like this below

  1 import numpy as np
  2 a = np.array( [ [8.0,7.0,6.0],[5.0,4.0] ] )
  3 print(a)
Related