【特征工程-时许(时间)特征处理方法汇总】

这篇具有很好参考价值的文章主要介绍了【特征工程-时许(时间)特征处理方法汇总】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

时间特征基本处理

# 时间特征的基本转换
class HandleDateFeature:
    def __init__(self, df, col):
        self.df = df
        self.col = col

    def datetime_transfrom(self):
        df[self.col + '_year'] = df[self.col].dt.year
        df[self.col + '_quarter'] = df[self.col].dt.quarter
        df[self.col + '_month'] = df[self.col].dt.month
        df[self.col + '_week'] = df[self.col].dt.weekofyear
        df[self.col + '_day'] = df[self.col].dt.dayofyear
        df[self.col + '_month_day'] = df[self.col].dt.day
        df[self.col + '_dayofweek'] = df[self.col].dt.dayofweek
        df[self.col + '_hour'] = df[self.col].dt.hour
        df[self.col + '_minute'] = df[self.col].dt.minute
        df[self.col + '_second'] = df[self.col].dt.second

        return df

    def date_isbegin(self):
        df[self.col + '_year_start'] = df[self.col].dt.is_year_start
        df[self.col + '_year_end'] = df[self.col].dt.is_year_end
        df[self.col + '_quarter_start'] = df[self.col].dt.is_quarter_start
        df[self.col + '_quarter_end'] = df[self.col].dt.is_quarter_end
        df[self.col + '_month_start'] = df[self.col].dt.is_month_start
        df[self.col + '_month_end'] = df[self.col].dt.is_month_end

        return df

基本处理基础上二次处理

class TwoHandleDateFeature:
    def __init__(self, df, col):
        self.df = df
        self.col = col

    # 统计年、月、季度、天、时、分、秒、是否周末 周期特征
    def cycle_fea(self):
        df[self.col + '_sin_month'] = df[self.col + '_month'].apply(lambda x: np.sin(np.pi*2/12 * x))
        df[self.col + '_cos_month'] = df[self.col + '_month'].apply(lambda x: np.cos(np.pi * 2 / 12 * x))
        df[self.col + '_sin_month_day'] = df[self.col + '_month_day'].apply(lambda x: np.sin(np.pi * 2 / 30 * x))
        df[self.col + '_cos_month_day'] = df[self.col + '_month_day'].apply(lambda x: np.cos(np.pi * 2 / 30 * x))
        df[self.col + '_sin_week'] = df[self.col + '_week'].apply(lambda x: np.sin(np.pi * 2 / 52 * x))
        df[self.col + '_cos_week'] = df[self.col + '_week'].apply(lambda x: np.cos(np.pi * 2 / 52 * x))
        df[self.col + '_sin_day'] = df[self.col + '_day'].apply(lambda x: np.sin(np.pi * 2 / 365 * x))
        df[self.col + '_cos_day'] = df[self.col + '_day'].apply(lambda x: np.cos(np.pi * 2 / 365 * x))
        df[self.col + '_sin_dayofweek'] = df[self.col + '_dayofweek'].apply(lambda x: np.sin(np.pi * 2 / 7 * x))
        df[self.col + '_cos_dayofweek'] = df[self.col + '_dayofweek'].apply(lambda x: np.cos(np.pi * 2 / 7 * x))
        df[self.col + '_sin_hour'] = df[self.col + '_hour'].apply(lambda x: np.sin(np.pi * 2 / 24 * x))
        df[self.col + '_cos_hour'] = df[self.col + '_hour'].apply(lambda x: np.cos(np.pi * 2 / 24 * x))
        df[self.col + '_sin_minute'] = df[self.col + '_minute'].apply(lambda x: np.sin(np.pi * 2 / 60 * x))
        df[self.col + '_cos_minute'] = df[self.col + '_minute'].apply(lambda x: np.sin(np.pi * 2 / 60 * x))

        df[self.col + '_weekend'] = np.where(df[self.col + '_dayofweek'].isin([5, 6]), 1, 0)

        return df

    # 工作、努力、卷王、黎明、清晨、早上、上午、中午、下午、傍晚、晚上、深夜  划分
    def part_day(self):
        df[self.col + '_work_hours'] = np.where(df[self.col + '_hour'].isin([9, 10, 11, 14, 15, 17]), 1, 0)
        df[self.col + '_early_bird__hours'] = np.where(df[self.col + '_hour'].isin([8, 18]), 1, 0)
        df[self.col + '_blackleg__hours'] = np.where(df[self.col + '_hour'].isin([7, 19, 20, 21]), 1, 0)
        df[self.col + '_dawn_hours'] = np.where(df[self.col + '_hour'].isin([4, 5]), 1, 0)
        df[self.col + '_early_morning_hours'] = np.where(df[self.col + '_hour'].isin([6, 7]), 1, 0)
        df[self.col + '_later_morning_hours'] = np.where(df[self.col + '_hour'].isin([8, 9, 10]), 1, 0)
        df[self.col + '_noon_hours'] = np.where(df[self.col + '_hour'].isin([11, 12, 13]), 1, 0)
        df[self.col + '_afternoon_hours'] = np.where(df[self.col + '_hour'].isin([14, 15, 16]), 1, 0)
        df[self.col + '_evening_hours'] = np.where(df[self.col + '_hour'].isin([17, 18, 19]), 1, 0)
        df[self.col + '_night_hours'] = np.where(df[self.col + '_hour'].isin([20, 21, 22]), 1, 0)
        df[self.col + '_midnight_hours'] = np.where(df[self.col + '_hour'].isin([23, 24, 1, 2, 3]), 1, 0)

        return df

