Skip to main content

This repository contains a Python implementation of RFM (Recency, Frequency, Monetary) analysis, a customer segmentation technique used in marketing and customer relationship management. The RFM analysis helps identify customer segments based on their purchasing behavior, allowing businesses to tailor their marketing strategies and customer retention efforts.

Project description

Here’s the README.md in reStructuredText format:

Latest PyPI version

RFM Analysis

This repository contains a Python implementation of RFM (Recency, Frequency, Monetary) analysis, a customer segmentation technique used in marketing and customer relationship management. The RFM analysis helps identify customer segments based on their purchasing behavior, allowing businesses to tailor their marketing strategies and customer retention efforts.

RFMAnalysis

The RFMAnalysis class provides methods to perform RFM analysis on customer transaction data. Here is an overview of the class and its methods:

### Class Initialization

RFMAnalysis(data, id_col, date_col, revenue_col)
  • data: Pandas DataFrame containing customer transaction data.

  • id_col: Column name representing the unique customer identifier.

  • date_col: Column name representing the transaction date.

  • revenue_col: Column name representing the transaction revenue.

### Methods

  • create_rfm_columns(): Creates the RFM columns (Recency, Frequency, Monetary) based on the transaction data.

  • scale_rfm_columns(): Scales the RFM columns into quartiles (4 segments) for scoring.

  • rfm_scores(): Calculates the RFM scores and segments for each customer.

  • top_customers(): Sorts the customers by RFM segments in descending order.

  • give_names_to_segments(): Assigns segment names to each RFM segment based on the RFM scores.

  • segments_distribution(): Returns a DataFrame with the mean RFM values and segment counts.

RFMVisualizer

The RFMVisualizer class provides visualizations for RFM analysis. Here is an overview of the class and its methods:

### Static Methods

  • plot_rfm(rfm_data): Plots the distribution of RFM scores.

  • visualize_segments(rfm_data): Displays a treemap visualization of customer segments.

  • segment_distribution_barplot(rfm_data): Displays a bar chart of segment counts.

  • segment_boxplot(rfm_data): Displays boxplots of RFM scores for each segment.

  • segment_comparison(rfm_data): Displays bar charts comparing average RFM scores for each segment.

Usage

To use the RFMAnalysis and RFMVisualizer classes, follow these steps:

  1. Load the customer transaction data into a Pandas DataFrame.

  2. Instantiate the RFMAnalysis class, providing the necessary parameters.

  3. Call the methods of the RFMAnalysis class to perform RFM analysis and generate segment information.

  4. Instantiate the RFMVisualizer class.

  5. Call the visualization methods of the RFMVisualizer class, passing the RFM data generated by the RFMAnalysis class.

Here’s an example of how to use these classes:

import pandas as pd
from RFMAnalysis import RFMAnalysis
from RFMVisualizer import RFMVisualizer

# Load the customer transaction data
data = pd.read_csv('customer_transactions.csv')

# Perform RFM analysis
analysis = RFMAnalysis(data, 'customer_id', 'transaction_date', 'revenue')
analysis.create_rfm_columns()
analysis.scale_rfm_columns()
analysis.rfm_scores()
analysis.top_customers()
analysis.give_names_to_segments()

# Visualize RFM analysis
visualizer = RFMVisualizer()
visualizer.plot_rfm(
analysis.rfm_data)

visualizer.visualize_segments(analysis.rfm_data) visualizer.segment_distribution_barplot(analysis.rfm_data) visualizer.segment_boxplot(analysis.rfm_data) visualizer.segment_comparison(analysis.rfm_data)

Requirements

The implementation requires the following libraries to be installed:

  • pandas

  • seaborn

  • matplotlib

  • squarify

You can install them using pip:

pip install pandas seaborn matplotlib squarify

License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.

History

0.1.0 (2023-05-16)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rfmanalysis-0.1.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rfmanalysis-0.1.0-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file rfmanalysis-0.1.0.tar.gz.

File metadata

  • Download URL: rfmanalysis-0.1.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for rfmanalysis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 403b96c49e5909a93fb302aa2c54a5ae6ed93ba40283e8125df466aba6b518be
MD5 391508567b9df8835173eaba0ba12006
BLAKE2b-256 eda9abb8e1002b23f7ff4527d8541932c8a3651492ca45fe745b75d4ab0ef63d

See more details on using hashes here.

File details

Details for the file rfmanalysis-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: rfmanalysis-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for rfmanalysis-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9d0a7601f4ce3868d2291c0ea687d155878e0e0cf54660fb5c061aa4049d2858
MD5 ec59aa6e0e97b73e4dc6e19379d0dc8f
BLAKE2b-256 f4895a71f6c969e16d4f9e56785212e16473482c337da03b37ca3ed956861ff9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page