函数 说明
type() 返回数据结构类型(list、list、numpy.ndarray等)
dtype() 返回数据结构类型(int、float等)备注:(1) 由于list、dict等可以包含不同的数据类型,因此不可调用dtype()函数 (2)np.arrray中要求所有元素属于同一数据类型,因此可调用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://blog.csdn.net/weixin_44533869/article/details/110654074