差分、滞后、滑窗、指数加权

class DateShiftRollingEwm:
    def __init__(self, df, col,  group, windows=None, alpha=None, shift=None):
        self.df = df
        self.col = col
        self.shift = shift
        self.windows = windows
        self.alpha = alpha
        # group---> list
        self.group = group

    def diffs(self):
        df[self.col + '_diff_1'] = df.groupby(self.group)[self.col].diff(periods=1)
        df[self.col + '_diff_2'] = df.groupby(self.group)[self.col + '_diff_1'].diff(periods=1)

        return df

    def shifts(self):
        df[self.col + '_shift_' + str(self.shift)] = df.groupby(self.group)[self.col].shift(
            self.shift).fillna(method='ffill').reset_index().sort_index().set_index('index')

        return df

    def rollings(self):
        """
        DataFrame.rolling(window, min_periods=None, freq=None, center=False, win_type=None, on=None, axis=0,
                          closed=None)

        - min_periods: 最少需要有值的观测点的数量, 对于int类型,默认与window相等
        - center: 把窗口的标签设置为居中, 布尔型, 默认False
        - win_type: 窗口的类型, 截取窗的各种函数。字符串类型,默认为None
        - on: 可选参数, 对于dataframe而言,指定要计算滚动窗口的列, 值为列名
        - closed:定义区间的开闭,支持int类型的window, 对于offset类型默认是左开右闭的即默认为right, 可以根据情况指定为left、both等
        - axis:方向(轴), 一般都是0
        """
        df[self.col + '_rolling_' + str(self.windows) + '_mean'] = df.groupby(self.group)[self.col].transform(
            lambda x: x.shift(1).rolling(window=self.windows, min_periods=3, win_type="triang").mean()).values.tolist()
        df[self.col + '_rolling_' + str(self.windows) + '_max'] = df.groupby(self.group)[self.col].transform(
            lambda x: x.shift(1).rolling(window=self.windows, min_periods=3).max()).values.tolist()
        df[self.col + '_rolling_' + str(self.windows) + '_min'] = df.groupby(self.group)[self.col].transform(
            lambda x: x.shift(1).rolling(window=self.windows, min_periods=3).min()).values.tolist()
