Victor
Victor

Reputation: 17097

Data type not understood while creating a NumPy array

I am trying to create a 2 * 3 numpy array as below:

x_sample= np.array([31000,28.69,7055.47],[79000,3.9,16933.26]);

But I get:

TypeError: data type not understood

Why am I getting the error?

Upvotes: 5

Views: 19711

Answers (3)

djvg
djvg

Reputation: 14255

The TypeError: data type not understood also occurs when trying to create a structured array, if the names defined in the dtype argument are not of type str.

Consider this minimal example:

numpy.array([], dtype=[(name, int)])
  • fails in Python 2 if type(name) is unicode
  • fails in Python 3 if type(name) is bytes
  • succeeds in Python 2 and 3 if type(name) is str

(tested with Python 2.7 + numpy 1.14, and Python 3.6 + numpy 1.15)

Upvotes: 0

BENY
BENY

Reputation: 323266

You can try

np.vstack(([31000,28.69,7055.47],[79000,3.9,16933.26]))

Upvotes: 0

Grr
Grr

Reputation: 16079

You are missing brackets around the two lists.

x_sample= np.array([[31000,28.69,7055.47],[79000,3.9,16933.26]])

The way it was written the dtype argument was receiving the value [79000,3.9,16933.26], which obviously cannot be interpreted as a valid NumPy data type and caused the error.

Upvotes: 9

Related Questions