1.numpy 數(shù)據(jù)類型
bool:布爾類型,1 個(gè)字節(jié),值為 True 或 False施蜜。
int:整數(shù)類型,通常為 int64 或 int32 雌隅。
intc:與 C 里的 int 相同翻默,通常為 int32 或 int64。
intp:用于索引恰起,通常為 int32 或 int64修械。
int8:字節(jié)(從 -arange128 到 127)
int16:整數(shù)(從 -32768 到 32767)
int32:整數(shù)(從 -2147483648 到 2147483647)
int64:整數(shù)(從 -9223372036854775808 到 9223372036854775807)
uint8:無(wú)符號(hào)整數(shù)(從 0 到 255)
uint16:無(wú)符號(hào)整數(shù)(從 0 到 65535)
uint32:無(wú)符號(hào)整數(shù)(從 0 到 4294967295)
uint64:無(wú)符號(hào)整數(shù)(從 0 到 18446744073709551615)
float:float64 的簡(jiǎn)寫(xiě)。
float16:半精度浮點(diǎn)检盼,5 位指數(shù)肯污,10 位尾數(shù)
float32:?jiǎn)尉雀↑c(diǎn),8 位指數(shù)吨枉,23 位尾數(shù)
float64:雙精度浮點(diǎn)蹦渣,11 位指數(shù),52 位尾數(shù)
complex:complex128 的簡(jiǎn)寫(xiě)貌亭。
complex64:復(fù)數(shù)柬唯,由兩個(gè) 32 位浮點(diǎn)表示。
complex128:復(fù)數(shù)圃庭,由兩個(gè) 64 位浮點(diǎn)表示锄奢。
可以在創(chuàng)建arrray時(shí)失晴,使用dtype 來(lái)指定它的數(shù)據(jù)類型, 對(duì)array 對(duì)象使用dtype 查看數(shù)據(jù)類型
2. ndarry(N維數(shù)組對(duì)象)?
創(chuàng)建
使用array() 函數(shù), 可以使用dtype 來(lái)指定類型
zeros() 函數(shù)來(lái)生成, 元素均為0 的數(shù)組
ones() 函數(shù)生成元素都為0 的數(shù)組
arange(start, stop,step,dtype) 函數(shù)來(lái)創(chuàng)建
n>>> np.arange(0,12).reshape(3,4)
array([[ 0,? 1,? 2,? 3],
[ 4,? 5,? 6,? 7],
[ 8,? 9, 10, 11]])
linspace(start, end, num) num: 生成的樣本數(shù),也就是指定由開(kāi)頭到結(jié)尾所指定的范圍分成幾個(gè)部分
>>> np.linspace(0,12,3)
array([? 0.,? 6.,? 12.])
也可以使用random.random(shape) 來(lái)用隨機(jī)數(shù)填充數(shù)組
>>> np.random.random((3,4))
array([[ 0.42793958,? 0.7801106 ,? 0.40430299,? 0.2440989 ],
[ 0.12988871,? 0.01634131,? 0.62752135,? 0.99287802],
[ 0.95552834,? 0.60075408,? 0.48059998,? 0.6966276 ]])
ndarry 數(shù)組的屬性
ndim 秩的數(shù)量
size 數(shù)組的大小
shape 數(shù)組的型
3.基本操作
重新設(shè)置形狀
>>> np.arange(0,12).reshape((3,4))
array([[ 0,? 1,? 2,? 3],
[ 4,? 5,? 6,? 7],
[ 8,? 9, 10, 11]])
算術(shù)運(yùn)算
直接使用算術(shù)運(yùn)算符進(jìn)行運(yùn)算
矩陣積: dot(A,B) 或A.dot(B)拘央, 矩陣不遵循交換律
通用函數(shù): sqrt(), log() , sin(), ...
聚合函數(shù)
min()
max()
sum()
mean() 平均數(shù)
std()
索引
跟列表有些類似
>>> a=np.arange(20).reshape(4,5)
>>> a
array([[ 0,? 1,? 2,? 3,? 4],
[ 5,? 6,? 7,? 8,? 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
>>> a[[3,4],[3,4]]
實(shí)際獲取的是[1,3]涂屁,也就是第2行和第4列對(duì)于的值8。以及[2, 4]灰伟,也就是第3行和第5列對(duì)于的值14拆又。
三維數(shù)組
>>>importnumpyasnp>>>a = np.arange(30).reshape(2,5,3)>>>a
array([[[0,1,2],
[3,4,5],
[6,7,8],
[9,10,11],
[12,13,14]],
[[15,16,17],
[18,19,20],
[21,22,23],
[24,25,26],
[27,28,29]]])# 索引>>>a[[0,1],[1,2],[1,2]]
array([4,23])
這里,[0,1]分布代表axis = 0和axis = 1栏账。而遏乔,后面的[1,2],[1,2]分別選擇了第2行第2列和第3行第3列的兩個(gè)數(shù)。
切片
對(duì)于多維數(shù)組发笔,我們只需要用逗號(hào),分割不同維度即可:
>>>a = np.arange(20).reshape(4,5)>>>a
array([[0,1,2,3,4],
[5,6,7,8,9],
[10,11,12,13,14],
[15,16,17,18,19]])# 先取第 3,4 列(第一個(gè)維度)凉翻,再取第 1了讨,2,3 行(第二個(gè)維度)制轰。>>>a[0:3,2:4]
array([[2,3],
[7,8],
[12,13]])# 按步長(zhǎng)為 2 取所有列和所有行的數(shù)據(jù)前计。>>>a[:,::2]
array([[0,2,4],
[5,7,9],
[10,12,14],
[15,17,19]])
數(shù)組迭代
遍歷矩陣的每個(gè)元素
for item in A:
? ? ? print item?
apply_along_axis() 函數(shù), axis =0, 時(shí)垃杖,為沿行男杈,=1時(shí),沿列
>>> np.apply_along_axis(np.mean, axis=0, arr=A)
array([ 0.31892036,? 0.32059507,? 0.5275357 ,? 0.56294685])
>>> np.apply_along_axis(np.mean, axis=1, arr=A)
array([ 0.34725639,? 0.52172109,? 0.428521? ])
>>> A
array([[ 0.37854916,? 0.07013463,? 0.16762009,? 0.77272168],
[ 0.39064676,? 0.79229944,? 0.57201198,? 0.33192621],
[ 0.18756517,? 0.09935115,? 0.84297504,? 0.58419266]])
條件和布爾數(shù)組
>>> A = np.random.random((4,4))
>>> A
array([[ 0.73033594,? 0.95318457,? 0.05115813,? 0.1413278 ],
[ 0.38830897,? 0.67513952,? 0.73892551,? 0.00472255],
[ 0.52383481,? 0.35105734,? 0.61480131,? 0.00726216],
[ 0.28354593,? 0.15196966,? 0.86462789,? 0.86654137]])
>>> A<0.5
array([[False, False,? True,? True],
[ True, False, False,? True],
[False,? True, False,? True],
[ True,? True, False, False]], dtype=bool)
>>> A[A<0.5]
array([ 0.05115813,? 0.1413278 ,? 0.38830897,? 0.00472255,? 0.35105734,
0.00726216,? 0.28354593,? 0.15196966])
形狀變換
reshape() 函數(shù)
ravel () 函數(shù)將二維數(shù)組變成一維數(shù)組
transpose() 交換行列
數(shù)組操作
vstack() 垂直入棧操作
hstack() 水平入棧操作
數(shù)組切分
hsplit() 水平分割
vsplit() 垂直分割
>>> A = np.arange(0,12).reshape(3,4)
>>> A
array([[ 0,? 1,? 2,? 3],
[ 4,? 5,? 6,? 7],
[ 8,? 9, 10, 11]])
>>> [b,c]= np.hsplit(A,2)
>>> b
array([[0, 1],
[4, 5],
[8, 9]])
>>> c
array([[ 2,? 3],
[ 6,? 7],
[10, 11]])
也可以指定axis 沿哪條軸分割
對(duì)象的副本
直接使用copy() 函數(shù)
>>> a
array([[ 0.93582073,? 0.36285637,? 0.1743055 ,? 0.73572584],
[ 0.57430482,? 0.86607796,? 0.73776101,? 0.12266299],
[ 0.59001065,? 0.38222194,? 0.06113438,? 0.01316895]])
>>> b=a.copy()
>>> b
array([[ 0.93582073,? 0.36285637,? 0.1743055 ,? 0.73572584],
[ 0.57430482,? 0.86607796,? 0.73776101,? 0.12266299],
[ 0.59001065,? 0.38222194,? 0.06113438,? 0.01316895]])
結(jié)構(gòu)化數(shù)組
創(chuàng)建一個(gè)簡(jiǎn)單的結(jié)構(gòu)化數(shù)組
>>> structured = np.array([(1,'first', 0.5, 1+2j), (2, 'second', 1.5, 2+2j)], dtype=('int16,a6,float32, complex64'))>>> structuredarray([(1, 'first',? 0.5,? 1.+2.j), (2, 'second',? 1.5,? 2.+2.j)],? ? ? dtype=[('f0', '>> structured[1]
(2, 'second',? 1.5,? 2.+2.j)
數(shù)據(jù)文件讀入
保存
>>> a
array([[ 0.93582073,? 0.36285637,? 0.1743055 ,? 0.73572584],
[ 0.57430482,? 0.86607796,? 0.73776101,? 0.12266299],
[ 0.59001065,? 0.38222194,? 0.06113438,? 0.01316895]])
>>> np.save('a_data',a)
np.load('a_data.npy‘)
讀取文件中列表形式
CSV
np.getfromtxt('data.csv', delimiter=',', names=True) , (打開(kāi)的文件名调俘, 分隔符, 是否保存列標(biāo)題) ,內(nèi)容為空的項(xiàng)填充為nan 值