1. 程式人生 > >陣列與矩陣

陣列與矩陣

import numpy as np
#建立隨機陣列
a1=np.random.randn(10)
print(a1)
#建立二維陣列
b1=np.random.randint(10,size=20).reshape(4,5)
print(b1)
#陣列運算
a2=np.random.randint(10,size=20).reshape(4,5)
b2=np.random.randint(10,size=20).reshape(4,5)
print(a2)
print(b2)
print(a2+b2)
print(a2-b2)
print(a2*b2)
#矩陣的運算
A=np.mat(a2)
B=np.mat(b2)
print(A)
print(B)
print(A+B)
print(A-B)
a3=np.mat(np.random.randint(10,size=20).reshape(4,5))
b3=np.mat(np.random.randint(10,size=20).reshape(5,4))
print(a3*b3)
#array常用函式
a4=np.random.randint(10,size=20).reshape(4,5)
print(np.unique(a4))
print(sum(a4))
print(sum(a4[0]))
print(sum(a4[:0]))
#使用pickle序列化numpy array
import pickle
x=np.arange(10)
f=open("x.pki","wb")
pickle.dump(x,f)