Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type parms of np.ndarray

when I type

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)

pylance tells me a's type is:

(variable) a: ndarray[Unknown, Unknown]

I have no idea about these two Unknown mean.

Referrer to the stub file

class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):

I still don't know about it. How can I fill them?

like image 571
AsukaMinato Avatar asked Feb 27 '26 13:02

AsukaMinato


1 Answers

Those unknown refer to the shape and data type of the array created.

Since np.array is just np.ndarray(shape=(1,)) they have the same return type: ndarray[_ShapeType@ndarray, _DType_co@ndarray]

Therefore your code should work like this:

import numpy as np

a: np.ndarray[int, np.dtype[np.float64]] = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)

where int refers to the size tuple datatypes and np.dtype[np.float64] the array data's type

like image 143
9 revs, 2 users 97% Avatar answered Mar 02 '26 03:03

9 revs, 2 users 97%



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!