#         df[self.col + '_rolling_' + str(self.windows) + '_std'] = df.groupby(self.group)[self.col].transform(
#             lambda x: x.shift(1).rolling(window=self.windows, min_periods=3, win_type="triang").std()).values.tolist()
#         df[self.col + '_rolling_' + str(self.windows) + '_skew'] = df.groupby(self.group)[self.col].transform(
#             lambda x: x.shift(1).rolling(window=self.windows, min_periods=3).skew()).values.tolist()
#         df[self.col + '_rolling_' + str(self.windows) + '_kurt'] = df.groupby(self.group)[self.col].transform(
#             lambda x: x.shift(1).rolling(window=self.windows, min_periods=3).kurt()).values.tolist()
#         df[self.col + '_rolling_' + str(self.windows) + '_quantile'] = df.groupby(self.group)[self.col].transform(
#             lambda x: x.rolling(window=self.windows, min_periods=3).quantile()).values.tolist()
#         df[self.col + '_rolling_' + str(self.windows) + '_corr'] = df.groupby(self.group)[self.col].transform(
#             lambda x: x.shift(1).rolling(window=self.windows, min_periods=3).corr()).values.tolist()

        return df

    def ewms(self):
        """
        DataFrame.ewm(self, com=None, span=None, halflife=None, alpha=None, min_periods=0, adjust=True, ignore_na=False, axis=0)
        com :  float,可选,根据质心指定衰减, α=1/(1+com), for com≥0。
        span :  float,可选,根据范围指定衰减, α=2/(span+1), for span≥1。
        halflife :  float,可选,根据半衰期指定衰减, α=1−exp(log(0.5)/halflife),forhalflife>0。
        alpha :  float,可选,直接指定平滑系数α, 0<α≤1。0.18.0版中的新功能。
        min_periods : int,默认0,窗口中具有值的最小观察数(否则结果为NA)。
        adjust : bool,默认为True,除以开始阶段的衰减调整因子,以解释相对权重的不平衡(将EWMA视为移动平均线)。
        ignore_na : bool,默认为False,计算权重时忽略缺失值;指定True可重现0.15.0之前的行为。
        axis : {0或'index',1或'columns'},默认0,要使用的轴。值0标识行,值1标识列。
        只能提供一个参数: com, span, halflife, 和 alpha 四个参数中有且仅有一个参数可被设置(不支持2个或2个以上的设置)。
        可供使用指数加权函数有:mean(), var(), std(), corr(), cov()
        """
        df[self.col + '_ewm_' + str(self.windows) + '_mean'] = df.groupby(self.group)[self.col].transform(
            lambda x: x.shift(1).ewm(alpha=self.alpha).mean()).values.tolist()
        df[self.col + '_ewm_' + str(self.windows) + '_std'] = df.groupby(self.group)[self.col].transform(
            lambda x: x.shift(1).ewm(alpha=self.alpha).std()).values.tolist()
        df[self.col + '_ewm_' + str(self.windows) + '_corr'] = df.groupby(self.group)[self.col].transform(
            lambda x: x.shift(1).ewm(alpha=self.alpha).corr()).values.tolist()

        return df

参考文献:https://zhuanlan.zhihu.com/p/466773545

文章来源地址https://www.toymoban.com/news/detail-723988.html

