# RFM Analysis for Effective Segmentation Using Python

Table of Contents

## Introduction

In this article, we explore the powerful technique of RFM analysis for customer segmentation using Python programming. RFM analysis can provide valuable insights into customer behavior, allowing businesses to segment their customer base and develop targeted retention strategies. We cover the key steps involved in conducting RFM analysis, including data preparation, RFM score calculation, and segmentation. We also demonstrate how to visualize the results using Python libraries such as Pandas, Matplotlib, and Seaborn. By the end of this article, you will have a solid understanding of how to use RFM analysis to gain valuable customer insights and develop effective segmentation strategies.

In this article, we will use Python to perform RFM analysis on a dataset of online retail transactions. The dataset can be found at the following link: https://archive.ics.uci.edu/ml/machine-learning-databases/00352/Online%20Retail.xlsx

First, we will import the necessary libraries and load the dataset:

(406829, 8)

Next, we will clean and preprocess the data. We will remove rows with no CustomerID

and duplicate data.

(406829, 8)

Next we will filter top ten country from where customer purchase.

354321 rows × 5 columns

354321 rows × 6 columns

`(Timestamp('2010-12-01 08:26:00'), Timestamp('2011-12-09 12:49:00'))`

354321 rows × 6 columns

## RFM Analysis

Here, you are going to perform following opertaions:

• For Recency, Calculate the number of days between present date and date of last purchase each customer.
• For Frequency, Calculate the number of orders for each customer.
• For Monetary, Calculate sum of purchase price for each customer.

3920 rows × 3 columns

3920 rows × 3 columns

3920 rows × 3 columns

## Computing Quantile of RFM values

Customers with the lowest recency, highest frequency and monetary amounts considered as top customers.

We will do it using Data binning .

```111    409
444    345
211    186
433    178
344    169
...
241      7
141      5
431      4
413      4
114      1
Name: RFM_Score, Length: 61, dtype: int64```

Middle 1604

Low 1176

Top 1140

Name: RFM_Level, dtype: int64

## Segmentation using k-means clustering (unsupervised)

K-means assumptions:

Symmetric distribution of features/variables
Variables with same average values
Variables with same variance

Index([‘recency’, ‘frequency’, ‘monetary’, ‘r_quartile’, ‘f_quartile’, ‘m_quartile’, ‘RFM_Score’, ‘RFM_Score_num’, ‘RFM_Level’], dtype=’object’)

mean :

recency 91.742092

frequency 90.388010

monetary 1863.899745

dtype: float64

std:

recency 99.533485

frequency 217.808385

monetary 7482.810958

dtype: float64

Apply transformer to remove skewness

Apply Standard Scaler for same mean and variance

3920 rows × 4 columns

3920 rows × 4 columns

mean :

recency 0

frequency 0

monetary 0

dtype: int64

std:

recency 1

frequency 1

monetary 1

dtype: int64

Now our features are ready. We will apply k-mean clustering. Apply k-means now

