Python?NumPy教程之數(shù)據(jù)類型對象詳解
每個 ndarray 都有一個關(guān)聯(lián)的數(shù)據(jù)類型 (dtype) 對象。這個數(shù)據(jù)類型對象(dtype)告訴我們數(shù)組的布局。這意味著它為我們提供了以下信息:
- 數(shù)據(jù)類型(整數(shù)、浮點數(shù)、Python 對象等)
- 數(shù)據(jù)大?。ㄗ止?jié)數(shù))
- 數(shù)據(jù)的字節(jié)順序(小端或大端)
- 如果數(shù)據(jù)類型是子數(shù)組,它的形狀和數(shù)據(jù)類型是什么。
ndarray 的值存儲在緩沖區(qū)中,可以將其視為連續(xù)的內(nèi)存字節(jié)塊。所以這些字節(jié)將如何被解釋由dtype對象給出。
構(gòu)造數(shù)據(jù)類型(dtype)對象
數(shù)據(jù)類型對象是 numpy.dtype 類的一個實例,可以使用numpy.dtype
.
參數(shù):
obj: 要轉(zhuǎn)換為數(shù)據(jù)類型對象的對象。
align : [bool, optional] 向字段添加填充以匹配 C 編譯器為類似 C 結(jié)構(gòu)輸出的內(nèi)容。
copy : [bool, optional] 制作數(shù)據(jù)類型對象的新副本。如果為 False,則結(jié)果可能只是對內(nèi)置數(shù)據(jù)類型對象的引用。
# Python 程序創(chuàng)建數(shù)據(jù)類型對象 import numpy as np # np.int16 被轉(zhuǎn)換為數(shù)據(jù)類型對象。 print(np.dtype(np.int16))
輸出:
int16
# Python 程序創(chuàng)建一個包含 32 位大端整數(shù)的數(shù)據(jù)類型對象 import numpy as np # i4 表示大小為 4 字節(jié)的整數(shù) # > 表示大端字節(jié)序和 # < 表示小端編碼。 # dt 是一個 dtype 對象 dt = np.dtype('>i4') print("Byte order is:",dt.byteorder) print("Size is:", dt.itemsize) print("Data type is:", dt.name)
輸出:
Byte order is: >
Size is: 4
Name of data type is: int32
類型說明符(在上述情況下為 i4)可以采用不同的形式:
b1、i1、i2、i4、i8、u1、u2、u4、u8、f2、f4、f8、c8、c16、a(表示字節(jié)、整數(shù)、無符號整數(shù)、浮點數(shù)、指定字節(jié)長度的復數(shù)和定長字符串)
int8,...,uint8,...,float16, float32, float64, complex64, complex128(這次是位大?。?/p>
注意: dtype 與 type 不同。
# 用于區(qū)分類型和數(shù)據(jù)類型的 Python 程序。 import numpy as np a = np.array([1]) print("type is: ",type(a)) print("dtype is: ",a.dtype)
輸出:
type is:
dtype is: int32
具有結(jié)構(gòu)化數(shù)組的數(shù)據(jù)類型對象
數(shù)據(jù)類型對象對于創(chuàng)建結(jié)構(gòu)化數(shù)組很有用。結(jié)構(gòu)化數(shù)組是包含不同類型數(shù)據(jù)的數(shù)組??梢越柚侄卧L問結(jié)構(gòu)化數(shù)組。
字段就像為對象指定名稱。在結(jié)構(gòu)化數(shù)組的情況下,dtype 對象也將是結(jié)構(gòu)化的。
# 用于演示字段使用的 Python 程序 import numpy as np # 一種結(jié)構(gòu)化數(shù)據(jù)類型,包含一個 16 字符的字符串(在“name”字段中)和兩個 64 位浮點數(shù)的子數(shù)組(在“grades”字段中) dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))]) # 具有字段等級的對象的數(shù)據(jù)類型 print(dt['grades']) # 具有字段名稱的對象的數(shù)據(jù)類型 print(dt['name'])
輸出:
('<f8', (2,))
# Python 程序演示了數(shù)據(jù)類型對象與結(jié)構(gòu)化數(shù)組的使用。 import numpy as np dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))]) # x 是一個包含學生姓名和分數(shù)的結(jié)構(gòu)化數(shù)組。 # 學生姓名的數(shù)據(jù)類型是np.unicode_,分數(shù)的數(shù)據(jù)類型是np.float(64) x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt) print(x[1]) print("Grades of John are: ", x[1]['grades']) print("Names are: ", x['name'])
輸出:
('John', [ 6., 7.])
Grades of John are: [ 6. 7.]
Names are: ['Sarah' 'John']
到此這篇關(guān)于Python NumPy教程之數(shù)據(jù)類型對象詳解的文章就介紹到這了,更多相關(guān)Python NumPy數(shù)據(jù)類型對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)的文軒網(wǎng)爬蟲完整示例
這篇文章主要介紹了Python實現(xiàn)的文軒網(wǎng)爬蟲,結(jié)合完整實例形式分析了Python爬蟲爬取文軒網(wǎng)圖書信息的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05python multiprocessing 多進程并行計算的操作
這篇文章主要介紹了python multiprocessing 多進程并行計算的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03