亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python:曲線擬合看起來很混亂

Python:曲線擬合看起來很混亂

隔江千里 2023-09-26 14:53:59
我試圖將曲線擬合到某些數據,但生成的曲線看起來像一團亂麻。不知道系數是否準確。使用這個示例數據集,它會打印出類似三角形的東西,而使用我的原始數據集,它看起來更糟。大部分都是教程。我嘗試從備用教程中刪除 sympy 代碼,但這樣做沒有任何效果。import matplotlib.pyplot as pltfrom scipy.optimize import curve_fitimport numpy as npimport sympy as symx = [0.0009425070688029959,0.0009398496240601303,0.0018779342723004293,0.004694835680751241,0.0009425070688029959,0.004734848484848552,0.0018993352326685255,0.0009460737937558928]y = [0.0028301886792453904,0.003762935089369628,0.001881467544684814,0.0009433962264150743,0.0028301886792453904,0.0019029495718363059,0.0038058991436727804,0.0018939393939393534]"""Plot your data"""plt.plot(x, y, 'ro',label="Original Data")"""brutal force to avoid errors"""    x = np.array(x, dtype=float) #transform your data in a numpy array of floats y = np.array(y, dtype=float) #so the curve_fit can work"""create a function to fit with your data. a, b, c and d are the coefficientsthat curve_fit will calculate for you. In this part you need to guess and/or use mathematical knowledge to finda function that resembles your data"""def func(x, b, c, d):  return b * x * x + c * x + d"""make the curve_fit"""popt, pcov = curve_fit(func, x, y)"""The result is:popt[0] = a , popt[1] = b, popt[2] = c and popt[3] = d of the function,so f(x) = popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3]."""print("b = " + str(popt[0]) + "  c = " + str(popt[1]) + "  d = " + str(popt[2]))"""tUse sympy to generate the latex sintax of the function"""xs = sym.Symbol('\lambda')    tex = sym.latex(func(xs,*popt)).replace('$', '')plt.title(r'$f(\lambda)= %s$' %(tex),fontsize=16)"""Print the coefficients and plot the funcion."""plt.plot(x, func(x, *popt), label="Fitted Curve") #same as line above \/#plt.plot(x, popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3], label="Fitted Curve") plt.legend(loc='upper left')plt.show()
查看完整描述

2 回答

?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

這是因為 Matplotlib 只會在原始數據(x 和 y 數組中)的幾個點之間并按照它們定義的順序繪制線條。只有 3 個唯一的 x 值(加上一些噪聲),這就是為什么您看到的看起來像三角形的原因。

解決方法是創建一個新數組,在您感興趣的范圍內均勻分布且有序的 x 值。您可以使用numpy 中的 linspace 函數來完成此操作。

例如,嘗試執行第二個繪圖命令:

x_eval?=?np.linspace(min(x),?max(x),?100)

plt.plot(x_eval,?func(x_eval,?*popt),?label="Fitted?Curve")

x_eval上面是原始數據中最小和最大 x 值之間均勻分布的 100 個值的列表。


查看完整回答
反對 回復 2023-09-26
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

看起來你需要排序xdata。


嘗試插入這個:


x,y = zip(*sorted(zip(x, y)))


這樣


import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

import numpy as np

import sympy as sym


x = [0.0009425070688029959,

0.0009398496240601303,

0.0018779342723004293,

0.004694835680751241,

0.0009425070688029959,

0.004734848484848552,

0.0018993352326685255,

0.0009460737937558928]

y = [0.0028301886792453904,

0.003762935089369628,

0.001881467544684814,

0.0009433962264150743,

0.0028301886792453904,

0.0019029495718363059,

0.0038058991436727804,

0.0018939393939393534]

"""

Plot your data

"""

plt.plot(x, y, 'ro',label="Original Data")


"""

brutal force to avoid errors

"""    


x,y = zip(*sorted(zip(x, y)))


x = np.array(x, dtype=float) #transform your data in a numpy array of floats 

y = np.array(y, dtype=float) #so the curve_fit can work


"""

create a function to fit with your data. a, b, c and d are the coefficients

that curve_fit will calculate for you. 

In this part you need to guess and/or use mathematical knowledge to find

a function that resembles your data

"""

def func(x, b, c, d):

  return b * x * x + c * x + d


"""

make the curve_fit

"""

popt, pcov = curve_fit(func, x, y)


"""

The result is:

popt[0] = a , popt[1] = b, popt[2] = c and popt[3] = d of the function,

so f(x) = popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3].

"""

print("b = " + str(popt[0]) + "  c = " + str(popt[1]) + "  d = " + str(popt[2]))


"""t

Use sympy to generate the latex sintax of the function

"""

xs = sym.Symbol('\lambda')    

tex = sym.latex(func(xs,*popt)).replace('$', '')

plt.title(r'$f(\lambda)= %s$' %(tex),fontsize=16)


"""

Print the coefficients and plot the funcion.

"""


plt.plot(x, func(x, *popt), label="Fitted Curve") #same as line above \/

#plt.plot(x, popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3], label="Fitted Curve") 


plt.legend(loc='upper left')

plt.show()


The plotted curve from the data above.



查看完整回答
反對 回復 2023-09-26
  • 2 回答
  • 0 關注
  • 214 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號