• Kaggle学习之M5 直接单步预测



    欲学此部分,需先看 时间序列问题案例分析Kaggle M5 Forecasting

    1. 读取数据

    开始分析之前,你需要知道

    • M5 比赛这个数据集信息
    • EDA常见策略
    • 仅仅用传统的时间序列,例如 Project 去训练模型,以及得到最后结果

    在本例中,我们将采用一个简单的数据集(sample_dataset.parquent)进行讲解,并应用到之前 EDA 结束之后保存的 M5 数据集上。

    import pandas as pd 
    import numpy as np
    # downcast就是在前面EDA notebook里的downcast函数的封装的包,如果没有安装请 pip install downcast
    from downcast import reduce
    pd.options.display.max_rows= 999
    pd.options.display.max_columns = 999
    
    calendar = pd.read_csv('../data/calendar.csv')
    sales_train_evaluation = pd.read_csv('../data/sales_train_evaluation.csv')
    sell_prices = pd.read_csv('../data/sell_prices.csv')
    
    example_df = pd.read_parquet('../data/sample_dataset.parquet')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2. 特征工程

    对于时间序列问题,尤其是销量预测类型的表格类数据,特征工程大都有以下几类:

    • 对于EDA发现的结果或者没有处理的部分进行处理(比如Nan值,在EDA过程中我们可能发现有Nan,在做特征第一步要把他们进行处理)
    • lag features
    • 针对lag features 的统计值,例如MA,一阶差分甚至二阶差分
    • 各维度上的统计值,例如各 item 维度上的售卖量之和;然后对这些值还能做一些比值类的特征,比如城市 A 占总全国销售量的百分比。
    • 将 date 列提取出时间相关的特征 dayofweek, month, year, isMonthEnd, isMonthStart

    对于特征工程里创建的特征,重点还是要思考它对于最后预测值的意义,一些比值,或者统计值是否能直接或者间接帮助到预测。

    2.1 结合EDA结果进行列处理

    可以看到这一步和EDA其实是相辅相成完成的,正常情况下不需要分隔成2个notebook

    处理 Nan

    • 将 Nan 处理成 ‘no_event’ 方便后面的类别型变量编码
    cat = ['event_name_1','event_type_1','event_name_2','event_type_2']
    for i in cat:
        calendar[i].fillna('no_event',inplace=True)
    
    • 1
    • 2
    • 3

    增加特征 在这里也是提取了很多时间相关的特征 (为什么?)

    • 增加 ‘is_weekend’ 特征,代表是否是周末
    • 增加 ‘month_day’,代表这是一个月的第几天
    • 增加 ‘month_week_number’,代表这是这个月的第几周
    • 增加 ‘events_per_day’,代表一天有几个events
    calendar['is_weekend'] = calendar['wday'].map(lambda x: 1 if x<=2 else 0) 
    calendar['is_weekend'] = calendar['is_weekend'].astype(np.int8)
    
    m = calendar["date"].tolist()
    m = [i.split("-")[2] for i in m]
    calendar["month_day"] = m
    calendar['month_day'] = calendar['month_day'].astype(np.int8)
    
    calendar['month_week_number'] = (calendar['month_day']-1) // 7 + 1 
    calendar['month_week_number'] = calendar['month_week_number'].astype(np.int8)
    
    calendar['events_per_day'] = calendar['event_type_1'].map(lambda x: 0 if x=='no_event' else 1) 
    index = calendar.index 
    indices = index[calendar['event_type_2']!='no_event'].tolist()
    for i in indices:
        calendar['events_per_day'][i] += 1
    calendar['events_per_day'] = calendar['events_per_day'].astype(np.int8)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    /Users/mikechen/anaconda3/envs/dingyan/lib/python3.7/site-packages/ipykernel_launcher.py:5: SettingWithCopyWarning:
    A value is trying to be set on a copy of a slice from a DataFrame
    See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
    “”"

  • 相关阅读:
    NodeJs版本过高无法启动Vue项目报错解决方法
    LeetCode54题:螺旋矩阵(python3)
    电流继电器JL-8GB/11/AC220V
    云服务器的安全设置常识
    Shell 和 Shell 脚本 (Shell Script)
    idea中的.idea文件夹以及*.iml文件(新版idea没有*.iml文件了),新旧版idea打开同一个项目会不会出现不兼容
    elasticsearch dsl集成python中indices.analyze方法参数analyzer的坑(调用es的analyze接口分析字符串)
    【附源码】Python计算机毕业设计某企业的采购管理系统优化设计
    【分布式任务调度】(二)XXL-JOB执行器配置及定时任务的创建
    go语言跨平台跨架构编译
  • 原文地址:https://blog.csdn.net/weixin_46713695/article/details/126333544