NumPy笔记:查看数据类型并修改操作(dtype,astype)


"""
    查看数据类型并修改操作
"""
 
import numpy as np
print("--------------常用数据类型----------------")
# 默认整数int32,小数float64
a = np.array([1, 1, 1])
b = np.array([1., 1, 1])
c = np.array([1, 1, 1], dtype=np.float32)
d = np.array([1, 1, 1], dtype=np.string_)
print("整型:", a.dtype)
print("浮点数:", b.dtype)
print("浮点数:", c.dtype)
print("字节数组:", d.dtype)
 
print("--------------数据类型修改----------------")
a = np.array([1, 2, 3, 4, 5])
print("字节数组:", a.tostring())
print("浮点数:", a.astype(np.float32))
 

 

整型: int32
浮点数: float64
浮点数: float32
字节数组: |S1
--------------数据类型修改----------------
字节数组: b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00'
浮点数: [1. 2. 3. 4. 5.]
 
 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM