python中dtype的用法_python中type函数判断数据类型

python中dtype的用法_python中type函数判断数据类型(1)type()是python内置的函数

(1)type()是python内置的函数。type() 返回数据结构类型(list、dict、numpy.ndarray 等)
(2)dtype 返回数据元素的数据类型(int、float等)
(3)astype() 改变np.array中所有数据元素的数据类型。
————————————
备注:
1)由于 list、dict 等可以包含不同的数据类型,因此没有dtype属性
2)np.array 中要求所有元素属于同一数据类型,因此有dtype属性
备注:能用dtype() 才能用 astype()

l1 = [1,2,4]
ar1 = np.array(l1)
print(type(l1)) #<class 'list'>
print(l1.dtype) #会报错

在这里插入图片描述


ar1 = np.array(l1)
print(type(a1)) #<class 'list'>
print(ar1.dtype) #会报错

在这里插入图片描述
注意下面的例子

ar1 = np.array(l1)
t1 = torch.from_numpy(ar1)
print(type(a1))   #<class 'numpy.ndarray'>
print(ar1.dtype)  #int32
#注意print(ar1.type())会报错

print(t1.type())   #torch.IntTensor
print(type(t1))    #<class 'torch.Tensor'>
print(t1.dtype)    #torch.int32

在这里插入图片描述

#a.astype(dtype) a不变
#返回Copy of the array, cast to a specified type.
ar1 = np.arange(10,dtype=float)
ar2 = ar1.astype(np.int)
print(ar1,ar1.dtype)
print(ar2,ar2.dtype)

在这里插入图片描述

今天的文章python中dtype的用法_python中type函数判断数据类型分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/71476.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注