python - 误差非线性回归 python 曲线拟合
问题描述
大家好,我想用曲线拟合在 python 中进行非线性回归,这是我的代码:
#fit a fourth degree polynomial to the economic data
from numpy import arange
from scipy.optimize import curve_fit
from matplotlib import pyplot
import math
x = [17.47,20.71,21.08,18.08,17.12,14.16,14.06,12.44,11.86,11.19,10.65]
y = [5,35,65,95,125,155,185,215,245,275,305]
# define the true objective function
def objective(x, a, b, c, d, e):
return ((a)-((b)*(x/3-5)))+((c)*(x/305)**2)-((d)*(math.log(305))-math.log(x))+((e)*(math.log(305)-(math.log(x))**2))
popt, _ = curve_fit(objective, x, y)
# summarize the parameter values
a, b, c, d, e = popt
# plot input vs output
pyplot.scatter(x, y)
# define a sequence of inputs between the smallest and largest known inputs
x_line = arange(min(x), max(x), 1)
# calculate the output for the range
y_line = objective(x_line, a, b, c, d, e)
# create a line plot for the mapping function
pyplot.plot(x_line, y_line, '--', color='red')
pyplot.show()
这是我的错误:
回溯(最近一次调用最后一次):文件“C:\Users\Fahmi\PycharmProjects\pythonProject\main.py”,第 16 行,在 popt 中,_ = curve_fit(objective, x, y) 文件“C:\Users\Fahmi \PycharmProjects\pythonProject\venv\lib\site-packages\scipy\optimize\minpack.py",第 784 行,在 curve_fit res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs) 文件“C :\Users\Fahmi\PycharmProjects\pythonProject\venv\lib\site-packages\scipy\optimize\minpack.py",第 410 行,最小平方形状,dtype = _check_func('leastsq', 'func', func, x0, args, n) 文件“C:\Users\Fahmi\PycharmProjects\pythonProject\venv\lib\site-packages\scipy\optimize\minpack.py”,第 24 行,在 _check_func res = atleast_1d(thefunc(((x0[:numinputs],) + args))) 文件“C:\Users\Fahmi\PycharmProjects\pythonProject\venv\lib\site-packages\scipy\optimize\minpack.py”,第 484 行,在 func_wrapped 返回func(xdata, params) - ydata 文件“C:\Users\Fahmi\PycharmProjects\pythonProject\main.py”,第 13 行,客观返回 ((a)-((b) (x/3-5))) +((c) (x/305)**2)-((d) (math.log(305))-math.log(x))+((e) (math.log(305)-(math .log(x))**2)) 类型错误:只有 size-1 数组可以转换为 Python 标量
谢谢之前
解决方案
这是数学库的一个已知问题。只需使用 numpy 即可解决您的问题,因为 numpy 函数支持标量和数组。
#fit a fourth degree polynomial to the economic data
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
x = [17.47,20.71,21.08,18.08,17.12,14.16,14.06,12.44,11.86,11.19,10.65]
y = [5,35,65,95,125,155,185,215,245,275,305]
# define the true objective function
def objective(x, a, b, c, d, e):
return ((a)-((b)*(x/3-5)))+((c)*(x/305)**2)-((d)*(np.log(305))-np.log(x))+((e)*(np.log(305)-(np.log(x))**2))
popt, _ = curve_fit(objective, x, y)
# summarize the parameter values
a, b, c, d, e = popt
# plot input vs output
plt.scatter(x, y)
# define a sequence of inputs between the smallest and largest known inputs
x_line = np.arange(np.min(x), np.max(x), 1)
# calculate the output for the range
y_line = objective(x_line, a, b, c, d, e)
# create a line plot for the mapping function
plt.plot(x_line, y_line, '--', color='red')
plt.show()
推荐阅读
- arrays - 如何对工作表数组进行排序 vba
- javascript - 在 div 中添加/删除活动类 scrollspy 导航并溢出
- php - 致命错误:未捕获的错误:类 PDOStatement 的对象无法转换为字符串
- javascript - 如何从 FireReader Promesse 中返回值
- android-camera - 如何裁剪 Camera 2 API ImageReader 图像
- java - 是什么让 Chrome 两次请求 X509 客户端证书
- thonny - Raspberry Pi Pico 不断弹出
- unix - 如何使用 gsutil 命令列出 gcs 存储桶中的所有文件(包含所有文件夹)以匹配文件内容中的特定字符串
- javascript - 触发时开始动画
- javascript - Typescript 的声明合并使用 ts-node 无法按预期工作