# 数组运算
a = np.random.randint(10, size =(4,5))
b = np.random.randint(10, size =(4,5))print( a * b)# 加print( a + b)# 乘法# 创建矩阵
s = np.mat([[1,2,3],[1,2,3]])
sa = np.mat(a)
sb = np.mat(b)# 矩阵的运算# print( sa * sb) # 运行不通过, rows not equals columsprint( sa + sb)
常用函数
a = np.random.randint(10, size =30).reshape(5,6)
np.unique(a)# it can find unique elementsum(a)# 列和sum(a[0])# 0 row sum sum(a[:,0])# 0 colum sum
a.max()# the max of the matrixmax(a[0])# the max of 0 rowmax(a[:,0])# the max of 0 colum
使用pickle序列化numpy array
import pickle
import numpy as np
arr = np.arange(10)# 创建文件
f =open('x.pkl','wb')# 写入文件
pickle.dump(arr, f)# 打开文件
f =open('x.pkl','rb')# 读取文件
a1 = pickle.load(f)# 序列化文件
np.save('one_array', arr)# 读取文件
np.load('one_array.npy')# 多个 arr 压缩大一个 npz 文件中
x = np.arange(10)
y = np.arange(20)
np.savez('two_array', a = x,b = y)# 加载这个文件
arrs = np.load('two_array.npz')print(dict(arrs))print(arrs['a'])