加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 返回类型numpy.minimum()?

发布时间:2020-12-20 11:18:16 所属栏目:Python 来源:网络整理
导读:当我在布尔数组上使用numpy函数minimum()和maximum()时,结果的类型打印为numpy.int32.但是,与numpy.int32类型的比较失败(即使在强制转换后).这是一个错误吗? g = np.ones((5,5),dtype = np.bool)h = np.maximum(g,4)i = np.int32(h)print 'type of g ',g.dt
当我在布尔数组上使用numpy函数minimum()和maximum()时,结果的类型打印为numpy.int32.但是,与numpy.int32类型的比较失败(即使在强制转换后).这是一个错误吗?

g = np.ones((5,5),dtype = np.bool)
h = np.maximum(g,4)
i = np.int32(h)

print 'type of g ',g.dtype.type   # prints <type 'numpy.bool_'>
print 'type of h ',h.dtype.type   # prints <type 'numpy.int32'>
print 'type of i ',i.dtype.type   # prints <type 'numpy.int32'>

print h.dtype.type == i.dtype.type # prints True
print h.dtype.type == np.int32     # prints False
print i.dtype.type == np.int32     # prints False
print i.dtype.type == np.bool_     # prints False

解决方法

对我来说似乎很正常.使用np.maximum返回g和4的最大值,即4(True == 1).你得到一个四分之一的矩阵,它是整数类型.

如果您使用此语法进行类型比较,则它们可以工作:h.dtype.type == np.dtype(np.int32)或更简单地说h.dtype == np.int32.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读