我数据的数据拟合公式的图和值是错误的,我不知道如何修复它们

发布于 2025-01-27 12:14:01 字数 1959 浏览 3 评论 0原文

数据拟合中的问题
大家好,我在python中很新,这是我第一次在堆栈溢出上写。我在拟合数据方面有问题。使用此代码,我想证明赛车速度和心率之间的相关性。我的代码实际上无法正常工作,如果我将结果放入exponentialldata = odendential(x_data,...,...,...,...)该图上向上移动150个单位。 另外,如果我将数据估计的数据估计,知道图表的数据不太合适,则数据不太适合数据(我将其发送给您的图片)。我认为计算出的前两个值是正确或非常接近的,但第三个值应约为5.2。 也许有人想到为此数据提供更好的方程式。

graph thragr,其值是由代码计算的值

具有TIRD值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

Graph with tird value 5.2

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2025-02-03 12:14:01

基本上,您的模型不是很好的选择。 AS a * exp(x -b)= a * exp(-b) * exp(x),等效于a'exp(x)。您至少需要a * exp(b * x) + c

有了这个,我变得很合适。

这就是我的工作方式:

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

exampledata = np.array([ 
    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
]).reshape( -1, 4)
print( exampledata )

#Define Function
def exponential(x, a, b, c):
  return a * np.exp( x * b ) + c


x_data = exampledata[:,2]
y_data = exampledata[:,3] 

# Plot the Data
fig = plt.figure( )
ax = fig.add_subplot( 1, 1, 1 )
ax.plot(x_data,y_data, 'o')
ax.set_title("Example Data")


#Evaluate and Plot Function
exponentialdata = exponential(x_data, 1.6e-6, 0.05,5.0)
plt.plot(x_data, exponentialdata, label="guess")

#Curve fit data to model
popt, pcov = curve_fit(exponential, x_data, y_data,  p0=(1e-6, 0.01,4.2),maxfev=1500)
ax.plot( x_data, exponential( x_data, *popt ), 'ro--',  label="Model")
perr = np.sqrt(np.diag(pcov))

plt.legend()

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

Basically your model is not well chosen. As a * exp(x -b) = a * exp(-b ) * exp(x) which is equivalent to a' exp(x). You need at least a a * exp( b * x ) + c.

With this I get a decent fit.

This is how mine is working:

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

exampledata = np.array([ 
    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
]).reshape( -1, 4)
print( exampledata )

#Define Function
def exponential(x, a, b, c):
  return a * np.exp( x * b ) + c


x_data = exampledata[:,2]
y_data = exampledata[:,3] 

# Plot the Data
fig = plt.figure( )
ax = fig.add_subplot( 1, 1, 1 )
ax.plot(x_data,y_data, 'o')
ax.set_title("Example Data")


#Evaluate and Plot Function
exponentialdata = exponential(x_data, 1.6e-6, 0.05,5.0)
plt.plot(x_data, exponentialdata, label="guess")

#Curve fit data to model
popt, pcov = curve_fit(exponential, x_data, y_data,  p0=(1e-6, 0.01,4.2),maxfev=1500)
ax.plot( x_data, exponential( x_data, *popt ), 'ro--',  label="Model")
perr = np.sqrt(np.diag(pcov))

plt.legend()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文