Python numpy資料型別

2021-10-04 07:09:08 字數 3522 閱讀 7401

本章內容了解 numpy 資料。numpy陣列屬性詳見

numpy 支援的資料型別比 python 內建的型別要多很多,基本上可以和 c 語言的資料型別對應上,其中部分型別對應為 python 內建的型別。下表列舉了常用 numpy 基本型別。

名稱描述

bool_

布林型資料型別(true 或者 false)

int_

預設的整數型別(類似於 c 語言中的 long,int32 或 int64)

intc

與 c 的 int 型別一樣,一般是 int32 或 int 64

intp

用於索引的整數型別(類似於 c 的 ssize_t,一般情況下仍然是 int32 或 int64)

int8

位元組(-128 to 127)

int16

整數(-32768 to 32767)

int32

整數(-2147483648 to 2147483647)

int64

整數(-9223372036854775808 to 9223372036854775807)

uint8

無符號整數(0 to 255)

uint16

無符號整數(0 to 65535)

uint32

無符號整數(0 to 4294967295)

uint64

無符號整數(0 to 18446744073709551615)

float_

float64 型別的簡寫

float16

半精度浮點數,包括:1 個符號位,5 個指數字,10 個尾數字

float32

單精度浮點數,包括:1 個符號位,8 個指數字,23 個尾數字

float64

雙精度浮點數,包括:1 個符號位,11 個指數字,52 個尾數字

complex_

complex128 型別的簡寫,即 128 位複數

complex64

複數,表示雙 32 位浮點數(實數部分和虛數部分)

complex128

複數,表示雙 64 位浮點數(實數部分和虛數部分)

numpy 的數值型別實際上是 dtype 物件的例項,並對應唯一的字元,包括 np.bool_,np.int32,np.float32,等等。

資料型別物件是用來描述與陣列對應的記憶體區域如何使用,這依賴如下幾個方面:

位元組順序是通過對資料型別預先設定"<"或">"來決定的。"<"意味著小端法(最小值儲存在最小的位址,即低位組放在最前面)。">"意味著大端法(最重要的位元組儲存在最小的位址,即高位組放在最前面)。

dtype 物件是使用以下語法構造的:

numpy.dtype(object, align, copy)
例項 1

import numpy as np # 使用標量型別

dt = np.dtype(np.int32)

print(dt)

輸出結果為:int32

例項 2

import numpy as np # int8, int16, int32, int64 四種資料型別可以使用字串 'i1', 'i2','i4','i8' 代替

dt = np.dtype('i4')

print(dt)

輸出結果為:int32

例項 3

import numpy as np # 位元組順序標註

dt = np.dtype('print(dt)

輸出結果為:int32

下面例項展示結構化資料型別的使用,型別欄位和對應的實際型別將被建立。

例項 4

import numpy as np # 首先建立結構化資料型別

dt = np.dtype([('age',np.int8)])

print(dt)

輸出結果為:[('age', 'i1')]

例項 5

import numpy as np# 將資料型別應用於 ndarray 物件

dt = np.dtype([('age',np.int8)])

a = np.array([(10,),(20,),(30,)], dtype = dt)

print(a)

輸出結果為:[(10,) (20,) (30,)]

例項 6

import numpy as np # 型別欄位名可以用於訪問實際的 age 列

dt = np.dtype([('age',np.int8)])

a = np.array([(10,),(20,),(30,)], dtype = dt)

print(a['age'])

輸出結果為:[10 20 30]

下面的示例定義乙個結構化資料型別 student,包含字串字段 name,整數字段 age,及浮點字段 marks,並將這個 dtype 應用到 ndarray 物件。

例項 7

import numpy as np

student = np.dtype([('name','s20'), ('age', 'i1'), ('marks', 'f4')])

print(student)

輸出結果為:[('name', 's20'), ('age', 'i1'), ('marks', 'f4')]

例項 8

import numpy as np

student = np.dtype([('name','s20'), ('age', 'i1'), ('marks', 'f4')])

a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)

print(a)

輸出結果為:[('abc', 21, 50.0), ('xyz', 18, 75.0)]

每個內建型別都有乙個唯一定義它的字元**,如下:

字元對應型別

b布林型

i(有符號) 整型

u無符號整型 integer

f浮點型

c複數浮點型

mtimedelta(時間間隔)

mdatetime(日期時間)

o(python) 物件

s, a

(byte-)字串

uunicode

v原始資料 (void)

python numpy 資料型別轉換

numpy資料型別轉換需要呼叫方法astype 不能直接修改dtype。呼叫astype返回資料型別修改後的資料,但是源資料的型別不會變,需要進一步對源資料的賦值操作才能改變。例如 a np.array 1.1,1.2 a.dtype dtype float64 a.astype np.int16 ...

python numpy 資料型別轉換

python numpy 資料型別轉換 numpy資料型別轉換需要呼叫方法astype 不能直接修改dtype。呼叫astype返回資料型別修改後的資料,但是源資料的型別不會變,需要進一步對源資料的賦值操作才能改變。例如 a np.array 1.1,1.2 a.dtype dtype float6...

python numpy 資料型別轉換

a np.array 1.1,1.2 a.dtype dtype float64 a.astype np.int16 array 1,1 dtype int16 a.dtype dtype float64 a的資料型別並沒有變 a a.astype np.int16 賦值操作後a的資料型別變化 a....