日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NumPy之:数据类型

發布時間:2024/2/28 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NumPy之:数据类型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 簡介
  • 數組中的數據類型
    • 類型轉換
    • 查看類型
  • 數據溢出

簡介

我們知道Python中有4種數字類型,分別是int,float,bool和complex。作為科學計算的NumPy,其數據類型更加的豐富。

今天給大家詳細講解一下NumPy中的數據類型。

數組中的數據類型

NumPy是用C語言來實現的,我們可以對標一下NumPy中數組中的數據類型跟C語言中的數據類型:

Numpy 中的類型C 中的類型說明
np.bool_boolBoolean (True or False) stored as a byte
np.bytesigned charPlatform-defined
np.ubyteunsigned charPlatform-defined
np.shortshortPlatform-defined
np.ushortunsigned shortPlatform-defined
np.intcintPlatform-defined
np.uintcunsigned intPlatform-defined
np.int_longPlatform-defined
np.uintunsigned longPlatform-defined
np.longlonglong longPlatform-defined
np.ulonglongunsigned long longPlatform-defined
np.half / np.float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
np.singlefloatPlatform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa
np.doubledoublePlatform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa.
np.longdoublelong doublePlatform-defined extended-precision float
np.csinglefloat complexComplex number, represented by two single-precision floats (real and imaginary components)
np.cdoubledouble complexComplex number, represented by two double-precision floats (real and imaginary components).
np.clongdoublelong double complexComplex number, represented by two extended-precision floats (real and imaginary components).

我們在Ipython環境中隨機查看一下上面的類型到底是什么:

import numpy as npIn [26]: np.byte Out[26]: numpy.int8In [27]: np.bool_ Out[27]: numpy.bool_In [28]: np.ubyte Out[28]: numpy.uint8In [29]: np.short Out[29]: numpy.int16In [30]: np.ushort Out[30]: numpy.uint16

所以上面的數據類型,其底層還是固定長度的數據類型,我們看下到底有哪些:

Numpy 類型C 類型說明
np.int8int8_tByte (-128 to 127)
np.int16int16_tInteger (-32768 to 32767)
np.int32int32_tInteger (-2147483648 to 2147483647)
np.int64int64_tInteger (-9223372036854775808 to 9223372036854775807)
np.uint8uint8_tUnsigned integer (0 to 255)
np.uint16uint16_tUnsigned integer (0 to 65535)
np.uint32uint32_tUnsigned integer (0 to 4294967295)
np.uint64uint64_tUnsigned integer (0 to 18446744073709551615)
np.intpintptr_tInteger used for indexing, typically the same as ssize_t
np.uintpuintptr_tInteger large enough to hold a pointer
np.float32float
np.float64 / np.float_doubleNote that this matches the precision of the builtin python float.
np.complex64float complexComplex number, represented by two 32-bit floats (real and imaginary components)
np.complex128 / np.complex_double complexNote that this matches the precision of the builtin python complex.

所有這些類型都是 dtype 對象的實例。常用的有5種基本類型,分別是bool,int,uint,float和complex。

類型后面帶的數字表示的是該類型所占的字節數。

上面表格中有一些 Platform-defined的數據類型,這些類型是跟平臺相關的,在使用的時候要特別注意。

這些dtype類型可以在創建數組的時候手動指定:

>>> import numpy as np >>> x = np.float32(1.0) >>> x 1.0 >>> y = np.int_([1,2,4]) >>> y array([1, 2, 4]) >>> z = np.arange(3, dtype=np.uint8) >>> z array([0, 1, 2], dtype=uint8)

由于歷史原因,為了向下兼容,我們也可以在創建數組的時候指定字符格式的dtype。

>>> np.array([1, 2, 3], dtype='f') array([ 1., 2., 3.], dtype=float32)

上面的 f 表示的是float類型。

類型轉換

如果想要轉換一個現有的數組類型,可以使用數組自帶的astype方法,也可以調用np的強制轉換方法:

In [33]: z = np.arange(3, dtype=np.uint8)In [34]: z Out[34]: array([0, 1, 2], dtype=uint8)In [35]: z.astype(float) Out[35]: array([0., 1., 2.])In [36]: np.int8(z) Out[36]: array([0, 1, 2], dtype=int8)

注意,上面我們使用了 float , Python將會把float 自動替換成為 np.float_,同樣的簡化格式還有 int == np.int_, bool == np.bool_, complex == np.complex_. 其他的數據類型不能使用簡化版本。

查看類型

查看一個數組的數據類型可以使用自帶的dtype屬性:

In [37]: z.dtype Out[37]: dtype('uint8')

dtype作為一個對象,本身也可以進行一些類型判斷操作:

>>> d = np.dtype(int) >>> d dtype('int32')>>> np.issubdtype(d, np.integer) True>>> np.issubdtype(d, np.floating) False

數據溢出

一般來說,如果超出了數據的范圍是會報異常的。比如我們有一個非常長的int值:

In [38]: a= 1000000000000000000000000000000000000000000000000000000000000000000000000000000In [39]: a Out[39]: 1000000000000000000000000000000000000000000000000000000000000000000000000000000In [40]: np.int(1000000000000000000000000000000000000000000000000000000) Out[40]: 1000000000000000000000000000000000000000000000000000000In [41]: np.int32(1000000000000000000000000000000000000000000000000000000) --------------------------------------------------------------------------- OverflowError Traceback (most recent call last) <ipython-input-41-71feb4433730> in <module>() ----> 1 np.int32(1000000000000000000000000000000000000000000000000000000)

上面的數字太長了,超出了int32的范圍,就會拋出異常。

但是NumPy的有些操作,如果超出范圍之后,并不會報異常,而是正常范圍,這時候我們就需要注意了:

In [43]: np.power(100, 8, dtype=np.int32) Out[43]: 1874919424In [44]: np.power(100, 8, dtype=np.int64) Out[44]: 10000000000000000

NumPy提供了兩個方法來測量int和float的范圍,numpy.iinfo 和 numpy.finfo :

In [45]: np.iinfo(int) Out[45]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)In [46]: np.iinfo(np.int32) Out[46]: iinfo(min=-2147483648, max=2147483647, dtype=int32)In [47]: np.iinfo(np.int64) Out[47]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

如果64位的int還是太小的話,可以使用np.float64,float64可以使用科學計數法,所以能夠得到更大范圍的結果,但是其精度可能會縮小。

In [48]: np.power(100, 100, dtype=np.int64) Out[48]: 0In [49]: np.power(100, 100, dtype=np.float64) Out[49]: 1e+200

本文已收錄于 http://www.flydean.com/02-python-numpy-datatype/

最通俗的解讀,最深刻的干貨,最簡潔的教程,眾多你不知道的小技巧等你來發現!

歡迎關注我的公眾號:「程序那些事」,懂技術,更懂你!

總結

以上是生活随笔為你收集整理的NumPy之:数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。