Please explain this

Greetings my fellow learners, it is requested to explain that why is this coming ‘cannot interpret 3 as a datatype’.

{moderator edit: code removed}

Hi @Talha1234 ,

The parameter passed to np.zeros should be a matrix of size (m rows and j columns), which is represented by [m, j], a square bracket is missing here.

Here is the link to the reference doc on np.zeros()

This reply is helpful doesn’t quite explain the question about the error message. The linked np doc provides this…

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

The parameters expected are a comma delimited list with the second argument, if provided, establishing the dtype

dtype data-type, optional

The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.

In the OP code, a comma delimited list is provided, but the second argument is an integer, which is not an allowed numpy data type.

The first argument shape is described as

shape int or tuple of ints

Passing one integer for the first argument is allowed syntactically. In this case it wouldn’t have produced the desired output, but it doesn’t throw an error.

So the real answer to the question is that the second argument was invalid, though @Kic is correct that omitting the optional arguments altogether and using the two integers to provide a tuple of ints as the first (and only) argument is the functional intent. Cheers

Hi @ai_curious ,

In @Talha1234’s code, A_out=np.zeros(m, j); here m is 4 and j is 3.
The error message is telling you that there is a typeError, where 3 is being passed as datatype.

If you check the reference doc, you can see that the 1st parameter is ‘shape’ which is either an integer or a tuple, and the 2nd parameter is datatype. Because the way is called like this np.zeros(4,3) , the 2nd parameter is now the datatype, not a tuple.
np.zeros() is a function. If you want np.zeros() to have a tuple as its 1st parameter, you have to specify it, eg. [4,3] or (4,3). So the code will look like this np.zeros((4,3))

Hope this help.