2017-05-14 56 views

回答

2

看一看

https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.optimize.curve_fit.html

有在底部,这几乎做了你所追求的一个例子。

编辑:回复评论

import matplotlib.pyplot as plt; 
import numpy as np; 
import scipy.optimize as opt; 

# This is the function we are trying to fit to the data. 
def func(x, a, b, c): 
    return a * np.exp(-b * x) + c 

# Generate some data, you don't have to do this, as you already have your data 
xdata = np.linspace(0, 4, 50) 
y = func(xdata, 2.5, 1.3, 0.5) 
y_noise = 0.2 * np.random.normal(size=xdata.size) 
ydata = y + y_noise 

# Plot the actual data 
plt.plot(xdata, ydata, ".", label="Data"); 

# The actual curve fitting happens here 
optimizedParameters, pcov = opt.curve_fit(func, xdata, ydata); 

# Use the optimized parameters to plot the best fit 
plt.plot(xdata, func(xdata, *optimizedParameters), label="fit"); 

# Show the graph 
plt.legend(); 
plt.show(); 

的X,Y数据是扩展数据和YDATA变量。所以如果你想使用这段代码,只需取出生成数据的位,并将x,y数据数组定义为“xdata”和“ydata”。

+0

这个例子对我来说非常复杂,因为我是python中的新手。你能告诉我,如果我使用你给出的例子的代码,我应该在哪里放置我的x和y值。 –