错误非线性回归python曲线拟合

法赫米·萨比拉·迪努拉克(Fahmi Sabila Dinnulhaq)

大家好,我想用曲线拟合在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 = minimumsq(func,p0,Dfun = jac,full_output = 1,** kwargs)文件” C :\ Users \ Fahmi \ PycharmProjects \ pythonProject \ venv \ lib \ site-packages \ scipy \ optimize \ minpack.py”,第410行,infinitesq形状,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))TypeError:只能将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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从非线性回归拟合功率曲线?

来自分类Dev

用线性/非线性回归拟合两条曲线

来自分类Dev

R中的非线性曲线拟合

来自分类Dev

非线性回归

来自分类Dev

确定R中非线性回归的拟合方程

来自分类Dev

2条曲线同时非线性回归

来自分类Dev

Matlab中的n维非线性曲线拟合

来自分类Dev

比较非线性回归模型

来自分类Dev

在R中绘制非线性回归

来自分类Dev

Fminsearch Matlab(非线性回归)

来自分类Dev

GEKKO多元非线性回归

来自分类Dev

神经网络的非线性回归

来自分类Dev

R中的非线性最小二乘曲线拟合

来自分类Dev

在GSL中,是任何用于非线性曲线拟合的函数

来自分类Dev

在 esttab 中结合线性和非线性回归的输出

来自分类Dev

如何在 Minitab 中运行非线性回归宏(简单的语法错误)?

来自分类Dev

Python移动视野时间序列非线性回归中的不良数据

来自分类Dev

非线性拟合正弦曲线失败

来自分类Dev

用ggplot绘制非线性回归列表

来自分类Dev

Keras序列模型非线性回归模型不良预测

来自分类Dev

多数据集的R中的非线性回归

来自分类Dev

使用PyMC(2)进行稳健的非线性回归

来自分类Dev

非线性回归误差(单梯度矩阵)

来自分类Dev

为R中的非线性回归找到合适的公式

来自分类Dev

如何对我的数据执行非线性回归

来自分类Dev

最大化R中的非线性回归函数

来自分类Dev

为什么在 scipy 中优化曲线拟合为这个非线性模型创建一条直线?

来自分类Dev

通过稍微更改我为线性回归编写的代码来获得曲线拟合的二阶多项式

来自分类Dev

我是否正确编写曲线拟合模型的代码?除线性回归外,所有模型的直线都偏离

Related 相关文章

热门标签

归档