# python手写多项式拟合、曲线拟合

python 同时被 2 个专栏收录
41 篇文章 3 订阅
10 篇文章 0 订阅

import numpy as np

def my_fit(x, y, power):
size = len(x)
c = np.ones((1, size))
x_arr = np.array(x)
x_a=x_arr.copy()
x_a.resize((1,size))
x_mat = np.append(x_a, c, axis=0)
y_arr = np.array(y)
y_arr.resize((1, size))
y_mat = np.mat(y_arr)
for i in range(2, power+1):
temp_x = x_arr**i
temp_x.resize((1, size))
x_mat = np.append(temp_x, x_mat, axis=0)
x_mat=np.mat(x_mat)
w = (x_mat * x_mat.T).I * x_mat * y_mat.T
w0=w.T
w0.resize(w0.size)
return w0

def f(x):
return 2*x ** 2 +x+3

x = np.linspace(-5, 5)
y = f(x) + 0.5*np.random.randn(len(x))  # 加入噪音
y_fit = np.polyfit(x, y, 2)  # 二次多项式拟合
print(y_fit)
w=my_fit(x,y,2)
print(w)

[1.99205043 1.00302882 3.08335708]
[1.99205043 1.00302882 3.08335708]

code：

import numpy as np
from scipy.optimize import curve_fit

def f_fit(x, a, b, c):
return a*np.sin(x)+b*x+c

def f_test(x):
return 2*np.sin(x)+3*x+1

def my_curve_fit1(fit_fun, x, y, p_num):  #p_num参数的个数
size = len(x)
if p_num <= 0 or p_num > size:
print('no parameter to fit')
return
test_list = [0]*p_num
test_list[0] = 1
x_arr = np.array(fit_fun(x, *test_list))
x_arr.resize((1, size))
for i in range(1, p_num):
test_list[i-1] = 0
test_list[i] = 1
temp_x = np.array(fit_fun(x, *test_list))
temp_x.resize((1, size))
x_arr = np.append(x_arr, temp_x, axis=0)
x_mat = np.mat(x_arr)
y_arr = np.array(y)
y_arr.resize((1, size))
y_mat = np.mat(y_arr)
w = (x_mat * x_mat.T).I * x_mat * y_mat.T
w0 = w.T
w0.resize(w0.size)
return w0

#为了强行接近库函数的接口,无需参数的个数，但实现有点捞
#很容易知道参数个数多于样本x的个数无法拟合
def my_curve_fit2(fit_fun, x, y):
test_list = [0]
size = len(x)
p_num = 1  #参数个数默认为1
for i in range(0, size):
try:
fit_fun(x, *test_list)
break
except:
p_num += 1
test_list.append(0)
if p_num > size:
print('can not fit')
return
test_list[0] = 1
x_arr = np.array(fit_fun(x, *test_list))
x_arr.resize((1, size))
for i in range(1, p_num):
test_list[i - 1] = 0
test_list[i] = 1
temp_x = np.array(fit_fun(x, *test_list))
temp_x.resize((1, size))
x_arr = np.append(x_arr, temp_x, axis=0)
x_mat = np.mat(x_arr)
y_arr = np.array(y)
y_arr.resize((1, size))
y_mat = np.mat(y_arr)
w = (x_mat * x_mat.T).I * x_mat * y_mat.T
w0 = w.T
w0.resize(w0.size)
return w0

x = np.linspace(-2*np.pi, 2*np.pi)
y = f_test(x)+0.3*np.random.randn(len(x))   #加入噪音
p_fit, prov = curve_fit(f_fit, x, y)  #曲线拟合
my_fit1 = my_curve_fit1(f_fit, x, y, 3)
my_fit2 = my_curve_fit2(f_fit, x, y)
print('sicpy库的曲线拟合')
print('a,b,c', p_fit)
print('手写曲线拟合1')
print('a,b,c', my_fit1)
print('手写曲线拟合2')
print('a,b,c', my_fit2)

sicpy库的曲线拟合
a,b,c [1.99131057 3.00473361 1.05173153]

a,b,c [1.99131057 3.00473361 1.05173153]

a,b,c [1.99131057 3.00473361 1.05173153]

• 1
点赞
• 0
评论
• 14
收藏
• 一键三连
• 扫一扫，分享海报

08-07
05-30

07-30
09-06
01-29 3万+
01-14
02-07 3890
04-19 440
02-24 926