欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python学习—scipy知识点

程序员文章站 2022-07-12 22:32:40
...

1.1 integral积分运算

import numpy as np
from scipy.integrate import quad,dblquad,nquad #quad是一元积分,dblquad是二元积分,nquad表示n维的积分
print(quad(lambda x:np.exp(-x),0,np.inf))#在进行一元积分时此处0是x的下界,np.inf是x的上界
print(dblquad(lambda t,x:np.exp(-x*t)/t**3,0,np.inf,lambda x:1,lambda x:np.inf))#在进行二元积分时需要定义两个变量,x的取值范围可以和t有关
def f(x,y):
    return x*y
def bound_y():
    return [0,0.5]
def bound_x(y):
    return [0,1-2*y]
print(nquad(f,[bound_x,bound_y]))#可将自定义的函数代入nquad中进行积分运算
(1.0000000000000002, 5.842607038578007e-11)
(0.3333333333366853, 1.3888461883425516e-08)
(0.010416666666666668, 4.101620128472366e-16)

1.2 优化器 Optimizer

可以自定义拉格朗日乘子法的运算过程,也可以实现凸优化的相关计算

from scipy.optimize import minimize
def rosen(x):
    return np.sum(100.0*(x[1:]-x[:-1]**2.0)**2.0+(1-x[:-1])**2.0)
x0 = np.array([1.3,0.7,0.8,1.9,1.2])
res = minimize(rosen,x0,method = 'nelder-mead',options = {'xtol':1e-8,'disp':True})
print(res)
Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 339
         Function evaluations: 571
 final_simplex: (array([[ 1.        ,  1.        ,  1.        ,  1.        ,  1.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ,  1.        ],
       [ 1.        ,  1.        ,  1.        ,  1.00000001,  1.00000001],
       [ 1.        ,  1.        ,  1.        ,  1.        ,  1.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ,  1.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ,  0.99999999]]), array([  4.86115343e-17,   7.65182843e-17,   8.11395684e-17,
         8.63263255e-17,   8.64080682e-17,   2.17927418e-16]))
           fun: 4.8611534334221152e-17
       message: 'Optimization terminated successfully.'
          nfev: 571
           nit: 339
        status: 0
       success: True
             x: array([ 1.,  1.,  1.,  1.,  1.])

1.3 插值 interpolation

from pylab import * #绘图模块
#from scipy import interpolate
from scipy.interpolate import interp1d
x = np.linspace(0,1,10)
y = np.sin(2*np.pi*x)
li = interpolate.interp1d(x,y,kind = 'cubic')
x_new = np.linspace(0,1,50)
y_new = li(x_new)
plot(x,y,'r')
plot(x_new,y_new,'k')
print(y_new)
show()
[  0.00000000e+00   1.31268478e-01   2.58058896e-01   3.79095818e-01
   4.93103803e-01   5.98807414e-01   6.94931212e-01   7.80199759e-01
   8.53337617e-01   9.13069347e-01   9.58119510e-01   9.87213058e-01
   9.99462682e-01   9.95096409e-01   9.74541587e-01   9.38225564e-01
   8.86575689e-01   8.20078818e-01   7.39913600e-01   6.47705004e-01
   5.45085433e-01   4.33687295e-01   3.15143957e-01   1.91245093e-01
   6.41081509e-02  -6.41081509e-02  -1.91245093e-01  -3.15143957e-01
  -4.33687295e-01  -5.45085433e-01  -6.47705004e-01  -7.39913600e-01
  -8.20078818e-01  -8.86575689e-01  -9.38225564e-01  -9.74541587e-01
  -9.95096409e-01  -9.99462682e-01  -9.87213058e-01  -9.58119510e-01
  -9.13069347e-01  -8.53337617e-01  -7.80199759e-01  -6.94931212e-01
  -5.98807414e-01  -4.93103803e-01  -3.79095818e-01  -2.58058896e-01
  -1.31268478e-01  -2.44929360e-16]
python学习—scipy知识点

1.4 线性计算和矩阵分解

from scipy import linalg as lg
arr = np.array([[1,2],[3,4]])
print(lg.det(arr)) #计算矩阵的行列式
print(lg.inv(arr)) #计算矩阵的逆矩阵
b = np.array([6,14])
print(lg.solve(arr,b))#解线性方程组,此时arr是系数矩阵,b是目标值,可直接代入lg.solve()进行线性方程组的求解
print(lg.eig(arr)) #计算矩阵的特征值和特征向量
print('LU:\n',lg.lu(arr))#进行矩阵分解中的LU分解
print('QR:\n',lg.qr(arr))#进行矩阵分解中的QR分解
print('SVD:\n',lg.svd(arr))#进行矩阵分解中的SVD分解(奇异值分解)
-2.0
[[-2.   1. ]
 [ 1.5 -0.5]]
[ 2.  2.]
(array([-0.37228132+0.j,  5.37228132+0.j]), array([[-0.82456484, -0.41597356],
       [ 0.56576746, -0.90937671]]))
LU:
 (array([[ 0.,  1.],
       [ 1.,  0.]]), array([[ 1.        ,  0.        ],
       [ 0.33333333,  1.        ]]), array([[ 3.        ,  4.        ],
       [ 0.        ,  0.66666667]]))
QR:
 (array([[-0.31622777, -0.9486833 ],
       [-0.9486833 ,  0.31622777]]), array([[-3.16227766, -4.42718872],
       [ 0.        , -0.63245553]]))
SVD:
 (array([[-0.40455358, -0.9145143 ],
       [-0.9145143 ,  0.40455358]]), array([ 5.4649857 ,  0.36596619]), array([[-0.57604844, -0.81741556],
       [ 0.81741556, -0.57604844]]))






相关标签: python scipy