python - 使具有不同采样率的两个时间序列具有可比性
问题描述
我有 2 组数据,两个时间序列都是可变的(在两种情况下都相同)与时间,我已经使用 pandas 和 matplotlib 导入并绘制了它们。
from os import chdir
chdir('C:\\Users\\me\\Documents\\Folder')
# import necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# read in csv file
file_df = pd.read_csv('C://Users//me//Documents//Folder//file.csv')
# define csv columns and assign values
VarA = file_df.loc[:, 'VarA'].values
TimeA = file_df.loc[:, 'TimeA'].values
VarB = file_df.loc[:, 'VarB'].values
TimeB = file_df.loc[:, 'TimeB'].values
# plot data selection and aesthetics
plt.plot(TimeA, VarA)
plt.plot(TimeB, VarB)
# plot labels
plt.xlabel('Time')
plt.ylabel('Variable')
#plot and add legend based on plot labels
plt.legend()
在这两种情况下,变量都在 0 分钟到 320 分钟之间进行采样。但是,一个数据集有 775 个样本(在 320 分钟内以随机间隔采集),而另一个数据集有 1732 个样本(同样,在 320 分钟内以随机间隔采集)。
本质上,我想做的是基于旧数据集制作两个新数据集,其中我的变量 vs 时间在 0 到 320 分钟之间,但两者都具有相同数量的变量 A 在相同时间步骤中获取的数据点(例如320 个样品每分钟变化一次)。
我猜需要一些插值?我真的不知道从哪里开始。我在同一个 .csv 中有两个数据集,我需要它们具有相同的样本大小,以便我可以运行以下计算。目前它没有运行,因为 'VarA' 和 'VarB' 的数据量不同。
x_values = VarB
y_values = VarA
correlation_matrix = np.corrcoef(x_values, y_values)
correlation_xy = correlation_matrix[0,1]
r_squared = correlation_xy**2
解决方案
我认为resample在这里可能有用。
推荐阅读
- swift - 在标签中设置和动画日期时间
- javascript - RegEx - 如果存在内部 HTML 标记,则不匹配
- java - Fragment android中的类
- swift - 如何以编程方式在 UIScrollView 中设置 StickyHeader
- python - 检测图像中最常用的颜色
- sql-server - SQL Server 中的索引性能
- javascript - Puppeteer:关闭地理定位权限请求
- r - 如何形成基于词汇表的 tfidf sparklyr 数据框
- vba - Excel VBA 注释更新
- laravel-5 - Laravel 雄辩的更改日期时间戳 GLOBAL 到 dmY H:i:s