You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算1->0:DELETE['TX']与0->1:ADD['RX']两组ECDF时间序列的差值

实现方法

你需要先分别计算两个类别的累积计数,再对齐时间轴做差值,具体代码如下:

1. 筛选目标类别并按时间排序

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# 筛选两个目标类别的数据,按时间升序排列
rx_add = df2[df2['seg_oper'] == "0->1:ADD['RX']"].sort_values('time', ignore_index=True)
tx_del = df2[df2['seg_oper'] == "1->0:DELETE['TX']"].sort_values('time', ignore_index=True)

2. 生成统一时间轴并计算累积计数差值

# 合并两个序列的所有时间点,去重排序得到统一X轴
all_times = np.sort(pd.concat([rx_add['time'], tx_del['time']]).unique())

# 分别计算两个序列在每个时间点的累积计数(和sns.ecdfplot stat='count'逻辑一致)
rx_cum_count = np.searchsorted(rx_add['time'], all_times, side='right')
tx_cum_count = np.searchsorted(tx_del['time'], all_times, side='right')

# 计算差值
diff_cum_count = tx_cum_count - rx_cum_count

上述代码中np.searchsorted的计算逻辑和seaborn.ecdfplot中stat='count'的累积计数逻辑完全一致,计算结果和你原来绘制的ECDF曲线数值完全匹配。

3. 结果查看与可视化

如果需要可视化差值曲线可以直接绘制:

# 绘制差值曲线
sns.set_style('whitegrid')
plt.figure(figsize=(10,6))
plt.plot(all_times, diff_cum_count, linewidth=2)
plt.xlabel('time')
plt.ylabel('Cumulative Count Difference (DELETE_TX - ADD_RX)')
plt.show()

如果需要把结果保存为DataFrame后续处理:

diff_result = pd.DataFrame({
    'time': all_times,
    'cum_count_diff': diff_cum_count
})

内容的提问来源于stack exchange,提问作者Lucas Aimaretto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 02:51:01