Numpy入門系列之資料型別及陣列建立

2021-10-24 22:34:13 字數 4317 閱讀 7773

首先導包

import numpy as np
numpy.nan空值
np.nan

#out: nan

#注:nan = nan = nan

【例】兩個numpy.nan是不相等的。
import numpy as np

print

(np.nan == np.nan)

# false

print

(np.nan != np.nan)

# true

numpy.isnan(x, *args, **kwargs)

test element-wise for nan and return result as a boolean array.

根據 nan 和返回結果作為布林陣列的測試元素。

【例】

import numpy as np

x = np.array([1

,1,8

, np.nan,10]

)print

(x)# [ 1. 1. 8. nan 10.]

↑注:numpy裡面必須是同乙個型別的,如果型別不同會強制轉成乙個型別
x.dype

# dtype('float64')

x = np.array([1

,1,8

,10])

print

(x)x.dtype

# [ 1 1 8 10]

# dtype('int32')

y = np.isnan(x)

print

(y)# [false false false true false]

注:會逐個對比x內元素是否為空值
z = np.count_nonzero(y)

print

(z)# 1

正無窮大numpy.inf
np.inf

# inf

# 注:inf = inf = infty = infinity = pinf

圓周率numpy.pi
np.pi

# 3.141592653589793

自然常數numpy.e
np.e

# 2.718281828459045

常見的資料型別

python 原生的資料型別相對較少, bool、int、float、str等。這在不需要關心資料在計算機中表示的所有方式的應用中是方便的。然而,對於科學計算,通常需要更多的控制。為了加以區分 numpy 在這些型別名稱末尾都加了「_」。

下表列舉了常用 numpy 基本型別。

型別備註

說明bool_ = bool8

8位布林型別

int8 = byte

8位整型

int16 = short

16位整型

int32 = intc

32位整型

int_ = int64 = long = int0 = intp

64位整型

uint8 = ubyte

8位無符號整型

uint16 = ushort

16位無符號整型

uint32 = uintc

32位無符號整型

uint64 = uintp = uint0 = uint

64位無符號整型

float16 = half

16位浮點型

float32 = single

32位浮點型

float_ = float64 = double

64位浮點型

str_ = unicode_ = str0 = unicode

unicode 字串

datetime64

日期時間型別

timedelta64

表示兩個時間之間的間隔

建立資料型別

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

class

dtype

(object):

def__init__

(self, obj, align=

false

, copy=

false):

pass

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

字元對應型別備註b

boolean

『b1』

isigned integer

『i1』, 『i2』, 『i4』, 『i8』

uunsigned integer

『u1』, 『u2』 ,『u4』 ,『u8』

ffloating-point

『f2』, 『f4』, 『f8』

ccomplex

floating-point

mtimedelta64

表示兩個時間之間的間隔

mdatetime64

日期時間型別

oobject

s(byte-)string

s3表示長度為3的字串

uunicode

unicode 字串

vvoid

【例】

import numpy as np

a = np.dtype(

'b1'

)print

(a.type)#

print

(a.itemsize)

# 1a = np.dtype(

'i1'

)print

(a.type)#

print

(a.itemsize)

# 1a = np.dtype(

'i2'

)print

(a.type)#

print

(a.itemsize)

# 2a = np.dtype(

'i4'

)print

(a.type)#

print

(a.itemsize)

# 4a = np.dtype(

'i8'

)print

(a.type)#

print

(a.itemsize)

# 8a = np.dtype(

'u1'

)print

(a.type)#

print

(a.itemsize)

# 1a = np.dtype(

'u2'

)print

(a.type)#

print

(a.itemsize)

# 2a = np.dtype(

'u4'

)print

(a.type)#

print

(a.itemsize)

# 4a = np.dtype(

'u8'

)print

(a.type)#

print

(a.itemsize)

# 8a = np.dtype(

'f2'

)print

(a.type)#

print

(a.itemsize)

# 2a = np.dtype(

'f4'

)print

(a.type)#

print

(a.itemsize)

# 4a = np.dtype(

'f8'

)print

(a.type)#

print

(a.itemsize)

# 8a = np.dtype(

's')

print

(a.type)#

print

(a.itemsize)

# 0a = np.dtype(

's3'

)print

(a.type)#

print

(a.itemsize)

# 3a = np.dtype(

'u3'

)print

(a.type)#

print

(a.itemsize)

# 12

NumPy 資料型別

numpy 支援比 python 更多種類的數值型別。下表顯示了 numpy 中定義的不同標量資料型別。序號資料型別及描述 1.bool 儲存為乙個位元組的布林值 真或假 2.int 預設整數,相當於 c 的long,通常為int32或int64 3.intc相當於 c 的int,通常為int32或...

NumPy 資料型別

numpy提供的數值型別,數值範圍比python提供的數值型別更大。numpy的數值型別,如下表所示 sn資料型別描述1 bool 布林值,取值ture false,占用乙個位元組 2int 是integer的預設型別。與c語言中的long型別相同,有可能是64位或32位。3intc 類似於c語言中...

Numpy資料型別

numpy是python的一種開源的數值計算擴充套件,是一些針對矩陣進行運算的模組。1.numpy介紹 2.numpy 學習筆記 3.python中的list和array的不同之處 4.python列表 numpy陣列與矩陣的區別 1.python中的list和np.array的不同之處 numpy...