gpt4 book ai didi

python - 求曲线中的线性部分和斜率

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:37 25 4
gpt4 key购买 nike

对于监测质量随时间变化的设备,我们希望计算数据线性部分的斜率。

下面显示的示例是通过读取设备生成的数据帧生成的。

import pandas as pd
import matplotlib.pyplot as plt

#Find DataFrame
df = pd.read_table("raw_data.csv", sep =";", skiprows = 11, decimal = ",")

# Plot figures
plt.figure()
plt.plot(df["Time(s)"], df["Mass(g)"], label = "Raw Data")
plt.axvspan(2, 17, color="b", alpha=0.2)
plt.xlabel("Time (s)")
plt.ylabel("Mass (g)")
plt.legend(loc=0)
plt.axis([0, None, 0, None])
plt.show()

Example curve

是否可以拟合该曲线中的线性部分(大致突出显示的部分)并计算其斜率?

最佳答案

使用numpy.polyfit():

df_sampled = df[:max_value] #select the points you want to keep
m, p = numpy.polyfit(df_sampled.index, df_sampled, deg=1)

该函数返回线性回归的斜率截距

关于python - 求曲线中的线性部分和斜率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39835069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com