lifetimes icon indicating copy to clipboard operation
lifetimes copied to clipboard

gamma_gamma_fitter.py:66: RuntimeWarning: divide by zero encountered in log

Open jhurliman opened this issue 7 years ago • 2 comments

The documentation describes frequency as the number of time periods containing repeat purchases. I'm converting transactional SQL data to RFM so frequency in my case is count(distinct date(date_trans)) - 1 as frequency. This results in a zero for all customers who only made a single purchase. When feeding these samples into the GammaGammaFitter, at https://github.com/CamDavidsonPilon/lifetimes/blob/master/lifetimes/fitters/gamma_gamma_fitter.py#L66 log(frequency) is calculated which causes a divide by zero runtime exception for those zero entries.

What is the correct fix for this?

  1. Remove all samples with no repeat purchases from the GammaGammaFitter data set.
  2. Change the definition of frequency for GammaGammaFitter to include the first purchase by adding 1 to the value.
  3. Something else?

jhurliman avatar Sep 18 '17 18:09 jhurliman

Hey @jhurliman, the correct answer is 1. The implemented model only allows for repeat purchases. The docs here do the filtering you suggest, too.

CamDavidsonPilon avatar Sep 19 '17 12:09 CamDavidsonPilon

@CamDavidsonPilon I am running into same RuntimeWarning: divide by zero encountered in log even though my data is filtered for frequency > 0.

returning_customers_summary = summary_data[summary_data['frequency'] > 0]
returning_customers_summary[['monetary_value', 'frequency']].corr()

ggf = lifetimes.GammaGammaFitter(penalizer_coef = 0.01)
ggf.fit(returning_customers_summary['frequency'], returning_customers_summary['monetary_value'])

summary_data['CLV_1year'] = ggf.customer_lifetime_value(
    transaction_prediction_model=model,
    frequency=summary_data['frequency'],
    recency=summary_data['recency'],
    T=summary_data['T'],
    monetary_value=summary_data['monetary_value'],
    time=12,
    discount_rate=0
).round(0)

Any thoughts?

adityaagarwal2001 avatar Jul 23 '20 20:07 adityaagarwal2001