0

Hi I want to ask I have a code like this do decide RFM Score:

Whenever I tried to run it it always say

Exception has occurred: NameError name 'rfm_segmentation_final' is not defined

I've tried several solutions but it wouldnt work.

I want to print the final segmentation level.

Any help is appreciated!

Timesbooked = int(1)

Promoused = int(1)

Date = int(250)

input_predict = pd.DataFrame({
    'Frequency' : [Timesbooked],
    'Score' : [Promoused],
    'Recency' : [Date]
})

def RScore(x,p,d):
    if x <= d[p][0.25]:
        return 4
    elif x <= d[p][0.50]:
        return 3
    elif x <= d[p][0.75]: 
        return 2
    else:
        return 1
def FMScore(x,p,d):
    if x <= d[p][0.25]:
        return 1
    elif x <= d[p][0.50]:
        return 2
    elif x <= d[p][0.75]: 
        return 3
    else:
        return 4

    quantiles = pd.read_excel('aa.xlsx')
    quantiles1 = quantiles.quantile(q=[0.25,0.5,0.75])
    rfm_df = input_predict

   
    rfm_df['R_Quartile'] = rfm_df['Recency'].apply(RScore, args=('Recency',quantiles1,))
    rfm_df['F_Quartile'] = rfm_df['Frequency'].apply(FMScore, args=('Frequency',quantiles1,))
    rfm_df['M_Quartile'] = rfm_df['Score'].apply(FMScore, args=('Score',quantiles1,))

    rfm_segmentation_final = rfm_df

    #Creating RFM segmentation table

    rfm_segmentation_final['RFMScore'] = rfm_segmentation_final.R_Quartile.map(str) \
                                + rfm_segmentation_final.F_Quartile.map(str) \
                                + rfm_segmentation_final.M_Quartile.map(str)

    rfm_segmentation_final['RFM_Score'] = rfm_segmentation_final['R_Quartile'] + rfm_segmentation_final['F_Quartile'] + rfm_segmentation_final['M_Quartile']

def rfm_level(rfm_segmentation_final):
    if rfm_segmentation_final['RFM_Score'] >= 9:
        return "Opulence"
    elif ((rfm_segmentation_final['RFM_Score'] >= 8) and (rfm_segmentation_final['RFM_Score'] < 9)):
        return 'Champions'
    elif ((rfm_segmentation_final['RFM_Score'] >= 7) and (rfm_segmentation_final['RFM_Score'] < 8)):
        return 'Loyal'
    elif ((rfm_segmentation_final['RFM_Score'] >= 6) and (rfm_segmentation_final['RFM_Score'] < 7)):
        return 'Potential'
    elif ((rfm_segmentation_final['RFM_Score'] >= 5) and (rfm_segmentation_final['RFM_Score'] < 6)):
        return 'Promising'
    elif ((rfm_segmentation_final['RFM_Score'] >= 4) and (rfm_segmentation_final['RFM_Score'] < 5)):
        return 'Needs Attention'
    else:
        return 'Require Activation'
    

rfm_segmentation_final['RFM_Level'] = rfm_segmentation_final.apply(rfm_level, axis=1)
print(rfm_segmentation_final['RFM_Level'])

2
  • 3
    You define rfm_segmentation_final inside FMScore. It is local to that function, but you try to use it outside the function. Commented Dec 8, 2020 at 4:04
  • Please read, duplicate stackoverflow.com/questions/37955081/… Commented Dec 8, 2020 at 5:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.