Skip to main content
 首页 » 编程设计

python之将 R broom/dplyr 问题转化为 Python

2024年11月24日10mq0036

我的目标是将我在 R 中所做的工作移植到 Python。我知道那里有一些包可以满足我的需要,但是我很难将我通常在 R 中做的事情转换成需要在 Python 中做的事情。我正在处理的场景与此平行:R: Summarize inside an nlsLM() statement

我从以下 R 数据框开始:

structure(list(datetime = structure(c(1514782800, 1480568400,1504242000, 1509512400, 1509512400, 1485925200, 1517461200, 1485925200, 1501563600, 1467349200, 1472706000, 1454302800, 1483246800, 1498885200, 1506834000, 1477976400, 1483246800, 1477976400, 1509512400, 1496293200, 1451624400, 1454302800, 1454302800, 1464757200, 1498885200, 1517461200, 1462078800, 1506834000, 1522558800, 1483246800, 1501563600, 1451624400, 1485925200, 1501563600, 1451624400, 1517461200, 1475298000, 1480568400, 1512104400, 1456808400, 1477976400, 1475298000, 1517461200, 1459486800, 1501563600, 1477976400, 1506834000, 1506834000, 1451624400, 1483246800), class = c("POSIXct", "POSIXt"), tzone = ""), value = c(2863.27837518519, 2878.40382333333, 1236.74444444444, 3522.48888888889, 3522.48888888889, 2033.55555555556, 3305.5, 2033.55555555556, 2094.7037037037, 3052.91875740741, 2960.52222222222, 1733.7918262963, 2850.28673851852, 2841.40740740741, 3310.77538814815, 2266.26172851852, 2850.28673851852, 2266.26172851852, 3522.48888888889, 2802.55555555556, 2196.82556740741, 1733.7918262963, 1733.7918262963, 3001.43703703704, 2841.40740740741, 3305.5, 2061.4826762963, 3310.77538814815, 3107.01851851852, 2850.28673851852, 2094.7037037037, 2196.82556740741, 2033.55555555556, 2094.7037037037, 2196.82556740741, 3305.5, 2848.90322592593, 2878.40382333333, 2873.73476703704, 2208.64755074074, 2266.2172851852, 2848.90322592593, 3305.5, 2021.68765444444, 2094.7037037037, 2266.26172851852, 3310.77538814815, 3310.77538814815, 2196.82556740741, 2850.28673851852), mon = structure(c(2018, 2016.91666666667, 2017.66666666667, 2017.83333333333, 2017.83333333333, 2017.08333333333, 2018.08333333333, 2017.08333333333, 2017.58333333333, 2016.5, 2016.66666666667, 2016.08333333333, 2017, 2017.5, 2017.75, 2016.83333333333, 2017, 2016.83333333333, 2017.83333333333, 2017.41666666667, 2016, 2016.08333333333, 2016.08333333333, 2016.41666666667, 2017.5, 2018.08333333333, 2016.33333333333, 2017.75, 2018.25, 2017, 2017.58333333333, 2016, 2017.08333333333, 2017.58333333333, 2016, 2018.08333333333, 2016.75, 2016.91666666667, 2017.91666666667, 2016.16666666667, 2016.83333333333, 2016.75, 2018.08333333333, 2016.25,2017.58333333333,2016.83333333333, 2017.75, 2017.75, 2016, 2017), class = "yearmon"),  
MW = c(2.6142700774997, 4.02670249993547, 0.666666666666667,  
0.724114015571947, 4.07197668868287, 3.74122386862433, 3.30097429092907,  
3.84858110028323, 0.666666666666667, 0.666666666666667, 4.35000446878457,  
0.666666666666667, 0.666666666666667, 3.8371824280444, 0.825077317374,  
0.666666666666667, 4.028058457579, 0.666666666666667, 4.3378032532779,  
3.84270845997837, 1.40955788986009, 0.666666666666667, 0.666666666666667,  
4.05845600900597, 4.00664052392117, 4.0295346724872, 0.666666666666667,  
4.14159923664523, 4.231951299842, 3.9562222817766, 0.666666666666667,  
3.61602795165213, 0.666666666666667, 3.58079262746603, 4.12197770915903,  
4.2610646492437, 4.02152528469467, 1.0117763092792, 2.03648922832252,  
0.666666666666667, 0.666666666666667, 3.8042476910097, 3.91787334748133,  
0.666666666666667, 0.666666666666667, 0.89571472289964, 4.1530002677697,  
3.93733212731873, 0.710671314318797, 0.666666666666667)), .Names = c("datetime", "value", "mon", "MW"), row.names = c(39113L, 12946L, 4365L, 37505L, 36601L, 31055L, 39814L, 31433L, 32105L, 20668L, 18191L, 8328L, 10232L, 25689L, 35528L, 4577L, 10302L, 5146L, 37975L, 29670L, 28429L, 7932L, 8468L, 23120L, 25111L, 39699L, 24312L, 36246L, 1556L, 11068L, 33269L, 29163L, 31685L, 32419L, 29059L, 40618L, 16751L, 11737L, 34371L, 6001L, 4864L, 16413L, 40304L, 8716L, 33190L, 5399L, 35610L, 36462L, 28338L, 10371L), class = "data.frame") 

