python 中的 type(), dtype(), astype()的区别

python 中的 type(), dtype(), astype()的区别函数 说明 type() 返回数据结构类型(list、dict、numpy.ndarray等) dtype() 返回数据元素的数据类型(int、float等) 备注:1)由于list、dict等可以包含不同的数据类型,因此不可调用dtype()函数 2)np.array中要求所有元素属于同一数据类型,因此可调用d…

大家好,又见面了,我是你们的朋友全栈君。

 

函数 说明
type() 返回数据结构类型(list、dict、numpy.ndarray 等)
dtype()

返回数据元素的数据类型(int、float等)

备注:1)由于 list、dict 等可以包含不同的数据类型,因此不可调用dtype()函数

           2)np.array 中要求所有元素属于同一数据类型,因此可调用dtype()函数

astype()

改变np.array中所有数据元素的数据类型。

备注:能用dtype() 才能用 astype()

 测试代码:

import numpy as np
class Myclass():
    pass

a = [[1,2,3],[4,5,6]]
b = {'a':1,'b':2,'c':3}
c = np.array([1,2,3])
d = Myclass()
e = np.linspace(1,5,10)
c_ = c.astype(np.float)
f = 10

print("type(a)=", type(a))  # type(a)= <class 'list'>
print("type(b)=", type(b))  # type(b)= <class 'dict'>
print("type(c)=", type(c))  # type(c)= <class 'numpy.ndarray'>
print("type(d)=", type(d))  # type(d)= <class '__main__.Myclass'>
print("type(e)=", type(e))  # type(e)= <class 'numpy.ndarray'>
print("type(f)=", type(f))  # type(f)= <class 'int'>
print("type(c_)=", type(c_)) # type(c_)= <class 'numpy.ndarray'>



# print(a.dtype) ## AttributeError: 'list' object has no attribute 'dtype'
# print(b.dtype) ## AttributeError: 'dict' object has no attribute 'dtype'
print(c.dtype)  ## int32
# print(d.dtype) ## AttributeError: 'Myclass' object has no attribute 'dtype'
print(e.dtype)  ## float64
print(c_.dtype)  ## float64
# print(f.dtype)  ## AttributeError: 'int' object has no attribute 'dtype'

# print(a.astype(np.int)) ## AttributeError: 'list' object has no attribute 'astype'
# print(b.astype(np.int)) ## AttributeError: 'dict' object has no attribute 'astype'
print(c.astype(np.int)) ## [1 2 3]
# print(d.astype(np.int)) ## AttributeError: 'Myclass' object has no attribute 'astype'
print(e.astype(np.int))  ## [1 1 1 2 2 3 3 4 4 5]
# print(f.astype(np.int))  ## AttributeError: 'int' object has no attribute 'astype'

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134155.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • getComputedStyle()与currentStyle()、style()方法「建议收藏」

    JS使用getComputedStyle()方法获取CSS属性值 在对网页进行调试的过程中,经常会用到js来获取元素的CSS样式,方法有很多很多,现在仅把我经常用的方法总结如下:1.obj.style:这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在&lt;styletype="text/c…

    2022年4月9日
    48
  • 实战Vue:基于Vue的移动端购物商城

    实战Vue:基于Vue的移动端购物商城基于Vue的移动端购物商城

    2022年6月5日
    32
  • vue刷新后数据消失_vue数据更新页面不更新

    vue刷新后数据消失_vue数据更新页面不更新vue页面刷新

    2025年6月13日
    3
  • 如何卸载tensorflow

    如何卸载tensorflowwindows系统下:1.按windows+r2.输入cmd3.输入pipuninstalltensorflow中间会提示输入Y或者N,输入Y后按回车即可。如果提示找不到pip,或者pip不是内部指令,点击这里解决。https://blog.csdn.net/qq_29371155/article/details/105074987…

    2022年6月22日
    48
  • kfold交叉验证好处_sklearn交叉验证

    kfold交叉验证好处_sklearn交叉验证运用Kfold交叉验证时,在一个限度内k的值越大越好。因为k越大我们验证的次数就越多,最后取出来的平均数越能代表训练模型的准确度。但是k是需要在一个限度之内的。k太大有两个坏处。1.容易给机器造成过重负担,花费大量时间。2.每一次验证的测试集(或验证集)中数据太少,很难得到准确的误报率。总体而言,k一般取10,取值依不同项目情况而定,当然一定存在k&lt;n(训练集数据条数)。…

    2022年9月2日
    7
  • Map集合和List集合总结

    Map集合和List集合总结Map集合和List集合哪个效率更高List接口List集合是一个元素有序(存储有序)、可重复的集合,集合中的每个元素都有对应的索引,以便于查询和修改,List集合是允许存储null值的。List集合可重复原因,请看源码:publicbooleanadd(Ee){ ensureCapacityInternal(size+1);//IncrementsmodCount!…

    2022年5月22日
    55

发表回复

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

关注全栈程序员社区公众号