R Prabhakar
R Prabhakar

Reputation: 91

np.zeros(dim,1) giving datatype not understood

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

Upvotes: 9

Views: 16081

Answers (2)

Stryker
Stryker

Reputation: 6120

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

Upvotes: 4

Joe
Joe

Reputation: 7121

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)

Upvotes: 8

Related Questions