然后我使用 python 中的 rpy2 模块将其转换为 pandas,方法是使用以下代码获取包含此数据帧的 R 文件:

import rpy2.robjects as robjects 
from rpy2.robjects.packages import importr 
import rpy2.robjects.packages as rpackages 
base = importr('base') 
utils = importr('utils') 
 
r = robjects.r 
obj1, obj2 = r.source("python-testing_script.R") 
mod_data = pandas2ri.ri2py(obj1) 

现在我将 mod_data 数据框作为 Python 中的 pandas 数据框。我的目标是复制以下代码在 R 中的作用,但在 Python 中:

require(minpack.lm) 
require(tidyverse) 
require(broom) 
 
dat <- structure(...) # provided in the question 
 
predictions <-  
    dat %>%  
    ungroup %>% 
    mutate(row = row_number()) %>% 
    do(augment(nlsLM( 
                formula = value ~ a * MW^b + 0*row,  
                data = ., 
                start = list(a = 100000, b=1/3), 
                upper = c(Inf, 1),  
                lower = c(0, 1/5) 
               ) 
           ) 
       ) 
 
joined <-  
    dat %>% 
    mutate(row = row_number()) %>% 
    left_join(predictions, by=c('MW', 'value', 'row')) %>% 
    select(-row) 
 
joined %>% 
    group_by(mon) %>% 
    mutate(monthly_avg_prediction = mean(.fitted)) 

到目前为止,我已经尝试将 dfply 包与 pybroom 和 scipy.optimize.curve_fit 一起使用,但在尝试时我一直遇到不同类型的错误。

我的预期结果是 pandas 数据帧预测,并加入了与我从上述 R 代码获得的 R 数据帧平行的结果。在此先感谢您提供任何有用的建议!

编辑:

我的项目取得了一些进展,但现在我被困在使用 pybroom 的过程中。这是我在 python 中的内容:

import pandas as pd 
import numpy as np 
from dfply import * 
from scipy.optimize import curve_fit 
import pybroom as br 
 
def fitFunc(MW, a, b): return a * (MW**b) 
predictions = mod_data >> ungroup >> br.augment(curve_fit(fitFunc, xdat, ydat,  
                                                              p0 = np.array([100000,1/3]),  
                                                              bounds = ([0,.2],[1000000, 1]))[0]) 

但在这里我得到错误:Sorry, "augment" does not support this object type (<class 'numpy.ndarray'>)

所以我现在更具体的问题是如何将使用 curve_fit 制作的模型转换为 pybroom 可以运行 augment 和 tidy 的模型。谢谢!

请您参考如下方法:

https://pybroom.readthedocs.io/en/stable/notebooks/pybroom-example-multi-datasets-scipy-robust-fit.html

根据此链接中 pybroom 的文档,augment 不能与 cuve_fit 函数一起使用。您需要切换到使用 lmfit 或根本不使用 augment 部分。