相关疑难解决方法(0)

类型提示 2D numpy 数组

作为这个问题的后续,我有一个函数将返回numpy.array固定列但可变行的二维

import numpy.typing as npt

def example() -> npt.ArrayLike:
    data = np.array([[1,2,3],
                     [4,5,6],
                     ...,
                     [x,y,z]])
Run Code Online (Sandbox Code Playgroud)

我如何具体提示返回的数组将是3N(变量)行组成的列?

python numpy type-hinting

22
推荐指数
1
解决办法
1万
查看次数

Python中的Numpy类型提示(PEP 484)

我想向采用numpy数组作为输入并返回字符串的方法中添加类型提示。这个numpy数组包含浮点数,所以我尝试了:

import numpy as np
def foo(array: np.ndarray[np.float64]) -> str:
Run Code Online (Sandbox Code Playgroud)

但由于,它无法正常工作TypeError: 'type' object is not subscriptable

我发现了这一点,但无法按照讨论进行!

python numpy pep

8
推荐指数
1
解决办法
1774
查看次数

使用 mypy 对 NumPy ndarray 进行特定类型注释

NumPy 1.20 中添加了对类型注释的支持。我试图弄清楚如何告诉 mypy 数组填充了特定类型的元素,注释np.ndarray[np.dcomplex]给出了 mypy error "ndarray" expects no type arguments, but 1 given

编辑:这个问题与numpy.ndarray 的类型提示/注释(PEP 484)不同,因为这个问题是在 4 年前提出的,当时没有任何官方支持类型提示。我问的是什么是官方的 方法,现在numpy 1.20实际上支持类型提示。https://numpy.org/doc/stable/reference/typing.html#module-numpy.typing上的文档指出,那里的最佳答案似乎只说你不应该用类型提示做的事情,而不是解释什么你应该做的。

numpy python-3.x mypy numpy-ndarray

8
推荐指数
1
解决办法
961
查看次数

标签 统计

numpy ×3

python ×2

mypy ×1

numpy-ndarray ×1

pep ×1

python-3.x ×1

type-hinting ×1