如何在python中为不同的兴趣类别应用一个模型?

时间:2018-05-19 13:47:55

标签: python pandas multiple-models linearmodels

我有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

1 个答案:

答案 0 :(得分: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])
   # ...