简体   繁体   English

拟合数据集的指数曲线

[英]Exponential curve fitting a data set

import numpy as np
import matplotlib.pyplot as plt

points = np.array([
 (333, 195.3267),
 (500, 223.0235),
 (1000, 264.5914),
 (2000, 294.8728),
 (5000, 328.3523),
 (10000, 345.4688)
])

# get x and y vectors
x = points[:,0]
y = points[:,1]

What would be my next steps in order to create an exponential curve fit the graph? 为了创建适合该图的指数曲线,下一步应该做什么?

Here is an example of fitting the data to a logarithmic quadratic equation that fits the data somewhat better than an exponential, and plots the fitted curve against a scatterplot of the raw data. 这是将数据拟合为对数二次方程的示例,该二次方程比指数拟合的数据好一些,并针对原始数据的散点图绘制了拟合曲线。 The code is not optimal, for example it repeatedly takes the log of X rather than just doing it one time. 该代码不是最佳的,例如,它反复记录X的日志,而不是一次记录一次。 The log(x) data can be more efficiently fit by directly using a linear fitting method as well, however here you can replace the fitted equation with an exponential much more easily with less code changes. 通过直接使用线性拟合方法也可以更有效地拟合log(x)数据,但是在这里,您可以用更少的代码更改更轻松地用指数替换拟合方程。

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

points = numpy.array([(333, 195.3267), (500, 223.0235), (1000, 264.5914), (2000, 294.8728
), (5000, 328.3523), (10000, 345.4688)])
# get x and y vectors
xData = points[:,0]
yData = points[:,1]

# function to be fitted
def LogQuadratic(x, a, b, c):
    return a + b*numpy.log(x) + c*numpy.power(numpy.log(x), 2.0)


# some initial parameter values
initialParameters = numpy.array([1.0, 1.0, 1.0])

fittedParameters, pcov = curve_fit(LogQuadratic, xData, yData, initialParameters)

# values for display of fitted function
a, b, c = fittedParameters

# for plotting the fitting results
xPlotData = numpy.linspace(min(xData), max(xData), 50)
y_plot = LogQuadratic(xPlotData, a, b, c)

plt.plot(xData, yData, 'D') # plot the raw data as a scatterplot
plt.plot(xPlotData, y_plot) # plot the equation using the fitted parameters
plt.show()

print('fitted parameters:', fittedParameters)

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

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