python - 如何键入采用通用数组的numpy函数
问题描述
根据numpy 文档,要声明我应该使用的通用 numpynp.floating[T]
类型T = TypeVar("T", bound=npt.NBitBase)
。然后 NDarray 采用标量类型。如何将两者结合起来创建一个采用通用浮点数组的函数?以下:
T = TypeVar("T", bound=npt.NBitBase)
def entropy(x: npt.NDArray[np.floating[T]]) -> np.floating[T]:
return cast(np.floating[T], np.sum(x * np.log(x)))
给我:TypeError: 'type' object is not subscriptable
解决方案
推荐阅读
- react-native - React Native Mi 秤体重数据
- javascript - 事件侦听器如何指向不同的对象?
- php - 如何根据laravel中的条件切换模型中的数据库表
- java - 如何从另一个线程访问变量
- apache - 修补 REST API 资源的机会,无需 HTTP PATCH
- javascript - 在 React 中,“history.push('/')”在某些组件中不起作用
- react-native - 是否可以使用 Class 组件而不是 Hook 来实现 Native Ads?
- bash - 使用 shell 将新的 webhook 附加到 alertmanager 配置文件
- flutter - Flutter 使用 customplot 绘制医学绘图问题
- dtn - 请告诉我如何使用一个模拟器同时使用两种交付协议