type、dtype和astype

type:python内置函数,用来获取变量的数据结构类型

>> print(type(x_train)) 
<class 'numpy.ndarray'>
>> a=[1,2,3,4]
>> print(type(a))
<class 'list'>

dtype:获取变量的数据类型
注: list、dict 等可以包含不同的数据类型,因此没有dtype属性。

>> print(x_train.dtype) 
uint8
a=[1,2,'s',200.2]
print(a.dtype)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-2377a652fbf4> in <module>
      1 a=[1,2,'s',200.2]
----> 2 print(a.dtype)

AttributeError: 'list' object has no attribute 'dtype'

astype:对数据类型进行转换

>>x_train = x_train.reshape(-1, 28, 28, 1)
>>print(x_train.dtype) 
uint8

>>x_train = x_train.astype('float32')
>>print(x_train.dtype) 
float32

你可能感兴趣的:(python,python,numpy)