赞
踩
numpy中的数据类型转换,不能直接改原数据的dtype! 只能用函数astype()。否则你的元素个数可能会倍增或倍减,数值也会对应不上!
第一种情况:
import numpy as np
a = np.array([0.88518868, 0.4527473 ,0.61944059 ,0.1480421 ])
print(a)
print(a.dtype)
print('\n'+'-'*50+'\n')
a.dtype = 'float32'
print(a)
print(a.dtype)
结果:
[0.88518868 0.4527473 0.61944059 0.1480421 ]
float64
--------------------------------------------------
[ 1.76177241e-06 1.84629714e+00 -6.12526950e+09 1.72637355e+00
7.94156442e-28 1.77986014e+00 1.07201006e-16 1.52304208e+00]
float32
第二种情况:
import numpy as np a = np.array([14555555554.88518868, 0.45274735555 ,0.61944059444 ,0.14804215564 ]) print(a) print(a.dtype) print('\n'+'-'*50+'\n') b= a.astype(np.float32) print(b) print(b.dtype) print('\n'+'-'*50+'\n') c= a.astype(np.float16) print(c) print(c.dtype)
结果:
[1.45555556e+10 4.52747356e-01 6.19440594e-01 1.48042156e-01]
float64
--------------------------------------------------
[1.4555556e+10 4.5274734e-01 6.1944062e-01 1.4804216e-01]
float32
--------------------------------------------------
[ inf 0.4526 0.6196 0.1481]
float16
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。