簡體   English   中英

Python使用curve_fit擬合對數函數

[英]Python using curve_fit to fit a logarithmic function

我正在嘗試使用curve_fit擬合對數曲線,假設它遵循Y=a*ln(X)+b ,但擬合數據仍然看起來curve_fit

現在我正在使用以下代碼:

from scipy.optimize import curve_fit
X=[3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4,
   4.5, 4.6, 4.7]
Y=[-5.890486683, -3.87063815, -2.733484754, -2.104972457, -1.728190699, 
   -1.477976987, -1.285589215, -1.120224363, -0.968576581, -0.82492453, 
   -0.688457731, -0.559780327, -0.440437932, -0.331886009, -0.235162505, 
   -0.150572236, -0.078157925, -0.01718885]

#plot Y against X
fig = plt.figure(num=None, figsize=(9, 7),facecolor='w', edgecolor='k')
ax2=fig.add_subplot(111)
ax2.scatter(X,Y)

#fit using curve_fit
popt, pcov = curve_fit(Hyp_func, X, Y,maxfev=10000)
print(' fit coefficients:\n', popt)
#fit coefficients:
#[9.51543579 -14.10114674]

#plot Y_estimated against X
Y_estimated=[popt[0]*np.log(i)+popt[1] for i in X]
ax2.scatter(X,Y_estimated, c='r')
def Hyp_func(x, a,b):
    return a*np.log(x)+b

在此處輸入圖片說明

擬合曲線(紅色)看起來仍然不像讀取曲線(藍色)那樣“彎曲”。 任何幫助,將不勝感激。

X 數據值有時需要為這個方程移動一點,當我嘗試這個時它工作得很好。 這是使用您的數據和 X 位移方程“y = a * ln(x + b)+c”的圖形 Python 擬合器。

在此處輸入圖片說明

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

# ignore any "invalid value in log" warnings internal to curve_fit() routine
import warnings
warnings.filterwarnings("ignore")

X=[3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7]
Y=[-5.890486683, -3.87063815, -2.733484754, -2.104972457, -1.728190699, -1.477976987, -1.285589215, -1.120224363, -0.968576581, -0.82492453, -0.688457731, -0.559780327, -0.440437932, -0.331886009, -0.235162505, -0.150572236, -0.078157925, -0.01718885]

# alias data to match previous example
xData = numpy.array(X, dtype=float)
yData = numpy.array(Y, dtype=float)

def func(x, a, b, c): # x-shifted log
    return a*numpy.log(x + b)+c

# these are the same as the scipy defaults
initialParameters = numpy.array([1.0, 1.0, 1.0])

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

modelPredictions = func(xData, *fittedParameters) 

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = func(xModel, *fittedParameters)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM