特征工程-时间周期处理

我们有一个带有日期类型列的 pandas 数据帧。利用这一列,我们可以创建以下特征:

  • 年中的周
  • 星期
  • 周末
  • 小时
  • 还有更多
1
2
3
4
5
6
7
8
9
10
11
12
# 添加'year'列,将 'datetime_column' 中的年份提取出来
df.loc[:, 'year'] = df['datetime_column'].dt.year
# 添加'weekofyear'列,将 'datetime_column' 中的周数提取出来
df.loc[:, 'weekofyear'] = df['datetime_column'].dt.weekofyear
# 添加'month'列,将 'datetime_column' 中的月份提取出来
df.loc[:, 'month'] = df['datetime_column'].dt.month
# 添加'dayofweek'列,将 'datetime_column' 中的星期几提取出来
df.loc[:, 'dayofweek'] = df['datetime_column'].dt.dayofweek
# 添加'weekend'列,判断当天是否为周末
df.loc[:, 'weekend'] = (df.datetime_column.dt.weekday >=5).astype(int)
# 添加 'hour' 列,将 'datetime_column' 中的小时提取出来
df.loc[:, 'hour'] = df['datetime_column'].dt.hour

有时在处理时间序列问题时,您可能需要的特征不是单个值,而是一系列值。 例如,客户在特定时间段内的交易。在这种情况下,我们会创建不同类型的特征,例如:使用数值特征时,在对分类列进行分组时,会得到类似于时间分布值列表的特征。在这种情况下,您可以创建一系列统计特征,例如

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
27
import 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
2
3
4
5
6
7
8
9
10
11
from tsfresh.feature_extraction import feature_calculators as fc
# 计算 x 数列的绝对能量(abs_energy),并将结果存储在 feature_dict 字典中的 'abs_energy' 键下
feature_dict['abs_energy'] = fc.abs_energy(x)
# 计算 x 数列中高于均值的数据点数量,将结果存储在 feature_dict 字典中的 'count_above_mean' 键下
feature_dict['count_above_mean'] = fc.count_above_mean(x)
# 计算 x 数列中低于均值的数据点数量,将结果存储在 feature_dict 字典中的 'count_below_mean' 键下
feature_dict['count_below_mean'] = fc.count_below_mean(x)
# 计算 x 数列的均值绝对变化(mean_abs_change),并将结果存储在 feature_dict 字典中的 'mean_abs_change' 键下
feature_dict['mean_abs_change'] = fc.mean_abs_change(x)
# 计算 x 数列的均值变化率(mean_change),并将结果存储在 feature_dict 字典中的 'mean_change' 键下
feature_dict['mean_change'] = fc.mean_change(x)
-------------本文结束感谢您的阅读-------------