數(shù)組的創(chuàng)建和查詢
import numpy as np
# create list from python
list_1 = [1, 2, 3, 4]
list_1
[1, 2, 3, 4]
array_1 = np.array(list_1)
array_1
array([1, 2, 3, 4])
list_2 = [5, 6, 7, 8]
array_2 = np.array([list_1, list_2])
array_2 # 二維數(shù)組
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
array_2.shape # 數(shù)組的類型 2行4列的數(shù)組
(2, 4)
array_2.size # 返回?cái)?shù)組的元素個(gè)數(shù)
8
array_2.dtype # 返回?cái)?shù)組元素的類型股毫,或者精確度最高的數(shù)據(jù)類型
dtype('int64')
array_3 = np.array([[1.0, 2, 3], [4.0, 5, 6]])
array_3.dtype # 返回?cái)?shù)組元素的類型,或者精確度最高的數(shù)據(jù)類型
dtype('float64')
array_4 = np.arange(1, 10, 2)
array_4
array([1, 3, 5, 7, 9])
np.zeros(4)
array([0., 0., 0., 0.])
np.zeros([2, 3]) # 全0矩陣
array([[0., 0., 0.],
[0., 0., 0.]])
np.eye(5) # 單位矩陣
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
np.eye(5).dtype
dtype('float64')
b = np.array([[1, 2, 3], [4, 5, 6]])
b[1][0] # 訪問(wèn)矩陣的元素
4
c = np.array([[1, 2, 3], [4, 5, 6], [8, 9 ,10]])
c
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 8, 9, 10]])
c[:2, 1:] # :2代表行陨倡,既0和1行骑疆,1:代表列田篇,既1列到最后一列
array([[2, 3],
[5, 6]])
數(shù)據(jù)與矩陣運(yùn)算
import numpy as np
np.random.randn(10) # 創(chuàng)建一維N個(gè)元素的數(shù)組替废,參數(shù)正態(tài)分布
array([ 1.59437623, 0.82730971, 0.47005237, -0.98136635, 1.5803171 ,
-0.7388171 , 0.44446894, -0.73766478, -1.18912274, -0.75089731])
np.random.randint(10, size=(2, 3)) # 得到一個(gè)2行3列的二維數(shù)組 ,元素0~9隨機(jī)整數(shù)
array([[8, 8, 7],
[0, 5, 0]])
np.random.randint(10, size = 20) # 創(chuàng)建一維數(shù)組,20個(gè)元素
array([5, 9, 0, 2, 8, 2, 9, 2, 0, 1, 7, 1, 9, 9, 2, 0, 5, 5, 3, 9])
np.random.randint(10, size = 20).reshape(4, 5) # 重塑成一個(gè)二維數(shù)組4行5列
array([[5, 8, 5, 4, 2],
[2, 1, 5, 1, 5],
[3, 5, 5, 7, 6],
[9, 2, 8, 3, 8]])
a = np.random.randint(10, size = 20).reshape(4, 5)
b = np.random.randint(10, size = 20).reshape(4, 5)
a
array([[6, 0, 9, 7, 1],
[5, 9, 4, 1, 4],
[2, 6, 1, 2, 0],
[8, 2, 3, 5, 8]])
b
array([[5, 8, 3, 5, 1],
[2, 4, 7, 0, 4],
[9, 9, 9, 3, 3],
[8, 5, 4, 3, 8]])
a + b # 數(shù)組的加法泊柬,相同行列的數(shù)組對(duì)應(yīng)位置元素的相加椎镣、減
array([[11, 8, 12, 12, 2],
[ 7, 13, 11, 1, 8],
[11, 15, 10, 5, 3],
[16, 7, 7, 8, 16]])
a * b # 數(shù)組的乘法,相同行列的數(shù)組對(duì)應(yīng)位置元素的乘兽赁、除
array([[30, 0, 27, 35, 1],
[10, 36, 28, 0, 16],
[18, 54, 9, 6, 0],
[64, 10, 12, 15, 64]])
np.mat([[1, 2, 3], [4, 5, 6]]) # 創(chuàng)建二維矩陣
matrix([[1, 2, 3],
[4, 5, 6]])
a # 數(shù)組
array([[6, 0, 9, 7, 1],
[5, 9, 4, 1, 4],
[2, 6, 1, 2, 0],
[8, 2, 3, 5, 8]])
np.mat(a) # 數(shù)組轉(zhuǎn)換為矩陣
matrix([[6, 0, 9, 7, 1],
[5, 9, 4, 1, 4],
[2, 6, 1, 2, 0],
[8, 2, 3, 5, 8]])
A = np.mat(a)
B = np.mat(b)
A
matrix([[6, 0, 9, 7, 1],
[5, 9, 4, 1, 4],
[2, 6, 1, 2, 0],
[8, 2, 3, 5, 8]])
B
matrix([[5, 8, 3, 5, 1],
[2, 4, 7, 0, 4],
[9, 9, 9, 3, 3],
[8, 5, 4, 3, 8]])
A + B # 矩陣的相加状答, 對(duì)應(yīng)位置元素的相加、減
matrix([[11, 8, 12, 12, 2],
[ 7, 13, 11, 1, 8],
[11, 15, 10, 5, 3],
[16, 7, 7, 8, 16]])
# A矩陣和B矩陣相乘闸氮,A矩陣的行數(shù)必須和B矩陣的列數(shù)相等才可以
a = np.mat(np.random.randint(10, size = 20).reshape(4, 5)) # 4行 5列
b = np.mat(np.random.randint(10, size = 20).reshape(5, 4)) # 5行 4列
a
matrix([[5, 4, 0, 8, 5],
[6, 4, 9, 3, 8],
[0, 4, 9, 0, 2],
[8, 0, 4, 0, 0]])
b
matrix([[6, 8, 0, 6],
[4, 7, 3, 9],
[5, 5, 1, 0],
[3, 6, 9, 9],
[7, 2, 3, 0]])
a * b # 生成一個(gè)4*4的矩陣: 4行4列
matrix([[105, 126, 99, 138],
[162, 155, 72, 99],
[ 75, 77, 27, 36],
[ 68, 84, 4, 48]])
a = np.random.randint(10, size = 20).reshape(4, 5) # 4行 5列 的數(shù)組
a
array([[2, 5, 4, 4, 8],
[1, 3, 0, 4, 6],
[1, 1, 6, 6, 2],
[3, 6, 1, 9, 7]])
np.unique(a) # 返回?cái)?shù)組去重后的所有元素剪况。類似set(list)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
sum(a) # 返回array每個(gè)列的和值
array([ 7, 15, 11, 23, 23])
sum(a[0]) # 計(jì)算第0行元素的和
23
sum(a[:, 2:]) # 計(jì)算所有行的第3列以后教沾,每列的和
array([11, 23, 23])
a.max() # 整個(gè)array的最大值
9
max(a[1:, 1]) # 返回第二行起蒲跨,第二列的最大值
6
使用pickle序列化numpy array
import pickle
import numpy as np
x = np.arange(10) # 創(chuàng)建一個(gè)10個(gè)元素的一維array
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
f = open('x.pkl', 'wb') # 打開(kāi)文件x.pkl,準(zhǔn)備以二進(jìn)制形式寫入數(shù)據(jù)
pickle.dump(x, f) # x對(duì)象寫入f文件
f.close()
f = open('x.pkl','rb') # 打開(kāi)文件授翻,準(zhǔn)備以二進(jìn)制讀取數(shù)據(jù)
y = pickle.load(f)
y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.save('one_array', x) # 通過(guò)numpy的save方法或悲,把x序列化到文件one_array.npy文件
np.load('one_array.npy') # 通過(guò)numpy的load方法,把文件one_array.npy的內(nèi)容反序列化出來(lái)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
z = np.arange(20)
z
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
np.savez('two_array.npz', a=x, b=z) # 序列化多個(gè)對(duì)象到一個(gè)文件
c = np.load('two_array.npz') # 反序列化到變量c, 然后取出: a 堪唐、b
c['a']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
c['b']
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
type(c)
numpy.lib.npyio.NpzFile
查看numpy manual