簡體   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]

為了創建適合該圖的指數曲線,下一步應該做什么?

這是將數據擬合為對數二次方程的示例,該二次方程比指數擬合的數據好一些,並針對原始數據的散點圖繪制了擬合曲線。 該代碼不是最佳的,例如,它反復記錄X的日志,而不是一次記錄一次。 通過直接使用線性擬合方法也可以更有效地擬合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