scipy 最小二乘法

使用 scipy 优化函数,最小二乘法求函数参数

import scipy.linalg as la
import numpy as np
import matplotlib.pyplot as plt

def LeastSquares(x,y):
    '''
    最小二乘法计算计算
    y = 3*x+2
    '''
    #y = np.log(v) # 实际值
    #x = np.log((np.power(Q,1/3))/R)
    M = x[:,np.newaxis]**[0,1]
    p,res,rnk,s = la.lstsq(M,y)
    print(p)
    return p

if __name__ == '__main__':
    x = np.linspace(1,2,100)
    y_exact = 3*x + 2
    xi = x + np.random.normal(0, 0.05, 100)
    yi = 3*xi + 2 + np.random.normal(0, 0.05, 100)
    p = LeastSquares(xi,yi)

参考:
lstsq
scipy-最小二乘

猜你喜欢

转载自blog.csdn.net/weixin_39107270/article/details/131086715