8

I am very new to python and numpy. I'm trying to initialize a Row Vector with zeros as follows:

w = np.zeros(dim,1)

I'm getting the error TypeError: data type not understood. I appreciate any help. Thanks

1
  • 5
    Pass in a tuple: np.zeros((dim,1))
    – ayhan
    Aug 28, 2017 at 10:59

2 Answers 2

7

See the documentation on np.zeros

If you call it the way you did, the size is dim, and the data type argument dtype is 1, which is not a valid data type.

The solution is

import numpy as np
dim = 3 # number of entries
shp = (dim, 1) # shape tuple
x = np.zeros(shp) # second argument 'dtype' is not used, default is 'float'
print(x)
1
  • Thanks for this response. Tried the above mentioned solution. But it didn't work. Aug 30, 2017 at 9:34
3

You should call is like:

w = np.zeros((dim, 1))

Based on the docs:

numpy.zeros(shape, dtype=float, order='C')

In this case (dim,1) is the shape

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.