我们有一个带有日期类型列的 pandas 数据帧。利用这一列,我们可以创建以下特征:
- 年
- 年中的周
- 月
- 星期
- 周末
- 小时
- 还有更多
1 | # 添加'year'列,将 'datetime_column' 中的年份提取出来 |
有时在处理时间序列问题时,您可能需要的特征不是单个值,而是一系列值。 例如,客户在特定时间段内的交易。在这种情况下,我们会创建不同类型的特征,例如:使用数值特征时,在对分类列进行分组时,会得到类似于时间分布值列表的特征。在这种情况下,您可以创建一系列统计特征,例如1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27import numpy as np
# 创建字典,用于存储不同的统计特征
feature_dict = {}
# 计算 x 中元素的平均值,并将结果存储在 feature_dict 中的 'mean' 键下
feature_dict['mean'] = np.mean(x)
# 计算 x 中元素的最大值,并将结果存储在 feature_dict 中的 'max' 键下
feature_dict['max'] = np.max(x)
# 计算 x 中元素的最小值,并将结果存储在 feature_dict 中的 'min' 键下
feature_dict['min'] = np.min(x)
# 计算 x 中元素的标准差,并将结果存储在 feature_dict 中的 'std' 键下
feature_dict['std'] = np.std(x)
# 计算 x 中元素的方差,并将结果存储在 feature_dict 中的 'var' 键下
feature_dict['var'] = np.var(x)
# 计算 x 中元素的差值,并将结果存储在 feature_dict 中的 'ptp' 键下
feature_dict['ptp'] = np.ptp(x)
# 计算 x 中元素的第10百分位数(即百分之10分位数),并将结果存储在 feature_dict 中的 'percentile_10' 键下
feature_dict['percentile_10'] = np.percentile(x, 10)
# 计算 x 中元素的第60百分位数,将结果存储在 feature_dict 中的 'percentile_60' 键下
feature_dict['percentile_60'] = np.percentile(x, 60)
# 计算 x 中元素的第90百分位数,将结果存储在 feature_dict 中的 'percentile_90' 键下
feature_dict['percentile_90'] = np.percentile(x, 90)
# 计算 x 中元素的5%分位数(即0.05分位数),将结果存储在 feature_dict 中的 'quantile_5' 键下
feature_dict['quantile_5'] = np.quantile(x, 0.05)
# 计算 x 中元素的95%分位数(即0.95分位数),将结果存储在 feature_dict 中的 'quantile_95' 键下
feature_dict['quantile_95'] = np.quantile(x, 0.95)
# 计算 x 中元素的99%分位数(即0.99分位数),将结果存储在 feature_dict 中的 'quantile_99' 键下
feature_dict['quantile_99'] = np.quantile(x, 0.99)
时间序列数据(数值列表)可以转换成许多特征。在这种情况下,一个名为 tsfresh 的 python 库非常有用。
1 | from tsfresh.feature_extraction import feature_calculators as fc |