/usr/local/lib/python3.8/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to ‘auto’ in 1.4. Set the value of `n_init` explicitly to suppress the warning warnings.warn(

3920 rows × 10 columns

1 1624

2 1218

0 1078

Name: cluster_labels, dtype: int64

Find best k value

` `

```{1: 11760.000000000013,
2: 6080.567689041685,
3: 4709.937307327478,
4: 3870.116603724802,
5: 3319.490869277227,
6: 2916.5075068200385,
7: 2655.5748530820592,
8: 2459.2276248472426,
9: 2299.9953369555833,
10: 2154.4760094876638,
11: 2016.785702586328,
12: 1896.2115029782149,
13: 1814.3350578718046,
14: 1733.105393862509,
15: 1664.5963048437225,
16: 1597.9977424250183,
17: 1531.5951097080115,
18: 1489.225635962716,
19: 1436.2061701139564,
20: 1391.496724909009}```

3920 rows × 7 columns

Apply k-means clustering

/usr/local/lib/python3.8/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to ‘auto’ in 1.4. Set the value of `n_init` explicitly to suppress the warning warnings.warn(

0 1086

1 1021

2 928

3 885

Name: cluster_labels, dtype: int64

`<AxesSubplot:xlabel='PC_1', ylabel='PC_2'>`

## Customer Cluster

Based on the RFM analysis that we conducted using the Online Retail dataset, we can identify several customer segments and develop retention policies for each of them. Here are some examples:

High-value (111): These are customers who have a high monetary value, high purchase frequency, and made a purchase recently. To retain these customers, businesses can offer them loyalty programs, special discounts, and personalized communication. Since these customers are already loyal and have a high spending potential, businesses can focus on building long-term relationships with them to ensure their continued loyalty.

New customers(144): These are customers who have made their first purchase recently. To retain these customers, businesses can offer them welcome discounts, personalized recommendations, and a seamless buying experience. Since these customers are still testing the waters and have not yet formed any brand loyalty, businesses need to focus on building a positive first impression to encourage repeat purchases.

At-risk customers(441): These are customers who have not made a purchase in a long time but have a high historical value. To retain these customers, businesses can offer them win-back campaigns, personalized offers, and targeted communication. Since these customers have not made a purchase in a while, it is important to remind them of the value that the business can offer and incentivize them to make another purchase.

Low-value customers(444): These are customers who have a low monetary value, low purchase frequency, and have not made a purchase recently. To retain these customers, businesses can offer them targeted promotions, personalized recommendations, and a simplified buying experience. Since these customers have not yet demonstrated a high spending potential, businesses need to focus on building loyalty by providing them with a positive buying experience and personalized recommendations.

By developing retention policies for each of these customer segments, businesses can improve customer loyalty, increase repeat purchases, and drive revenue growth. However, it is important to remember that customer behavior is constantly evolving, and retention policies need to be adapted and updated regularly to remain effective.

## Limitation

Lack of context: RFM analysis only considers a customer’s recency, frequency, and monetary value of purchases. It does not take into account other factors that may influence customer behavior, such as demographics, psychographics, or external market conditions. This can limit the accuracy of the insights gained from RFM analysis.

Lack of predictive power: RFM analysis is based on historical data and does not provide predictive insights into future customer behavior. While it can help identify customer segments that are more likely to make a purchase in the future, it cannot predict with certainty when or how much they will spend.

Homogeneity of segments: RFM analysis may create customer segments that are too homogeneous, meaning that they may not capture the full diversity of customer behavior. For example, two customers may have the same RFM scores but have different motivations for making a purchase. This can limit the effectiveness of retention policies developed based on these segments.

Data limitations: RFM analysis relies on clean and accurate data. If there are missing or incorrect data points, it can lead to inaccurate insights and segmentation. Additionally, if the data is not up-to-date or lacks sufficient historical data, it may not accurately reflect customer behavior.

Assumption of equal weights: RFM analysis assumes that recency, frequency, and monetary value are equally important factors in determining customer behavior. However, this may not be true for all businesses and may vary based on industry, customer base, and business objectives. Therefore, it is important to consider other factors when developing retention policies.

Despite these limitations, RFM analysis can still provide valuable insights into customer behavior and help businesses develop effective retention policies. However, it is important to be aware of its limitations and supplement it with other data sources and analysis methods to gain a more complete understanding of customer behavior.

## Application

RFM analysis can be applied in a variety of fields and industries where businesses have access to customer transaction data. Here are some examples:

E-commerce: Online retailers can use RFM analysis to segment their customers and develop targeted retention policies to encourage repeat purchases.

Banking and financial services: Banks and financial institutions can use RFM analysis to segment their customers based on their usage patterns and develop personalized services and offers.

Telecommunications: Telecommunications companies can use RFM analysis to segment their customers based on their usage patterns and develop targeted communication and retention policies.

Hospitality and travel: Hotels and travel companies can use RFM analysis to segment their customers based on their booking behavior and develop targeted marketing and retention policies.

Healthcare: Healthcare providers can use RFM analysis to segment their patients based on their utilization patterns and develop targeted communication and engagement strategies.

Subscription-based businesses: Subscription-based businesses can use RFM analysis to segment their customers based on their subscription behavior and develop targeted retention and upsell strategies.

Overall, any business that has access to transaction data can use RFM analysis to gain insights into customer behavior and develop targeted retention policies. However, it is important to supplement RFM analysis with other data sources and analysis methods to gain a more complete understanding of customer behavior.

## Conclusion

RFM analysis is a powerful technique for customer segmentation that can provide valuable insights into customer behavior. By using Python programming to conduct RFM analysis, businesses can segment their customer base and develop targeted retention strategies. The key steps involved in conducting RFM analysis include data preparation, RFM score calculation, and segmentation. By visualizing the results using Python libraries such as Pandas, Matplotlib, and Seaborn, businesses can gain a better understanding of their customer base and develop effective marketing strategies.

RFM analysis is not without its limitations, and it should be supplemented with other data sources and analysis methods to gain a more complete understanding of customer behavior. However, when used in conjunction with other techniques, RFM analysis can be a valuable tool for businesses looking to increase customer loyalty and drive revenue growth.

Overall, RFM analysis is a valuable technique for customer segmentation that can provide businesses with valuable insights into customer behavior. By using Python programming to conduct RFM analysis, businesses can gain a competitive edge in today’s rapidly evolving marketplace.