欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用python做數(shù)據(jù)擬合詳情

 更新時間:2022年01月24日 12:58:49   作者:圖様  
這篇文章主要介紹了利用python做數(shù)據(jù)擬合,下面文章圍繞如何讓利用python做數(shù)據(jù)擬合的相關資料展開詳細內容,需要的朋友可以參考一下,希望對大家有所幫助

1、例子:擬合一種函數(shù)Func,此處為一個指數(shù)函數(shù)。

出處:

SciPy v1.1.0 Reference Guide

#Header
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

#Define a function(here a exponential function is used)
def func(x, a, b, c):
 return a * np.exp(-b * x) + c

#Create the data to be fit with some noise
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
np.random.seed(1729)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
plt.plot(xdata, ydata, 'bo', label='data')

#Fit for the parameters a, b, c of the function func:
popt, pcov = curve_fit(func, xdata, ydata)
popt #output: array([ 2.55423706, 1.35190947, 0.47450618])
plt.plot(xdata, func(xdata, *popt), 'r-',
 label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

#In the case of parameters a,b,c need be constrainted
#Constrain the optimization to the region of 
#0 <= a <= 3, 0 <= b <= 1 and 0 <= c <= 0.5
popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5]))
popt #output: array([ 2.43708906, 1. , 0.35015434])
plt.plot(xdata, func(xdata, *popt), 'g--',
 label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

#Labels
plt.title("Exponential Function Fitting")
plt.xlabel('x coordinate')
plt.ylabel('y coordinate')
plt.legend()
leg = plt.legend()  # remove the frame of Legend, personal choice
leg.get_frame().set_linewidth(0.0) # remove the frame of Legend, personal choice
#leg.get_frame().set_edgecolor('b') # change the color of Legend frame
#plt.show()

#Export figure
#plt.savefig('fit1.eps', format='eps', dpi=1000)
plt.savefig('fit1.pdf', format='pdf', dpi=1000, figsize=(8, 6), facecolor='w', edgecolor='k')
plt.savefig('fit1.jpg', format='jpg', dpi=1000, figsize=(8, 6), facecolor='w', edgecolor='k')

上面一段代碼可以直接在spyder中運行。得到的JPG導出圖如下:

2. 例子:擬合一個Gaussian函數(shù)

出處:LMFIT: Non-Linear Least-Squares Minimization and Curve-Fitting for Python

#Header
import numpy as np
import matplotlib.pyplot as plt
from numpy import exp, linspace, random
from scipy.optimize import curve_fit

#Define the Gaussian function
def gaussian(x, amp, cen, wid):
 return amp * exp(-(x-cen)**2 / wid)

#Create the data to be fitted
x = linspace(-10, 10, 101)
y = gaussian(x, 2.33, 0.21, 1.51) + random.normal(0, 0.2, len(x))
np.savetxt ('data.dat',[x,y])  #[x,y] is is saved as a matrix of 2 lines

#Set the initial(init) values of parameters need to optimize(best)
init_vals = [1, 0, 1] # for [amp, cen, wid]

#Define the optimized values of parameters
best_vals, covar = curve_fit(gaussian, x, y, p0=init_vals)
print(best_vals) # output: array [2.27317256  0.20682276  1.64512305]

#Plot the curve with initial parameters and optimized parameters
y1 = gaussian(x, *best_vals) #best_vals, '*'is used to read-out the values in the array
y2 = gaussian(x, *init_vals) #init_vals
plt.plot(x, y, 'bo',label='raw data')
plt.plot(x, y1, 'r-',label='best_vals')
plt.plot(x, y2, 'k--',label='init_vals')
#plt.show()

#Labels
plt.title("Gaussian Function Fitting")
plt.xlabel('x coordinate')
plt.ylabel('y coordinate')
plt.legend()
leg = plt.legend()  # remove the frame of Legend, personal choice
leg.get_frame().set_linewidth(0.0) # remove the frame of Legend, personal choice
#leg.get_frame().set_edgecolor('b') # change the color of Legend frame
#plt.show()

#Export figure
#plt.savefig('fit2.eps', format='eps', dpi=1000)
plt.savefig('fit2.pdf', format='pdf', dpi=1000, figsize=(8, 6), facecolor='w', edgecolor='k')
plt.savefig('fit2.jpg', format='jpg', dpi=1000, figsize=(8, 6), facecolor='w', edgecolor='k')

上面一段代碼可以直接在spyder中運行。得到的JPG導出圖如下:

3. 用一個lmfit的包來實現(xiàn)2中的Gaussian函數(shù)擬合

需要下載lmfit這個包,下載地址:

https://pypi.org/project/lmfit/#files

下載下來的文件是.tar.gz格式,在MacOS及Linux命令行中解壓,指令:

將其中的lmfit文件夾復制到當前project目錄下。

上述例子2中生成了data.dat,用來作為接下來的方法中的原始數(shù)據(jù)。

 出處:

Modeling Data and Curve Fitting

#Header
import numpy as np
import matplotlib.pyplot as plt
from numpy import exp, loadtxt, pi, sqrt
from lmfit import Model

#Import the data and define x, y and the function
data = loadtxt('data.dat')
x = data[0, :]
y = data[1, :]
def gaussian1(x, amp, cen, wid):
 return (amp / (sqrt(2*pi) * wid)) * exp(-(x-cen)**2 / (2*wid**2))

#Fitting
gmodel = Model(gaussian1)
result = gmodel.fit(y, x=x, amp=5, cen=5, wid=1) #Fit from initial values (5,5,1)
print(result.fit_report())

#Plot
plt.plot(x, y, 'bo',label='raw data')
plt.plot(x, result.init_fit, 'k--',label='init_fit')
plt.plot(x, result.best_fit, 'r-',label='best_fit')
#plt.show()


#Labels
plt.title("Gaussian Function Fitting")
plt.xlabel('x coordinate')
plt.ylabel('y coordinate')
plt.legend()
leg = plt.legend()  # remove the frame of Legend, personal choice
leg.get_frame().set_linewidth(0.0) # remove the frame of Legend, personal choice
#leg.get_frame().set_edgecolor('b') # change the color of Legend frame
#plt.show()

#Export figure
#plt.savefig('fit3.eps', format='eps', dpi=1000)
plt.savefig('fit3.pdf', format='pdf', dpi=1000, figsize=(8, 6), facecolor='w', edgecolor='k')
plt.savefig('fit3.jpg', format='jpg', dpi=1000, figsize=(8, 6), facecolor='w', edgecolor='k')

上面這一段代碼需要按指示下載lmfit包,并且讀取例子2中生成的data.dat。

得到的JPG導出圖如下:

到此這篇關于利用python做數(shù)據(jù)擬合詳情的文章就介紹到這了,更多相關python做數(shù)據(jù)擬合內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Flask實現(xiàn)定制日志并輸出到文件

    Flask實現(xiàn)定制日志并輸出到文件

    這篇文章主要為大家學習介紹了Flask如何實現(xiàn)定制日志并輸出到文件,文中的示例代碼簡介易懂,感興趣的小伙伴快跟隨小編一起學習一下吧
    2023-07-07
  • Python入門教程(四十)Python的NumPy數(shù)組創(chuàng)建

    Python入門教程(四十)Python的NumPy數(shù)組創(chuàng)建

    這篇文章主要介紹了Python入門教程(四十)Python的NumPy數(shù)組創(chuàng)建,NumPy 用于處理數(shù)組,NumPy 中的數(shù)組對象稱為 ndarray,我們可以使用 array() 函數(shù)創(chuàng)建一個 NumPy ndarray 對象,需要的朋友可以參考下
    2023-05-05
  • Python?matplotlib如何簡單繪制不同類型的表格

    Python?matplotlib如何簡單繪制不同類型的表格

    通過Matplotlib,開發(fā)者可以僅需要幾行代碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯誤圖,散點圖等,下面這篇文章主要給大家介紹了關于Python?matplotlib如何簡單繪制不同類型表格的相關資料,需要的朋友可以參考下
    2022-07-07
  • 基于Python實現(xiàn)一鍵找出磁盤里所有貓照

    基于Python實現(xiàn)一鍵找出磁盤里所有貓照

    最近在整理我磁盤上的照片,發(fā)現(xiàn)不少貓照,突然覺得若能把這些貓照都挑出來,觀察它們的成長軌跡也是一件不錯的事情。一張一張的找實在是太費勁了,能不能自動化地找出來呢?本文將詳細為大家講講,需要的可以參考一下
    2022-05-05
  • 淺析python 動態(tài)庫m.so.1.0錯誤問題

    淺析python 動態(tài)庫m.so.1.0錯誤問題

    這篇文章主要介紹了python 動態(tài)庫m.so.1.0錯誤問題,文中給大家提到了python中使用動態(tài)庫的方法,通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Scrapy-Redis結合POST請求獲取數(shù)據(jù)的方法示例

    Scrapy-Redis結合POST請求獲取數(shù)據(jù)的方法示例

    這篇文章主要給大家介紹了關于Scrapy-Redis結合POST請求獲取數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Scrapy-Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-05-05
  • python提取log文件內容并畫出圖表

    python提取log文件內容并畫出圖表

    這篇文章主要介紹了python提取log文件內容并畫出圖表,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • python實現(xiàn)時間序列自相關圖(acf)、偏自相關圖(pacf)教程

    python實現(xiàn)時間序列自相關圖(acf)、偏自相關圖(pacf)教程

    這篇文章主要介紹了python實現(xiàn)時間序列自相關圖(acf)、偏自相關圖(pacf)教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python使用OpenPyXL處理Excel表格

    Python使用OpenPyXL處理Excel表格

    這篇文章主要介紹了Python使用OpenPyXL處理Excel表格,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Python基礎學習之函數(shù)方法實例詳解

    Python基礎學習之函數(shù)方法實例詳解

    這篇文章主要介紹了Python基礎學習之函數(shù)方法,結合實例形式分析了Python函數(shù)方法的定義、參數(shù)、復用和繼承相關操作技巧,需要的朋友可以參考下
    2019-06-06

最新評論