我数据的数据拟合公式的图和值是错误的,我不知道如何修复它们
数据拟合中的问题
大家好,我在python中很新,这是我第一次在堆栈溢出上写。我在拟合数据方面有问题。使用此代码,我想证明赛车速度和心率之间的相关性。我的代码实际上无法正常工作,如果我将结果放入exponentialldata = odendential(x_data,...,...,...,...)
该图上向上移动150个单位。 另外,如果我将数据估计的数据估计,知道图表的数据不太合适,则数据不太适合数据(我将其发送给您的图片)。我认为计算出的前两个值是正确或非常接近的,但第三个值应约为5.2。 也许有人想到为此数据提供更好的方程式。
的图形这是我使用的一组数据:
Pace (seconds);Stroke Rate;Heart Rate;Speed (m/s)
97;26;157;5.15
94.9;28;165;5.27
95.8;26;170;5.22
92.5;30;170;5.41
94;28;173;5.32
90.6;32;173;5.52
94.6;27;176;5.29
91.7;30;177;5.45
92;29;178;5.43
90;32;180;5.56
89.8;31;182;5.57
87.9;32;184;5.69
您是否有人可以帮助我解决这些问题?我不知道该怎么做,我一直在这里呆了几天。 预先感谢,我希望我在解释问题上足够清楚
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import csv
## PACE-BPM ##
# Import CSV Data
with open("new 1 minuto 26-28-30-32.csv","r") as i:
rawdata = list(csv.reader(i,delimiter = ";"))
exampledata = np.array(rawdata[1:], dtype=np.float)
x_data = exampledata[:,2]
y_data = exampledata[:,3]
# Plot the Data
plt.figure(2,dpi=120)
plt.plot(x_data,y_data, 'o')
plt.title("Example Data")
plt.xlabel(rawdata[0][2])
plt.ylabel(rawdata[0][3])
#Define Function
def exponential(x, a, b, c):
return a * np.exp(x-b) + c
#Evaluate and Plot Function
exponentialdata = exponential(x_data, 155, 188, 5.2)
plt.plot(x_data, exponentialdata, 'ro--', label="Model")
plt.legend()
#Curve fit data to model
popt, pcov = curve_fit(exponential, x_data, y_data, bounds=(155,188))
perr = np.sqrt(np.diag(pcov))
fit_a = popt[0]
fit_b = popt[1]
fit_c = popt[2]
print(fit_a)
print(fit_b)
print(fit_c)
##########
# Show the plot
plt.show()
Problem in data fitting
Hi everyone, I'm quite new in Python and this is the first time I write on Stack Overflow. I've got a problem in fitting the data. With this code I would like to demonstrate a correlation between speed and heart rate in rowing. My code doesn't work properly in fact if i put the results in exponentialdata = exponential(x_data, ..., ..., ...)
the graph it has moved upwards by 150 units.
Also if I put the data that I estimated manually knowing a little of exponential proprieties the graph didn't fit so well the data (I send you herewith a picture). I think that the first two value calculated are correct or very close but the third should be around 5.2.
Maybe someone has an idea for a better equation for this data.
Graph with values calculated by the code
This is the set of data that I used:
Pace (seconds);Stroke Rate;Heart Rate;Speed (m/s)
97;26;157;5.15
94.9;28;165;5.27
95.8;26;170;5.22
92.5;30;170;5.41
94;28;173;5.32
90.6;32;173;5.52
94.6;27;176;5.29
91.7;30;177;5.45
92;29;178;5.43
90;32;180;5.56
89.8;31;182;5.57
87.9;32;184;5.69
Is there anyone of you that can help me with these problems? I don't know how to do it, I've been stuck here for days.
Thanks in advance and I hope I was clear enough in explaining my problems
This is my code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import csv
## PACE-BPM ##
# Import CSV Data
with open("new 1 minuto 26-28-30-32.csv","r") as i:
rawdata = list(csv.reader(i,delimiter = ";"))
exampledata = np.array(rawdata[1:], dtype=np.float)
x_data = exampledata[:,2]
y_data = exampledata[:,3]
# Plot the Data
plt.figure(2,dpi=120)
plt.plot(x_data,y_data, 'o')
plt.title("Example Data")
plt.xlabel(rawdata[0][2])
plt.ylabel(rawdata[0][3])
#Define Function
def exponential(x, a, b, c):
return a * np.exp(x-b) + c
#Evaluate and Plot Function
exponentialdata = exponential(x_data, 155, 188, 5.2)
plt.plot(x_data, exponentialdata, 'ro--', label="Model")
plt.legend()
#Curve fit data to model
popt, pcov = curve_fit(exponential, x_data, y_data, bounds=(155,188))
perr = np.sqrt(np.diag(pcov))
fit_a = popt[0]
fit_b = popt[1]
fit_c = popt[2]
print(fit_a)
print(fit_b)
print(fit_c)
##########
# Show the plot
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您的模型不是很好的选择。 AS
a * exp(x -b)= a * exp(-b) * exp(x)
,等效于a'exp(x)
。您至少需要a * exp(b * x) + c
。有了这个,我变得很合适。
这就是我的工作方式:
Basically your model is not well chosen. As
a * exp(x -b) = a * exp(-b ) * exp(x)
which is equivalent toa' exp(x)
. You need at least aa * exp( b * x ) + c
.With this I get a decent fit.
This is how mine is working: