numpy的数据类型

numpy的数据类型主要包括整数、浮点数、复数、布尔值、字符串、python对象类型,如下图所示


1、使用dtype指定创建数组的数据类型
    #默认是浮点数是float64
    arr9 = np.array([1.,2.,3.],dtype=np.float32)
    print(arr9.dtype)
    #float32
    #通过类型代码指定
    arr9 = np.array([1.,2.,3.],dtype="f4")
    print(arr9.dtype)
    #float32
2、使用astype来改变数组的数据类型
    #指定数组的数据类型为字符串
    arr9 = np.array(["1.1","2.3","3.1"],dtype="S")
    arr9 = arr9.astype("f8")
    print(arr9)
    #[ 1.1  2.3  3.1]
    print(arr9.dtype)
    #float64
注意:在使用astype的时候,系统会创建一个新的数组(原始数组的一份拷贝),即使指定的数据类型与原始数组相同。

猜你喜欢

转载自blog.csdn.net/sinat_29957455/article/details/80412885