Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

numpy

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

like image 567
R Prabhakar Avatar asked Aug 28 '17 10:08

R Prabhakar


2 Answers

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)
like image 102
Joe Avatar answered Nov 03 '22 00:11

Joe


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

like image 23
Stryker Avatar answered Nov 03 '22 01:11

Stryker