gpt4 book ai didi

Python:获取时间序列线性图中曲线的起止时间

转载 作者:行者123 更新时间:2023-12-04 17:14:07 37 4
gpt4 key购买 nike

我正在尝试找到线性曲线的起点和终点。曲线中可能会有一些下降,因此应该有“如果 y 秒内小于 x,则忽略”

开始和结束处的扁平线不是 0,而是来自传感器的背景噪声的测量值。

我确定在 scipy 或其他东西中有这个名称,但到目前为止,我一直无法找到它,所以任何人都可以提供帮助(不一定在 scipy 或 pandas 中)

下面的代码和图表示例。我需要知道 T1 和 T2 的时间(以纳秒为单位),这样我才能找到测量的总时间。

import pandas as pd
dg = pd.read_csv("curve.csv", sep=";")
dg.to_json()
'{"tol":{"0":"0,60","1":"0,40","2":"0,60","3":"0,50","4":"0,60","5":"0,40","6":"0,60","7":"0,40","8":"0,50","9":"0,70","10":"0,40","11":"14,00","12":"15,00","13":"13,00","14":"16,00","15":"16,00","16":"12,00","17":"1,00","18":"1,00","19":"19,00","20":"18,00","21":"17,00","22":"16,00","23":"19,00","24":"30,00","25":"20,00","26":"5,00","27":"0,50","28":"0,60","29":"0,40","30":"0,50","31":"0,30","32":"0,70","33":"0,30"},"time":{"0":"06:00:01","1":"06:00:02","2":"06:00:03","3":"06:00:04","4":"06:00:05","5":"06:00:06","6":"06:00:07","7":"06:00:08","8":"06:00:09","9":"06:00:10","10":"06:00:11","11":"06:00:12","12":"06:00:13","13":"06:00:14","14":"06:00:15","15":"06:00:16","16":"06:00:17","17":"06:00:18","18":"06:00:19","19":"06:00:20","20":"06:00:21","21":"06:00:22","22":"06:00:23","23":"06:00:24","24":"06:00:25","25":"06:00:26","26":"06:00:27","27":"06:00:28","28":"06:00:29","29":"06:00:30","30":"06:00:31","31":"06:00:32","32":"06:00:33","33":"06:00:34"}}'
import plotly.graph_objects as go
fig = go.Figure(data=go.Line(y=dg.tol.to_list(),x = dg.time))
fig.show()

enter image description here

最佳答案

所以我会实现一个移动平均值,然后在滚动平均值高于某个值时获取最小和最大时间戳。

以下代码应该有所帮助:

import numpy as np
import pandas as pd
np.random.seed(1)

# Create fake data that has low levels besides a model with high values
rows,cols = 1000,1
data = np.random.rand(rows,cols) # You can use other random functions to generate values with constraints
tidx = pd.date_range('2019-01-01', periods=rows, freq='S') # freq='MS'set the frequency of date in months and start from day 1. You can use 'T' for minutes and so on
data_frame = pd.DataFrame(data, columns=['value'], index=tidx)
data_frame['row_number'] = range(len(data_frame))
data_frame['value'] = np.where(
(data_frame['row_number'] >= 250) & (data_frame['row_number'] <= 500),
(data_frame['value'] + .02) * 1000,
data_frame['value'] * 50
)
data_frame = data_frame.drop('row_number', axis=1)

# Tune the below two values to get the proper senstivity
rolling_string = '5s'
trigger_amount = 75

# Run rolling average
data_frame['rolling_average'] = data_frame.rolling(rolling_string).mean()
data_frame = data_frame.reset_index()
data_frame.columns=['date', 'value', 'rolling_average']

# Get rows of the trigger amount
data_frame['increased'] = np.where(
data_frame['value'] >= trigger_amount,
True,
False,
)

# Find the earliest and latest date above these values
early_date = data_frame[data_frame['increased'] == True]['date'].min()
late_date = data_frame[data_frame['increased'] == True]['date'].max()

# Print values
print("t1: " + str(early_date) + "\nt2: " +str(late_date))

###Output###
t1: 2019-01-01 00:04:11
t2: 2019-01-01 00:08:20

用于滚动平均值的秒数(或在您的情况下为纳秒)越多,在急剧增加和减少时拾取的速度就越慢。触发值也是如此。

最后可视化我的工作:

import plotly.express as px

viz = data_frame.melt(id_vars=['date'], value_vars=['value', 'rolling_average'])

fig = px.line(data_frame=viz, x='date', y='value', color='variable')
fig.add_vline(x=early_date)
fig.add_vline(x=late_date)
fig.add_hline(y=trigger_amount)
fig.show()

enter image description here

关于Python:获取时间序列线性图中曲线的起止时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69004192/

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