菜鳥教程學習-Numpy

1.Numpy的學習

1.1ndarray對象

用于存放同類型元素的多維數(shù)組

import numpy as np

a = np.array([1, 2, 3])

print(a)

[1 2 3]


使數(shù)組多余一個維度

import numpy as np

b = np.array([[1,2],[3,4]])

print(b)

[[1 2]

?[34]]


設(shè)置數(shù)組的最小維度

c = np.array([1,2,3,4],ndmin=2)

print(c)

[[1 2 3 4]]


2.Numpy數(shù)據(jù)類型

使用標量類型

import numpy as np

dt = np.dtype(np.int32)

print(dt)

int32


數(shù)據(jù)類型用字符串代替(int8 對應(yīng)i1,int16對應(yīng)i2,……)

import numpy as np

dt = np.dtype(

'i4')

print(dt)

int32



字節(jié)順序的標注

import numpy as np

dt = np.dtype(

'<i4')

print(dt)

int32


結(jié)構(gòu)化數(shù)據(jù)類型的創(chuàng)建:類型字段與對應(yīng)的實際類型

import numpy as np

dt = np.dtype([(

'age',np.int8)])

print(dt)

[('age', 'i1')]


將數(shù)據(jù)類型應(yīng)用于ndarray對象

import numpy as np

dt = np.dtype([(

'age', np.int8)])

a = np.array([(

10,), (20,), (30,), ], dtype=dt)

print(a)

[(10,) (20,) (30,)]


類型字段名可用于存取實際的age列

import numpy as np

dt = np.dtype([(

'age',np.int8)])

a =np.array([(

10,),(20,),(30),],dtype = dt)

print(a['age'])


定義結(jié)構(gòu)化數(shù)據(jù)類型student,包含字符串字段name,整型字段age和浮點字段marks,將這個數(shù)據(jù)類型應(yīng)用到對象student

import numpy as np

student = np.dtype([(

'name', 'S20'), ('age', 'i1'), ('marks', 'f4')])

print(student)

[('name', 'S20'), ('age', 'i1'), ('marks','


將定義的數(shù)據(jù)類型應(yīng)用到對象中

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(student)

[(b'abc', 21, 50.) (b'xyz', 18, 75.)][if !supportAnnotations][a1][endif]?


每個內(nèi)建類型有其唯一對應(yīng)的字符代碼,如b表示布爾型,i表示整型,f表示浮點型。





[if !supportAnnotations]

[endif]

[if !supportAnnotations]

[endif][if !supportAnnotations][endif]

[if !supportAnnotations][a1][endif]顯示錯誤,b是什么?同時為什么沒有0?

[if !supportAnnotations]

[endif]

?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容