-1

我有一个包含 4 种商品的每日销售数据集,这些商品在 4 个不同的特许经营店中销售。

我必须建立一个模型来预测所有特许经营权的所有这 4 种商品的每周销售额。

我打算使用一个基本模型进行预测

reg = linear_model.Ridge(alpha=1)

我的问题是如何编码以将该模型应用于所有 4 种产品和特许经营权。

我会感谢你的时间和精力来帮助我。谢谢

我的桌子如下

DepotName    Product    Date        SalesUnits  
    A           A1      2015-01-23  2.0 
    A           A2      2015-01-23  225.0   
    A           A3      2015-01-23  120.0   
    A           A4      2015-01-23  72.0    
    B           A1      2015-01-23  90.0    
    B           A2      2015-01-23  2.0 
    B           A3      2015-01-23  1.0 
    B           A4      2015-01-23  2.0 
    C           A1      2015-01-23  1.0 
    C           A2      2015-01-23  1.0 
    C           A3      2015-01-23  4.0 
    C           A4      2015-01-23  8040.0  
    D           A1      2015-01-23  1590.0  
    D           A2      2015-01-23  1.0     
    D           A3      2015-01-23  1590.0  
    D           A4      2015-01-23  1.0
    A           A1      2015-01-24  2.0 
    A           A2      2015-01-24  225.0   
    A           A3      2015-01-24  120.0   
    A           A4      2015-01-24  72.0    
    B           A1      2015-01-24  90.0    
    B           A2      2015-01-24  2.0 
    B           A3      2015-01-24  1.0 
    B           A4      2015-01-24  2.0 
    C           A1      2015-01-24  1.0 
    C           A2      2015-01-24  1.0 
    C           A3      2015-01-24  4.0 
    C           A4      2015-01-24  8040.0  
    D           A1      2015-01-24  1590.0  
    D           A2      2015-01-24  1.0     
    D           A3      2015-01-24  1590.0  
    D           A4      2015-01-24  1.0
4

1 回答 1

1

只需groupby在指标上运行一个操作:

for g in data.groupby(['DepotName', 'Product']):
   # g[0]: TUPLE OF CURRENT GROUP NAMES
   # g[1]: DATAFRAME OF CURRENT GROUP

   predictors = [... list of column names ...]

   reg = linear_model.Ridge(alpha=1)
   reg.fit(g[1][predictors], g[1]['SalesUnits'])

   y_pred = reg.predict(g[1][predictors])
   # ...
于 2018-05-19T18:30:21.663 回答