{"id":24061556,"url":"https://github.com/walid0912/rfm_analysis","last_synced_at":"2025-09-02T13:39:13.795Z","repository":{"id":213736468,"uuid":"734469445","full_name":"walid0912/RFM_Analysis","owner":"walid0912","description":"RFM Analysis is employed to comprehend and categorize customers according to their purchasing patterns. RFM, an acronym for recency, frequency, and monetary value, comprises three essential metrics that offer insights into customer involvement, allegiance, and significance to a business.","archived":false,"fork":false,"pushed_at":"2024-01-07T14:17:45.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-09T07:54:26.913Z","etag":null,"topics":["data-analysis","data-visualization","python","rfm-analysis"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/walid0912.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-21T19:03:02.000Z","updated_at":"2024-01-07T13:52:42.000Z","dependencies_parsed_at":"2023-12-22T18:29:56.139Z","dependency_job_id":"57c01a98-d4a2-42ef-940d-a849b9ce9112","html_url":"https://github.com/walid0912/RFM_Analysis","commit_stats":null,"previous_names":["walid0912/rfm_analysis"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/walid0912%2FRFM_Analysis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/walid0912%2FRFM_Analysis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/walid0912%2FRFM_Analysis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/walid0912%2FRFM_Analysis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/walid0912","download_url":"https://codeload.github.com/walid0912/RFM_Analysis/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240877291,"owners_count":19871983,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-analysis","data-visualization","python","rfm-analysis"],"created_at":"2025-01-09T07:50:02.915Z","updated_at":"2025-02-26T15:20:57.035Z","avatar_url":"https://github.com/walid0912.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RFM Analysis Documentation\n\n## Introduction\nThe following Python script performs RFM (Recency, Frequency, Monetary) Analysis using the Plotly library to visualize and analyze customer segmentation based on their purchasing behavior. RFM analysis involves evaluating three key metrics - recency, frequency, and monetary value.\n\n\n# Import necessary libraries\n```python\nimport pandas as pd\nimport plotly.express as px\nimport plotly.io as pio\nimport plotly.graph_objects as go\nfrom datetime import datetime\n```\n\n# Set Plotly template\n```python\npio.templates.default = \"plotly_white\"\n```\n\n# Load the RFM data from a CSV file\n```python\ndata = pd.read_csv(\"rfm_data.csv\")\nprint(data.head())\n```\n\n# Convert 'PurchaseDate' to datetime\n```python\ndata['PurchaseDate'] = pd.to_datetime(data['PurchaseDate'])\n```\n\n# Calculate Recency\n```python\ndata['Recency'] = (datetime.now().date() - data['PurchaseDate'].dt.date).dt.days\n```\n\n# Calculate Frequency\n```python\nfrequency_data = data.groupby('CustomerID')['OrderID'].count().reset_index()\nfrequency_data.rename(columns={'OrderID': 'Frequency'}, inplace=True)\ndata = data.merge(frequency_data, on='CustomerID', how='left')\n```\n\n# RFM Segment Distribution\n```python\nsegment_counts = data['Value Segment'].value_counts().reset_index()\nsegment_counts.columns = ['Value Segment', 'Count']\n\npastel_colors = px.colors.qualitative.Pastel\n\n# Create the bar chart\nfig_segment_dist = px.bar(segment_counts, x='Value Segment', y='Count', \n                          color='Value Segment', color_discrete_sequence=pastel_colors,\n                          title='RFM Value Segment Distribution')\n\n# Update the layout\nfig_segment_dist.update_layout(xaxis_title='RFM Value Segment',\n                              yaxis_title='Count',\n                              showlegend=False)\n\n# Show the figure\nfig_segment_dist.show()\n```\n\n\n# Calculate Monetary Value\n```python\nmonetary_data = data.groupby('CustomerID')['TransactionAmount'].sum().reset_index()\nmonetary_data.rename(columns={'TransactionAmount': 'MonetaryValue'}, inplace=True)\ndata = data.merge(monetary_data, on='CustomerID', how='left')\n\nprint(data.head())\n```\n# Define scoring criteria for each RFM value\n```python\nrecency_scores = [5, 4, 3, 2, 1]  # Higher score for lower recency (more recent)\nfrequency_scores = [1, 2, 3, 4, 5]  # Higher score for higher frequency\nmonetary_scores = [1, 2, 3, 4, 5]  # Higher score for higher monetary value\n\n# Calculate RFM scores\ndata['RecencyScore'] = pd.cut(data['Recency'], bins=5, labels=recency_scores)\ndata['FrequencyScore'] = pd.cut(data['Frequency'], bins=5, labels=frequency_scores)\ndata['MonetaryScore'] = pd.cut(data['MonetaryValue'], bins=5, labels=monetary_scores)\n\n# Convert RFM scores to numeric type\ndata['RecencyScore'] = data['RecencyScore'].astype(int)\ndata['FrequencyScore'] = data['FrequencyScore'].astype(int)\ndata['MonetaryScore'] = data['MonetaryScore'].astype(int)\n\n\n# Calculate RFM score by combining the individual scores\ndata['RFM_Score'] = data['RecencyScore'] + data['FrequencyScore'] + data['MonetaryScore']\n\nprint(data.head())\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwalid0912%2Frfm_analysis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwalid0912%2Frfm_analysis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwalid0912%2Frfm_analysis/lists"}