到了这里,关于【特征工程-时许(时间)特征处理方法汇总】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 机器学习中高维组合特征的处理方法+推荐系统使用矩阵分解为用户推荐的原理解析,《百面机器学习》学习笔记

    为了提高复杂关系的拟合能力,在特征工程中经常会把一阶离散特征进行组合,构成高阶组合特征。 假设有A B两组特征,C为受到A B两种特征影响的因素,且对特征A来说,其有 A i , i ∈ [ 0 , 1 ] {A^i,iin [0,1]} A i , i ∈ [ 0 , 1 ] 两种特征取值。同时,对于特征B来说,其有 B j , j ∈

    2024年02月05日
    浏览(35)
  • 机器学习基础之《特征工程(2)—特征工程介绍、特征抽取》

    一、什么是特征工程 机器学习领域的大神Andrew Ng(吴恩达)老师说“Coming up with features is difficult, time-consuming, requires expert knowledge. “Applied machine learning” is basically feature engineering. ” 注:业界广泛流传:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已

    2024年02月13日
    浏览(26)
  • 【机器学习】特征工程 - 字典特征提取

    「作者主页」: 士别三日wyx 「作者简介」: CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」: 对网络安全感兴趣的小伙伴可以关注专栏《网络安全入门到精通》 特征工程就是从 「原始数据」 中提取 「特征」 ,以供 「算法」 和 「模型

    2024年02月11日
    浏览(37)
  • 机器学习重要内容:特征工程之特征抽取

    目录 1、简介 2、⭐为什么需要特征工程 3、特征抽取 3.1、简介 3.2、特征提取主要内容 3.3、字典特征提取 3.4、\\\"one-hot\\\"编码 3.5、文本特征提取 3.5.1、英文文本 3.5.2、结巴分词 3.5.3、中文文本 3.5.4、Tf-idf ⭐所属专栏:人工智能 文中提到的代码如有需要可以私信我发给你噢😊 特

    2024年02月12日
    浏览(30)
  • 【机器学习】特征工程 - 文本特征提取TfidfVectorizer

    「作者主页」: 士别三日wyx 「作者简介」: CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」: 对网络安全感兴趣的小伙伴可以关注专栏《网络安全入门到精通》 对 「文本」 进行特征提取时,一般会用 「单词」 作为特征,即特征词。

    2024年02月12日
    浏览(27)
  • python机器学习——机器学习相关概念 & 特征工程

    监督学习:输入数据有特征有标签,即有标准答案 分类:k-近邻算法、贝叶斯分类、决策树与随机森林、逻辑回归、神经网络 回归:线性回归、岭回归 标注:隐马尔可夫模型 (不做要求) 无监督学习:输入数据有特征无标签,即无标准答案 聚类:k-means 特征工程是将原始数据

    2024年02月11日
    浏览(41)
  • 机器学习特征工程学习笔记(一)

            机器学习特征工程是指在机器学习任务中对原始数据进行转换、提取和选择,以创建更有效、更具有表征能力的特征的过程。良好的特征工程可以显著提升模型的性能,并帮助解决数据中存在的各种问题。         以下是一些常见的机器学习特征工程技术:

    2024年02月11日
    浏览(32)
  • 机器学习基础之《特征工程(4)—特征降维》

    一、什么是特征降维 降维是指在某些限定条件下,降低随机变量(特征)个数,得到一组“不相关”主变量的过程 1、降维 降低维度 ndarry     维数:嵌套的层数     0维:标量,具体的数0 1 2 3...     1维:向量     2维:矩阵     3维:多个二维数组嵌套     n维:继续嵌套

    2024年02月13日
    浏览(30)
  • 机器学习-特征工程

    1.1 什么是特征 数值特征(连续特征)、文本特征(离散特征) 1.2 特征的种类  1.3 特征工程 特征是机器学习可疑直接使用的,模型和特征之间是一个循环过程; 实际上特征工程就是将原始数据处理成机器学习可以直接使用数据的过程; 特征工程,降噪、将特征转化为数字

    2024年02月06日
    浏览(29)
  • 机器学习——特征工程

    对于机器学习特征工程的知识,你是怎样理解“特征” 在机器学习中,特征(Feature)是指从原始数据中提取出来的、用于训练和测试机器学习模型的各种属性、变量或特点。特征可以是任何类型的数据,例如数字、文本、图像、音频等等。 特征工程是机器学习中非常重要的

    2024年02月05日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包