sách gpt4 ai đã đi

Python 和 matplotlib 绘制超出域的点,曲线拟合较差

In lại 作者:太空宇宙 更新时间:2023-11-03 21:19:20 27 4
mua khóa gpt4 Nike

首先我要说的是,我对 Python 中的曲线拟合完全陌生,所以我可能做的事情完全是明显错误的。

我有一个实验性“原始”数据集,其中包含温度 (x) 与信号 (y)。

Tôi đang cố gắng sử dụng scipy.curve_fit 将玻尔兹曼方程拟合到该数据。我的脚本没有抛出任何错误,但是当我在 matplotlib 中绘制它时,当我的实验数据域仅包含 ~308 --> 400 之间的值时,它使用 0 到 600 之间的 x 值。不仅如此,它拟合的曲线数据>似乎<>

#Get the data
file = 'Data/my_excel_file.xlsx'
df = pd.read_excel(file, sheet_name='simplified')

#Define the Boltzmann function for fitting
def BoltzmannFitEquation(T, Fmin, Fmax, Tm, breadth):
return Fmin + ((Fmax - Fmin) / (1 + np.exp((Tm - (T/breadth)))))

#Grabbing the y-values (signal) from the dataframe
signal = df['signal'].tolist()

#Convert my temps from the dataframe from C to K.
for temp in temps_c:
temps_k.append(float(temp) + 273)

#Now lets fit a Boltzmann equation to the smoothed data
p0 = [0.9, 1.2, 347, 1] #initial predictions
c, cov = curve_fit(BoltzmannFitEquation, temps_k, signal, p0)

yp = BoltzmannFitEquation(temps_k, c[0], c[1], c[2], c[3]) #Plot of the prediction with the optimized coefficients

plt.plot(yp)

我排除了一堆代码来简化事情 - 但如果您想具体查看一些内容,请告诉我,它可以帮助解决我看到此问题的原因。

蓝线是“原始”数据和导数,橙色线是拟合曲线和导数。

nhập mô tả hình ảnh ở đây

请注意拐点在顶部图表上不匹配,但在底部图表上匹配。为什么曲线拟合这么差?为什么它甚至会包含域之外的值?

1 Câu trả lời

当我尝试以下代码时,我看起来很合适。

nhập pandas dưới dạng pd
nhập numpy dưới dạng np
nhập matplotlib.pyplot dưới dạng plt
from scipy.optimize import curve_fit

file = 'Data/my_excel_file.xlsx'
#file = '/home/zunzun/temp/temp.xlsx'
df = pd.read_excel(file, sheet_name='simplified')

#Define the Boltzmann function for fitting
def BoltzmannFitEquation(T, Fmin, Fmax, Tm, breadth):
return Fmin + ((Fmax - Fmin) / (1 + np.exp((Tm - (T/breadth)))))

#Grabbing the data from the dataframe
signal = np.array(df['signal'].tolist())
temps_c = np.array(df['temperature'].tolist())

#Convert my temps from the dataframe from C to K.
temps_k = temps_c + 273.0

#Now lets fit a Boltzmann equation to the smoothed data
p0 = [0.9, 1.2, 347, 1] #initial predictions
c, cov = curve_fit(BoltzmannFitEquation, temps_k, signal, p0)

yp = BoltzmannFitEquation(temps_k, c[0], c[1], c[2], c[3]) #Plot of the prediction with the optimized coefficients

print("Fitted paraneters:", c)

plt.plot(temps_k, signal) # data
plt.plot(temps_k, yp) # fit
plt.hiển thị()

plots

关于Python 和 matplotlib 绘制超出域的点,曲线拟合较差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54428680/

27 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com