简体   繁体   English

在各个行上迭代 function 直到满足条件,然后继续下一行

[英]Iterate a function over individual rows until condition is met, then move on to the next row

I am working with the lifetimes library to build a customer lifetime value model.我正在使用生命周期库来构建客户生命周期价值 model。 The library comes with a method called conditional_expected_number_of_purchases_up_to_time that allows you to predict purchases for each customer in your data set over a specified time period.该库附带一个名为conditional_expected_number_of_purchases_up_to_time的方法,该方法允许您预测数据集中每个客户在指定时间段内的购买情况。

Here is the dataframe I am working with:这是我正在使用的 dataframe:

df = pd.DataFrame([[aaa@email.com, 6.0, 112.0, 139.0], [bbb@email.com, 11.0, 130.0, 130.0]], columns=['email', 'frequency', 'recency', 'T'])

Each row in the dataframe represents an individual customer. dataframe 中的每一行代表一个单独的客户。 To predict the number of expected purchases for each customer over the next 4 periods, I would execute the following code:为了预测每个客户在接下来的 4 个时期内的预期购买次数,我将执行以下代码:

t = 4
df['est_purchases'] = mbgf.conditional_expected_number_of_purchases_up_to_time(t, df['frequency'], df['recency'], df['T'])

What I would like to do now is, for each row in the dataframe, approximate the total number of remaining purchases over the rest of their lifetime.我现在想做的是,对于 dataframe 中的每一行,近似于 rest 在其生命周期内的剩余购买总数。 Let's call this quantity Residual Customer Purchases (RCP).我们将此数量称为剩余客户购买量 (RCP)。

To do this, I have defined two functions: the first calculates the incremental RCP between two time periods and the second function approximates the total RCP by incrementally increasing t until the incremental RCP falls below a specific tolerance level:为此,我定义了两个函数:第一个函数计算两个时间段之间的增量 RCP,第二个 function 通过递增t来近似总 RCP,直到增量 RCP 低于特定容差水平:

## Function to calculate incremental RCP
    def RCP(row):
        dif = (mbgf.conditional_expected_number_of_purchases_up_to_time(t, 
              row['frequency'], row['recency'], row['T'])
            - mbgf.conditional_expected_number_of_purchases_up_to_time((t-1), 
              row['frequency'], row['recency'], row['T']))
        return dif
    
## Create column for incremental RCP
    df['m_RCP'] = df.apply(RCP, axis = 1)

   ## Function to approximate total RCP 
    def approximate(fn, model, rfm, t=1, eps_tol=1e-6, eps=0, **kwargs):
        eps = 0
        cf = 0
        while True:
            cf += df.apply(fn, axis = 1)
            if(cf - eps < eps_tol):
                break
            eps = cf; t+=1
       return cf
    
## Create column for total RCP
    df['t_RCP'] = df.apply(approximate(RCP, model = mbgf, rfm = df), axis = 1)

The first function is working as expected.第一个 function 按预期工作。 But when I try to execute the second function ( approximate ) I get this error:但是当我尝试执行第二个 function ( approximate )时,我收到此错误:

ValueError: The truth value of a Series is ambiguous. ValueError:Series 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

I want the approximate function to iterate the RCP function for a single row until the RCP value no longer increases, and do this one by one for each row in the dataframe.我想要近似的 function 对单行迭代 RCP function 直到 RCP 值不再增加,然后对 Z6A8064B5DF479455500553C47ZC 中的每一行逐一执行此操作。

What am I doing wrong and what should I be doing instead?我做错了什么,我应该怎么做?

You are calling df.apply(fn, axis=1) which returns a series that you assign to cf .您正在调用df.apply(fn, axis=1) ,它返回您分配给cf的系列。 Then you're comparing the series cf - eps to a constant, which returns an array of booleans.然后您将系列cf - eps与一个常量进行比较,该常量返回一个布尔数组。 An array of booleans is ambiguous to use in a conditional expression which is what causes the error.布尔数组在条件表达式中使用不明确,这是导致错误的原因。

What I would do is define a function iterated_RCP(row) that takes as input a row of the dataframe and iterates RCP on that row until it converges.我要做的是定义一个 function iterated_RCP iterated_RCP(row) ,它将 dataframe 的一行作为输入,并在该行上迭代 RCP 直到它收敛。 Then you can do something like df.assign(t_RCP=df.apply(iterated_RCP, axis=1)) .然后你可以做类似df.assign(t_RCP=df.apply(iterated_RCP, axis=1))的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最蟒蛇的方式不断地遍历列表直到满足条件? - Most pythonic way to continually iterate over a list until a condition is met? 在条件满足之前迭代列表的最pythonic方法是什么? - What is the most pythonic way to iterate over a list until a condition is met? Python 循环遍历数据帧行,直到第一次满足条件 - Python loop over dataframe rows until condition is met the first time Dataframe 如果在 dataframe 列上满足条件,那么在 dataframe 中获取这一行和接下来的 3 行? - Dataframe if a condition is met on a dataframe column then get this row and the next 3 rows in a dataframe? 如何迭代直到满足python for循环中的条件 - How to iterate until a condition is met in python for loop 迭代无限生成器直到满足条件 - iterate infinite generator until condition is met 有没有办法迭代for循环直到满足条件? (Python) - Is there a way to iterate a for loop until a condition is met? (python) 分别遍历 df 行和两列的总和值,直到其中一列满足条件 - Iterate through df rows and sum values of two columns separately until condition is met on one of those columns PYTHON:连续遍历行,并根据行中的值更改值,直到满足条件 - PYTHON: Continually iterating over row(s) and changing values based on values within the row until condition is met 如何迭代 function 以便将 output 作为参数迭代传递,直到满足条件 - How to iterate function so that output is passed as argument iteratively until condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM