python - scipy.optimize.curve_fit 产生无意义的曲线拟合

标签 python machine-learning scipy statistics curve-fitting

我正在尝试将曲线拟合到一些生成的数据,这些数据在绘制时类似于指数函数。我正在使用 scipy.optimize.curve_fit,因为它似乎是这项工作的最佳选择(也是最好的记录)。每次运行代码时,实际数据都是新生成的,但这里是一个示例集:

import pandas
import scipy.optimize as opt

x1 = [0.4145392937447818, 0.7807888116968482, 0.7903528929788539, 
1.5081613036989836, -0.295895237606155, -0.0855307279546107, 
1.0523973736479486, -0.6967509832843239, -0.30499200990688413, 
1.1990545631966807, -1.270460772249312, 0.9531042718153095, 1.5747175535222993, 
-0.6483709650867473, 0.47820180254528477, 1.14266851615097, 0.6237953640100202, 
0.0664027559951128, 0.877280002485417, 0.9432317053343211, 1.0367424879878504, 
-0.6410400513164749, 1.667835241401498, -0.20484029870424125, 
2.887026948755316]

y1 = [0.718716626591187, 0.579938466590508, 0.722005637974309, 
1.61842778379047, 0.331301712743162, 0.342649242449043, 1.14950611092907, 
0.299221762023701, 0.345063839940754, 1.08398125906313, 0.315433168226251, 
1.3343730617376, 1.32514210008176, 0.308702648499771, 0.495749985226691, 
0.406025683910759, 0.445087968405107, 0.423578575247177, 0.816264419038205, 
1.16110461165631, 1.81572974380867, 0.420890068255763, 0.821468286117842, 
0.416275933630732, 4.7877353794036]

data = pandas.DataFrame({"Pi_values": x1, 
                         "CO2_at_solubility": y1})

然后,我做曲线拟合业务...

##Define curve fitting
def func(x, m, c, c0):
    return c0 + m**x * c

#draw the figure
fig, ax1 = plt.subplots()
plt.xlabel('Pi Parameter')
plt.ylabel('CO2 wt%')

#plot generated data
#tried converting pandas columns to np arrays based on an issue another user was having, but it does not help
x1 = data["Pi_values"].values
y1 = data["CO2_at_solubility"].values

# Curve fitting with scipy.optimize.curve_fit
popt, pcov = opt.curve_fit(func, x1, y1)
# Use the optimized parameters to plot the best fit
plt.plot(x1, y1, 'o', x1, func(x1, *popt))

这是非常奇怪的结果。无论我尝试什么形式的方程式,如果它能够拟合任何“曲线”,它看起来就像这样一团糟:

Click here to see the plot

或者这个烂摊子...

Click here to see another plot

知道这里会发生什么吗?我找不到任何其他类似的例子。我在 jupyter notebook 中运行 python3.5。

我尝试过但没有用的其他方法:等式的其他形式;其他方程;改变初始猜测值;在 y 值太小的情况下缩放值。

最佳答案

你只需要对 x 值进行排序

data.sort_values(by='Pi_values', ascending=True, inplace=True)

curve_fit 之前:

x1 = data["Pi_values"].values
y1 = data["CO2_at_solubility"].values
# Curve fitting with scipy.optimize.curve_fit
popt, pcov = opt.curve_fit(func, x1, y1)
# Use the optimized parameters to plot the best fit
plt.plot(x1, y1, 'o', x1, func(x1, *popt))

enter image description here

关于python - scipy.optimize.curve_fit 产生无意义的曲线拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52269680/

相关文章:

python - 如何将 Pandas DataFrame 转换为 Pandas ML ModelFrame?

python - Scipy 的 convolve2d 显着增加数组中的值

python - 如何在带边界的 python 优化中找到全局最小值?

python - 在 nexus 上创建和使用 PyPi 代理存储库

python - 如何强制 pandas DataFrame 在构造时使用所需的数据类型?

machine-learning - Keras 保存的模型预测不同 session 上的不同值

machine-learning - 训练 Faster R-CNN 时如何固定共享卷积层

python - 直接打开/加载图像作为 numpy ndarray

python - 极坐标/等值线图 : How to plot some curves inside a circle?

python - 在字符串python中找到一个模式