日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 非线性多项式拟合_用python进行非线性回归-有什么简单的方法可以更好地拟合这些数据?...

發布時間:2025/3/15 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 非线性多项式拟合_用python进行非线性回归-有什么简单的方法可以更好地拟合这些数据?... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

此示例代碼使用具有兩個形狀參數(a和b)和偏移項(不影響曲率)的表達式。方程為“y=1.0/(1.0+exp(-a(x-b)))+Offset”,參數值a=2.1540318329369712E-01,b=-6.6744890642157646E+00,Offset=-3.524129985969645e-01,R平方為0.988,RMSE為0.0085。

該示例包含您用Python代碼發布的數據,用于擬合和繪制,并使用scipy.optimize.differential_evolution遺傳算法自動估計初始參數。差分進化的scipy實現使用拉丁超立方體算法來確保對參數空間的徹底搜索,這需要搜索的范圍-在本示例代碼中,這些范圍基于最大和最小數據值。

import numpy, scipy, matplotlib

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

from scipy.optimize import differential_evolution

import warnings

xData = numpy.array([19.1647, 18.0189, 16.9550, 15.7683, 14.7044, 13.6269, 12.6040, 11.4309, 10.2987, 9.23465, 8.18440, 7.89789, 7.62498, 7.36571, 7.01106, 6.71094, 6.46548, 6.27436, 6.16543, 6.05569, 5.91904, 5.78247, 5.53661, 4.85425, 4.29468, 3.74888, 3.16206, 2.58882, 1.93371, 1.52426, 1.14211, 0.719035, 0.377708, 0.0226971, -0.223181, -0.537231, -0.878491, -1.27484, -1.45266, -1.57583, -1.61717])

yData = numpy.array([0.644557, 0.641059, 0.637555, 0.634059, 0.634135, 0.631825, 0.631899, 0.627209, 0.622516, 0.617818, 0.616103, 0.613736, 0.610175, 0.606613, 0.605445, 0.603676, 0.604887, 0.600127, 0.604909, 0.588207, 0.581056, 0.576292, 0.566761, 0.555472, 0.545367, 0.538842, 0.529336, 0.518635, 0.506747, 0.499018, 0.491885, 0.484754, 0.475230, 0.464514, 0.454387, 0.444861, 0.437128, 0.415076, 0.401363, 0.390034, 0.378698])

def func(x, a, b, Offset): # Sigmoid A With Offset from zunzun.com

return 1.0 / (1.0 + numpy.exp(-a * (x-b))) + Offset

# function for genetic algorithm to minimize (sum of squared error)

def sumOfSquaredError(parameterTuple):

warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm

val = func(xData, *parameterTuple)

return numpy.sum((yData - val) ** 2.0)

def generate_Initial_Parameters():

# min and max used for bounds

maxX = max(xData)

minX = min(xData)

maxY = max(yData)

minY = min(yData)

parameterBounds = []

parameterBounds.append([minX, maxX]) # seach bounds for a

parameterBounds.append([minX, maxX]) # seach bounds for b

parameterBounds.append([0.0, maxY]) # seach bounds for Offset

# "seed" the numpy random number generator for repeatable results

result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)

return result.x

# generate initial parameter values

geneticParameters = generate_Initial_Parameters()

# curve fit the test data

fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters)

print('Parameters', fittedParameters)

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('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)

總結

以上是生活随笔為你收集整理的python 非线性多项式拟合_用python进行非线性回归-有什么简单的方法可以更好地拟合这些数据?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。