美文网首页python基础
numpy数据类型dtype转换

numpy数据类型dtype转换

作者: 不忘初心的女孩 | 来源:发表于2018-11-10 17:06 被阅读0次

1.导入numpy

import numpy as np

生成一个浮点数组

a=np.random.random (4)
print(a)#[0.83168059 0.36136573 0.21077195 0.83809788]
print(a.dtype)#float64
print(a.shape)#(4,)

改变dtype,发现数组长度翻倍!

a.dtype ="float32"
print(a)#[ 4.6087299e-03  1.8518010e+00 -7.7555648e+07  1.5651706e+00
          # 2.0775437e+27  1.8061410e+00  6.5161847e-38  1.8454261e+00]
print(a.shape)#(8,)

** 改变dtype,数组长度再次翻倍!**

a.dtype ="float16"
print(a)#[-2.727e+01  1.401e-02  8.140e-04  1.974e+00 -5.856e+03 -6.245e-01
        # 3.616e-02  1.951e+00  2.217e+00 -6.560e+03  5.547e+00  1.983e+00
        # -6.960e+03  3.381e-02  1.031e-03  1.940e+00]
print(a.shape)#(16,)

改变dtype=‘float‘,发现默认就是float64,长度也变回最初的4

a.dtype ="float"
print(a)#[0.47555571 0.83067659 0.72388204 0.45367421]
print(a.shape)#(4,)
print(a.dtype)#float64

把a变为整数,观察其信息

a.dtype="int64"
print(a)#[4578487747925135360 4605336301850395495 4605895735205766151 4605821938832751062]
print(a.shape)#(4,)

改变dtype,发现数组长度翻倍!

a.dtype="int32"
print(a)#[  824871971  1072684806  1876090816  1066641210  1074107524  1071642489
        # -1396153144  1070270040]
print(a.shape )#(8,)

改变dtype,发现数组长度再次翻倍!

a.dtype="int16"
print(a)#[ 31052   4922  16971  16337  22794  28694   7133  16365  13074  29367
       # 2651  16346   6957  11100 -29120  16361]
print(a.shape)#(16,)

** 改变dtype,发现数组长度再次翻倍!**

a.dtype ="int8"
print(a)#[  33  121   95   71   22   62  -28   63  -96   95   54   18  -61  -45
        # -86   63   99  -50 -128  122  -69  -84  -22   63  115 -122   54   -8
        # -14  -26  -21   63]
print(a.shape)#(32,)

改变dtype,发现整数默认int32!

a.dtype ="int"
print(a)#[ 1435759300  1071835120  -502263884  1072408896  -789422780  1070874281
        # -1289434146  1070773663]
print(a.shape)#(8,)
print(a.dtype)#int32

2.很多时候我们用numpy从文本文件读取数据作为numpy的数组,默认的dtype是float64。
但是有些场合我们希望有些数据列作为整数。如果直接改dtype=‘int‘的话,就会出错!原因如上,数组长度翻倍了!!!
下面的场景假设我们得到了导入的数据。我们的本意是希望它们是整数,但实际上是却是浮点数(float64)

b=np.array([1.,2.,3.,4.])
print(b.dtype)#float64

用 astype(int) 得到整数,并且不改变数组长度

c=b.astype(int)
print(c)#[1 2 3 4]
print(c.shape)#(4,)
print(c.dtype)#int32

如果直接改变b的dtype的话,b的长度翻倍了,这不是我们想要的(当然如果你想的话)

b.dtype="int"
print(b.dtype)#int32
print(b)#[         0 1072693248          0 1073741824          0 1074266112
        # 0 1074790400]
print(b.shape)#(8,)

3.结论
numpy中的数据类型转换,不能直接改原数据的dtype! 只能用函数astype()。

相关文章

  • numpy转换数据类型

    转:Numpy数据类型转换astype,dtype - CSDN博客 np =numpy.array(list) ...

  • Numpy

    Numpy中数据的常用的保存方法 numpy.genfromtxt读取本地文件 dtype转换数据类型,关键字设置...

  • NumPy 基础入门

    NumPy 基础入门 多维数组 数值类型 数据类型对象(dtype) 记录类型 操作形状 堆叠 分割 属性 转换

  • NumPy之:数据类型对象dtype

    简介 之前讲到了NumPy中有多种数据类型,每种数据类型都是一个dtype(numpy.dtype )对象。今天我...

  • numpy数据类型dtype转换

    1.导入numpy 生成一个浮点数组 改变dtype,发现数组长度翻倍! ** 改变dtype,数组长度再次翻倍!...

  • NumPy基础

    1. 创建ndarray 2.ndarray数据类型 arr.shapearr.dtype显式转换dtype: f...

  • Numpy部分常用函数整理

    1、数据类型numpy.dtype()从已有的数组创建数组:numpy.asarray()等差数列构成的一维数组:...

  • Numpy

    1.numpy数据存储与列表的区别 2.numpy数组的创建 直接创建 dtype转换np.astype()相当于...

  • Numpy数据类型对象(dtype)

    https://blog.csdn.net/Zhili_wang/article/details/81140282...

  • Numpy dtype

    https://www.runoob.com/numpy/numpy-dtype.html

网友评论

    本文标题:numpy数据类型dtype转换

    本文链接:https://www.haomeiwen.com/subject/wgnzxqtx.html