Perhitungan Quartile RFM ANALYSIS

import numpy as np, pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import datetime as dt
from random import sample
from numpy.random import uniform
from math import isnan

import sklearn
from sklearn.preprocessing import StandardScaler

data2= pd.read_excel('F:/File datamining/Data zakat.xlsx')

#Menampilkan data

data2

transaksi = data2.groupby(['No.', 'Tanggal']).sum().reset_index()

transaksi.head()

import datetime as dt

NOW = dt.datetime(2022,4,12)

data2['Tgl'] = pd.to_datetime(data2['Tanggal'])

rfmTable = data2.groupby('NPWZ').agg({'Tanggal': lambda x: (NOW - x.max()).days, 'No.': lambda x: len(x), 'Nominal': lambda x: x.sum()})

rfmTable['Tanggal'] = rfmTable['Tanggal'].astype(int)

rfmTable.rename(columns={'Tanggal': 'recency',

                         'No.': 'frequency',

                         'Nominal': 'monetary_value'}, inplace=True)

rfmTable

quantiles = rfmTable.quantile(q=[0.25, 0.5, 0.75])

quantiles

(bisa bantu saya? maksud dari variabel x,p,dan d pada perhitungan quartile dibawah ini apa ya?)

def RScore(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

def FMScore(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

segmented_rfm['R'] = segmented_rfm['recency'].apply(RScore, args=('recency',quantiles,))

segmented_rfm['F'] = segmented_rfm['frequency'].apply(FMScore, args=('frequency',quantiles,))

segmented_rfm['M'] = segmented_rfm['monetary_value'].apply(FMScore, args=('monetary_value',quantiles,))

segmented_rfm

segmented_rfm['RFM_score'] = segmented_rfm[['R', 'F', 'M']].sum(axis=1)

segmented_rfm

avatar DelaHanafia
@DelaHanafia

1 Kontribusi 0 Poin

Dipost 2 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban