numpy的包训练:读写文件、常用函数、random随机数模块和 有用的linalg线性代数模块

44 篇文章 0 订阅
18 篇文章 1 订阅
import numpy as np
import matplotlib.pyplot as plt
'''
# 1)numpy readtxt, savetxt 读取和创建txt csv等文件
# 2)numpy 常用的函数操作(都是可对数组迭代的,而内建的len(),abs()等只能单元素操作:绝对值fabs,四舍五入、三角-双曲三角、对数指数
# 3)np.random random模块的随机操作,生成正态、均匀、泊松的数组random.normal .poisson .uniform;生成排列组合,生成随机数shuffle,choice,
#    permutation(和itertool里面的permutation区别,那个纯排列),randn, randint, rand(均匀分布)
# 4) np.linalg模块,求线性方程组、行列式、特征值-向量、svd分解、最小二乘估计
Perfey -- 2020-9-10 21:39
'''

#1 read 鸢尾花
a = np.loadtxt('iris.data',dtype = np.float64, delimiter=',', usecols=[0,1,2,3])
b = np.loadtxt('iris.data', dtype= "S", delimiter = ',',usecols=[4])
print('a:\n',a, '\nb:\n',b)

t = np.dtype([('len1','f'),('wid1','f'),('len2','f'),('wid2','f'),('class',"S20")])
c = np.loadtxt('iris.data', dtype = t, delimiter = ',')
print('All c:\n',c)

print('type:',type(c),type(a),type(b))

sa = np.savetxt('iris.csv', c, fmt = '%3.1f:%3.1f:%3.1f:%3.1f:%s')
print(sa)

#2 np functions:
x = np.array(np.arange(20))
y = np.log(x[1:])
z = np.log10(x[1:])
w = np.log2(x[1:])
x2 = np.multiply(100.1234567,x)
y2 = np.exp(y)
print(x,'\n',y,'\n',z,'\n',w,'\n',x2,'\n',y2)

print('raw:',x2,'\n ceil\n',np.ceil(x2),'\n floor \n',np.floor(x2),'\n rint \n',np.rint(x2),'\n modf \n', np.modf(x2))

x_cos = np.cos(x)
x_sin = np.sin(x)
x_cosh = np.cosh(x)
x_sinh = np.sinh(x)
x_tan = np.tan(x)
x_tanh = np.tanh(x)

plt.figure()
plt.plot(x,x_cos, 'ro-',label='cos()')
plt.plot(x,x_sin,'bs-',label = 'sin()')

plt.legend()
plt.figure()
plt.plot(x,x_cosh, 'gx-', label = 'cosh()')
plt.plot(x,x_sinh, 'k*-', label = 'sinh()')
plt.plot(x,x_tan, 'c+-', label = 'tan()')
plt.plot(x,x_tanh, 'm<-', label='tanh()')
plt.legend()
plt.show()

# 3 random number ,np.random,正态、排列、choice、均匀、poisson
x = np.random.rand(2,3,4) #uniform distribution
print(x)
y = np.random.randn(3,5,2)#normal distribution
print(y)
z = np.random.randint(10,100,(3,4))
print(z)
w = np.arange(24).reshape(3,4,2)
print(w)
np.random.shuffle(w)
print(w)
m = np.random.permutation(w)
print(m)
n = np.random.uniform(3,3000,(2,4))
print(n)
l = np.random.poisson(10,(2,3,4))
print(l)

# 4 linalg模块,求逆,求QR分解,求行列式
from numpy.linalg import qr
x = np.array([[0,3,2], [1,4,-1],[3,5,100]])
b = [2,3,4]
q,r = qr(x)
print('q matrix(orthorgonal):\n',q)
print('r matrix(upper triangle):\n',r)
print('x-q*r\n',x-np.matmul(q,r), '\n clean residual:\n', np.around(x-np.matmul(q,r),3))
#inverse
x_inv = np.linalg.inv(x)
print('inv matrix:\n',x_inv)
print('x*x_inv:\n',np.around(np.matmul(x,x_inv)))
#det,eig,svd 行列式,本征值,奇异值
x_d = np.linalg.det(x)
x_eig = np.linalg.eig(x)
x_svd = np.linalg.svd(x)
print('det\n',x_d)
print('eig\n',x_eig)
print('svd\n',x_svd)

s,v,d = np.linalg.svd(x)
# v2 = np.eye(3,3)
# v2[0,0],v2[1,1],v2[2,2] = v
v2 = np.diag(v)  #np.diag是将传入数组,排布在新生成矩阵的主对角线上
print('v\n', v2)
x_recover = np.matmul(s,v2, np.transpose(d))
x_r2 = (s*v)@d  #@操作相对于矩阵乘法,np.matmul
print('svd recover\n',np.around(x-x_recover,2),'\n', x_recover,'\n',np.around(x-x_r2,2))  #svd复原失败了;
#solve
p = np.linalg.solve(x,b)
print('Solution of Equation x*p = b\n',p)
print('x*p == b\n', x@p-b)

#lstsq
p2, _, *_ = np.linalg.lstsq(x,b)
print('ls solution of equation \n',p2)
print('xp2 == b\n', x@p2-b)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值