简体   繁体   English

如何使用“scipy.optimize.curve_fit”顺利拟合我的数据点?

[英]How do i get a smooth fit for my data points, using “scipy.optimize.curve_fit”?

I want to fit some data points using scipy.optimize.curve_fit . 我想使用scipy.optimize.curve_fit来拟合一些数据点。 Unfortunately I get an unsteady fit and I do not know why. 不幸的是,我得到一个不稳定的适合,我不知道为什么。

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

M = np.array([730,910,1066,1088,1150], dtype=float)
V = np.array([95.71581923, 146.18564513, 164.46723727, 288.49796413, 370.98703941], dtype=float)

def func(x, a, b, c):
    return a * np.exp(b * x) + c

popt, pcov = curve_fit(func, M, V, [0,0,1], maxfev=100000000)
print(*popt)

fig, ax = plt.subplots()
fig.dpi = 80

ax.plot(M, V, 'go', label='data')
ax.plot(M, func(M, *popt), '-', label='fit')

plt.xlabel("M")
plt.ylabel("V")
plt.grid()
plt.legend()
plt.show()

在此输入图像描述

I would acutally expect some kind of a smooth curve. 我会期望某种平滑的曲线。 Can someone explain what I am doing wrong here? 有人可以解释我在这里做错了什么吗?

You are only plotting the same x points as the original data in your call: 您只是在呼叫中绘制与原始数据相同的x点:

ax.plot(M, V, 'go', label='data')
ax.plot(M, func(M, *popt), '-', label='fit')

To fix this, you can use a wider range - here we use all the values from 700 to 1200: 要解决这个问题,你可以使用更广泛的范围 - 这里我们使用700到1200之间的所有值:

toplot = np.arange(700,1200)
ax.plot(toplot, func(toplot, *popt), '-', label='fit')

光滑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM