Python 曲线拟合与数据预处理

Python 曲线拟合与数据预处理,python,scipy,Python,Scipy,我试图用曲线拟合一些随时间变化的数据(x=时间,y=数据),但在计算要使用的函数以及如何/如果需要清理数据时遇到了一些困难 这是我的代码: import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit #define xdata dnd ydata ydata = [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4,

我试图用曲线拟合一些随时间变化的数据(x=时间,y=数据),但在计算要使用的函数以及如何/如果需要清理数据时遇到了一些困难

这是我的代码:

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

#define xdata dnd ydata

ydata = [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 12]
ydata_1 = [2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4]
ydata_2 = [1, 3, 7, 7, 10, 12, 12, 13, 13, 11, 11, 11, 12, 14, 15, 15, 15, 15, 16, 16, 16, 17, 16, 18, 18, 19, 19, 19, 19, 19, 19, 20, 20, 20, 21, 22, 22, 22, 23, 23, 24, 26, 25, 25, 25, 26, 26, 26, 26, 27, 27]
xdata = []
for i, _ in enumerate(ydata):
    xdata.append(i+1)

x = np.array(xdata)
y = np.array(ydata)

def func(xdata, a, b, c, d):
    t = 60
    return (a * xdata * np.exp(1 - (b / (t - c)**d)))

xfine = np.linspace(y.min(), y.max(), 100)

popt, pcov = curve_fit(func, x, y)


fig = plt.figure()
ax4 = fig.add_subplot(1,1,1)
ax4.plot(x, y, '.')
ax4.plot(xfine, func(xfine, popt[0], popt[1], popt[2], popt[3]),'r-')
ax4.set_xlabel('Time')
ax4.set_ylabel('Score')
ax4.grid('on')

plt.show()

正如你所看到的,我的数据随着时间的推移是非常重复的,而且函数到目前为止还不能真正工作。我真的不知道我做错了什么以及如何修复它。

您正在将线性函数拟合到ydata中。如果要拟合指数型,请尝试以下方法:

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

#define xdata dnd ydata

ydata = [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 
        3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 
        7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 
        10, 10, 10, 10, 10, 11, 12]
y = np.array(ydata)
x = np.arange(1,len(y)+1,1)

def func(xdata, a, b, c):
    return (a*np.power(b, xdata)) + c
    #or return (a*np.exp(-b*xdata))

xfine = np.arange(1, len(y)+1, 0.1)

popt, pcov = curve_fit(func, x, y)

fig = plt.figure()
ax4 = fig.add_subplot(1,1,1)
ax4.plot(x, y, '.')
ax4.plot(xfine, func(xfine, popt[0], popt[1], popt[2]),'r-')
ax4.set_xlabel('Time')
ax4.set_ylabel('Score')
plt.show()

您正在将线性函数拟合到ydata中。如果要拟合指数型,请尝试以下方法:

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

#define xdata dnd ydata

ydata = [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 
        3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 
        7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 
        10, 10, 10, 10, 10, 11, 12]
y = np.array(ydata)
x = np.arange(1,len(y)+1,1)

def func(xdata, a, b, c):
    return (a*np.power(b, xdata)) + c
    #or return (a*np.exp(-b*xdata))

xfine = np.arange(1, len(y)+1, 0.1)

popt, pcov = curve_fit(func, x, y)

fig = plt.figure()
ax4 = fig.add_subplot(1,1,1)
ax4.plot(x, y, '.')
ax4.plot(xfine, func(xfine, popt[0], popt[1], popt[2]),'r-')
ax4.set_xlabel('Time')
ax4.set_ylabel('Score')
plt.show()

尝试使用
t=6
xfine=np.linspace(x.min(),x.max(),100)
。看看这是不是你想要的want@Sheldore它确实工作得更好,但函数仍然是错误的,不知道如何改进
func()
好吧,有了@Sheldore提出的更改,我肯定从曲线拟合中得到了我所期望的结果。您想要什么行为?尝试使用
t=6
xfine=np.linspace(x.min(),x.max(),100)
。看看这是不是你想要的want@Sheldore它确实工作得更好,但函数仍然是错误的,不知道如何改进
func()
好吧,有了@Sheldore提出的更改,我肯定从曲线拟合中得到了我所期望的结果。你想要什么样的行为?这看起来不错,谢谢。我对这些数据进行了更多的测量(意味着y将不同,x将相同),我如何组合它们?我试图传递
y=[ydata\u list1,ydata\u list2]x=[1,2…60],[1,2…60]
,但它没有真正起作用:/Function:一种特殊的关系,其中每个输入都有一个输出。所以你的输入似乎不是一个正确的函数,它不是一个真正的函数,尽管函数应该足够接近预测。我真的不知道我还能尝试什么。这个看起来不错,谢谢。我对这些数据进行了更多的测量(意味着y将不同,x将相同),我如何组合它们?我试图传递
y=[ydata\u list1,ydata\u list2]x=[1,2…60],[1,2…60]
,但它没有真正起作用:/Function:一种特殊的关系,其中每个输入都有一个输出。所以你的输入似乎不是一个正确的函数,它不是一个真正的函数,尽管函数应该足够接近预测。我真的不知道我还能尝试什么。