help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
ecommerce-customers
RSK World
ecommerce-customers
E-commerce Customer Dataset - Customer Segmentation + Marketing Analytics + Customer Behavior Analysis
ecommerce-customers
  • __pycache__
  • .gitignore583 B
  • GITHUB_RELEASE_INSTRUCTIONS.md5.2 KB
  • ISSUES_FIXED.md4.3 KB
  • LICENSE1.4 KB
  • LICENSE.txt1.4 KB
  • README.md13.1 KB
  • RELEASE_NOTES.md5.1 KB
  • analyze_customers.py13.2 KB
  • customer_segmentation.py8.4 KB
  • ecommerce_customers.csv19.9 KB
  • generate_enhanced_dataset.py7.1 KB
  • index.html26.6 KB
  • queries.sql21.5 KB
  • requirements.txt250 B
  • test_dataset.py4 KB
  • visualize_data.py11.4 KB
advanced_visualization.cpython-313.pycvisualize.cpython-313.pycREADME.mdcustomer_segmentation.py
README.md
Raw Download

README.md

# E-commerce Customer Dataset

<!--
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
-->

## 📊 Dataset Overview

This dataset contains comprehensive e-commerce customer behavior data including purchase history, browsing patterns, product preferences, and customer segmentation labels. Perfect for customer segmentation, recommendation systems, and marketing analytics.

**Dataset Details:**
- **Total Customers:** 100
- **Features:** 40 (Enhanced Dataset)
- **Difficulty Level:** Intermediate
- **Category:** Tabular Data
- **Technologies:** CSV, SQL, Pandas, NumPy

## 🎯 Key Features

### Core Features
- ✅ **Purchase History** - Complete purchase records with frequency, order values, and totals
- ✅ **Browsing Patterns** - Detailed browsing behavior including time spent and device preferences
- ✅ **Product Preferences** - Customer preferences for different product categories (6 categories)
- ✅ **Customer Segments** - Pre-labeled segments (High Value, Medium Value, Low Value)
- ✅ **Ready for Clustering** - Optimized for machine learning and clustering models

### Enhanced Unique Features (27 Additional Features)
- ✅ **Customer Lifetime Value (CLV)** - Calculated CLV for each customer
- ✅ **Return Rate** - Product return rates per customer
- ✅ **Payment Methods** - 6 payment method preferences (Credit Card, Debit Card, PayPal, Digital Wallet, Bank Transfer, Cash on Delivery)
- ✅ **Newsletter Subscription** - Subscription status and email engagement
- ✅ **Social Media Engagement** - Engagement scores and social shares count
- ✅ **Average Review Rating** - Customer review ratings (1-5 scale)
- ✅ **Cart Abandonment Rate** - Shopping cart abandonment percentages
- ✅ **Discount Usage** - Discount usage percentage and coupon redemptions
- ✅ **Referral Sources** - 7 referral sources (Google Search, Social Media, Email Campaign, Direct, Referral, Advertisement, Influencer)
- ✅ **Customer Satisfaction Score** - Satisfaction scores (1-5 scale)
- ✅ **Preferred Shopping Hour** - Hour of day when customers prefer to shop
- ✅ **Mobile App User** - Mobile app usage identification
- ✅ **Wishlist Items** - Number of items in customer wishlists
- ✅ **Customer Since (Months)** - Customer tenure in months
- ✅ **Loyalty Tier** - 5-tier loyalty program (Bronze, Silver, Gold, Platinum, Diamond)
- ✅ **Email Open Rate** - Email marketing open rates
- ✅ **Click-Through Rate** - Email marketing CTR
- ✅ **Cross-Category Purchases** - Number of different categories purchased
- ✅ **Repeat Purchase Rate** - Customer repeat purchase percentage
- ✅ **Average Session Duration** - Time spent per session (seconds)
- ✅ **Pages Per Session** - Average pages viewed per session
- ✅ **Geographic Region** - 5 regions (North, South, East, West, Central)
- ✅ **Preferred Shipping** - Shipping method preferences (Standard, Express, Overnight, Same Day)
- ✅ **Support Interactions** - Number of customer support interactions
- ✅ **Product Reviews Count** - Number of product reviews written
- ✅ **Social Shares** - Number of social media shares
- ✅ **Coupon Redemptions** - Number of coupons redeemed

## 📁 Project Structure

```
ecommerce-customers/
│
├── ecommerce_customers.csv # Enhanced dataset (40 features)
├── generate_enhanced_dataset.py # Dataset generation script
├── analyze_customers.py # Comprehensive data analysis script
├── customer_segmentation.py # Advanced clustering analysis
├── visualize_data.py # Data visualization script
├── queries.sql # 50 SQL queries for data analysis
├── index.html # Interactive demo page
├── requirements.txt # Python dependencies
└── README.md # This file
```

## 🚀 Quick Start

### 1. Install Dependencies

```bash
pip install pandas numpy matplotlib seaborn scikit-learn
```

### 2. Load the Dataset

```python
import pandas as pd

# Load the dataset
df = pd.read_csv('ecommerce_customers.csv')

# Display basic information
print(df.head())
print(df.info())
```

### 3. Run Analysis Scripts

**Basic Analysis:**
```bash
python analyze_customers.py
```

**Customer Segmentation:**
```bash
python customer_segmentation.py
```

### 4. SQL Queries

Import the dataset into your SQL database and run the queries from `queries.sql`:

```sql
-- Example: Get customer segment distribution
SELECT
segment,
COUNT(*) AS customer_count,
AVG(spending_score) AS avg_spending_score
FROM ecommerce_customers
GROUP BY segment;
```

## 📊 Dataset Schema (40 Features)

### Basic Information
| Column | Type | Description |
|--------|------|-------------|
| customer_id | INT | Unique customer identifier |
| age | INT | Customer age |
| gender | STRING | Customer gender (Male/Female) |
| annual_income | DECIMAL | Annual income in USD |
| spending_score | INT | Spending score (0-100) |

### Purchase Behavior
| Column | Type | Description |
|--------|------|-------------|
| purchase_frequency | INT | Number of purchases |
| avg_order_value | DECIMAL | Average order value in USD |
| total_purchases | INT | Total number of purchases |
| customer_lifetime_value | DECIMAL | Calculated CLV |
| repeat_purchase_rate | DECIMAL | Repeat purchase percentage |
| last_purchase_days | INT | Days since last purchase |

### Browsing & Engagement
| Column | Type | Description |
|--------|------|-------------|
| browsing_time_minutes | INT | Total browsing time in minutes |
| avg_session_duration | INT | Average session duration (seconds) |
| pages_per_session | INT | Average pages per session |
| preferred_shopping_hour | INT | Preferred shopping hour (0-23) |

### Product & Preferences
| Column | Type | Description |
|--------|------|-------------|
| product_category_preference | STRING | Preferred product category (6 categories) |
| device_type | STRING | Preferred device (Mobile/Desktop/Tablet) |
| cross_category_purchases | INT | Number of different categories purchased |
| wishlist_items | INT | Number of wishlist items |

### Customer Segmentation
| Column | Type | Description |
|--------|------|-------------|
| segment | STRING | Customer segment (High/Medium/Low Value) |
| loyalty_tier | STRING | Loyalty tier (Bronze/Silver/Gold/Platinum/Diamond) |
| customer_since_months | INT | Customer tenure in months |

### Payment & Transactions
| Column | Type | Description |
|--------|------|-------------|
| payment_method | STRING | Preferred payment method (6 types) |
| return_rate | DECIMAL | Product return rate |
| discount_usage_pct | DECIMAL | Discount usage percentage |
| coupon_redemptions | INT | Number of coupons redeemed |

### Marketing & Communication
| Column | Type | Description |
|--------|------|-------------|
| newsletter_subscribed | STRING | Newsletter subscription (Yes/No) |
| email_open_rate | DECIMAL | Email open rate (0-1) |
| click_through_rate | DECIMAL | Email CTR (0-1) |
| referral_source | STRING | Referral source (7 types) |
| social_media_engagement | INT | Social media engagement score |
| social_shares | INT | Number of social shares |

### Customer Experience
| Column | Type | Description |
|--------|------|-------------|
| customer_satisfaction_score | DECIMAL | Satisfaction score (1-5) |
| avg_review_rating | DECIMAL | Average review rating (1-5) |
| product_reviews_count | INT | Number of reviews written |
| cart_abandonment_rate | DECIMAL | Cart abandonment rate |
| support_interactions | INT | Customer support interactions |

### Geographic & Logistics
| Column | Type | Description |
|--------|------|-------------|
| geographic_region | STRING | Geographic region (5 regions) |
| preferred_shipping | STRING | Preferred shipping method (4 types) |

### Technology
| Column | Type | Description |
|--------|------|-------------|
| mobile_app_user | STRING | Mobile app user (Yes/No) |

## 🔍 Analysis Examples

### Customer Segmentation Analysis

```python
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# Load data
df = pd.read_csv('ecommerce_customers.csv')

# Prepare features
features = ['annual_income', 'spending_score', 'purchase_frequency']
X = df[features].values

# Standardize
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# K-Means clustering
kmeans = KMeans(n_clusters=4, random_state=42)
df['cluster'] = kmeans.fit_predict(X_scaled)

# Analyze clusters
print(df.groupby('cluster')[features].mean())
```

### Purchase Behavior Analysis

```python
# Analyze by segment
segment_stats = df.groupby('segment').agg({
'spending_score': 'mean',
'purchase_frequency': 'mean',
'avg_order_value': 'mean'
})
print(segment_stats)
```

### Product Preference Analysis

```python
# Product category preferences
category_stats = df.groupby('product_category_preference').agg({
'customer_id': 'count',
'spending_score': 'mean',
'avg_order_value': 'mean'
})
print(category_stats)
```

## 📈 Use Cases

1. **Customer Segmentation** - Identify and group customers based on behavior
2. **Marketing Analytics** - Understand customer preferences and spending patterns
3. **Recommendation Systems** - Build product recommendation engines
4. **Churn Prediction** - Identify customers at risk of churning
5. **Customer Lifetime Value** - Calculate CLV for different segments
6. **Targeted Marketing** - Create personalized marketing campaigns

## 🛠️ Technologies Used

- **CSV** - Data storage format
- **SQL** - Database queries and analysis
- **Pandas** - Data manipulation and analysis
- **NumPy** - Numerical computations
- **Matplotlib/Seaborn** - Data visualization
- **Scikit-learn** - Machine learning algorithms

## 📝 SQL Queries Included

The `queries.sql` file contains **50 ready-to-use SQL queries** including:

- Basic queries (counts, statistics)
- Segmentation analysis
- Gender-based analysis
- Product preference analysis
- Device type analysis
- Age group analysis
- Purchase behavior analysis
- Browsing behavior analysis
- **Enhanced Features Queries (25 new queries):**
- Customer Lifetime Value analysis
- Payment method preferences
- Loyalty tier analysis
- Newsletter subscription impact
- Social media engagement
- Customer satisfaction metrics
- Geographic region analysis
- Referral source performance
- Mobile app users analysis
- Cart abandonment patterns
- Discount usage analysis
- Return rate analysis
- Shopping hour preferences
- Wishlist analysis
- Cross-category purchases
- Support interactions
- Shipping preferences
- Session behavior
- Repeat purchase rates
- Email marketing effectiveness
- Customer retention metrics
- Product review engagement
- Top performers analysis

## 🎨 Demo Page

Open `index.html` in your browser to view an interactive demo page with:
- Dataset overview and statistics
- Feature descriptions
- Dataset preview
- Analysis script information
- Download links

## 📊 Sample Insights

### Basic Insights
- **High Value Customers:** ~40% of customers with average spending score > 75
- **Most Preferred Category:** Multiple categories (Clothing, Electronics, Home & Garden, etc.)
- **Device Preference:** Mobile devices are most popular
- **Purchase Frequency:** High-value customers average 15+ purchases
- **Browsing Time:** Correlates positively with purchase frequency

### Enhanced Insights
- **Customer Lifetime Value:** High-value customers have significantly higher CLV
- **Loyalty Tiers:** Diamond and Platinum tiers show highest engagement
- **Payment Methods:** Credit cards and digital wallets are most popular
- **Email Marketing:** Subscribers show 3x higher engagement rates
- **Social Media:** High engagement correlates with higher spending
- **Mobile App:** App users have higher purchase frequency
- **Geographic:** Regional variations in spending patterns
- **Satisfaction:** High satisfaction scores correlate with repeat purchases
- **Cart Abandonment:** Lower abandonment rates in high-value segment
- **Referral Sources:** Social media and influencers drive quality customers

## 🔗 Resources

- **Website:** [https://rskworld.in](https://rskworld.in)
- **Email:** help@rskworld.in
- **Phone:** +91 93305 39277

## 📄 License

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

**Copyright (c) 2026 RSK World**

This dataset and associated code are provided for educational and research purposes.
You are free to use, modify, and distribute according to the MIT License terms.

## 🙏 Credits

**Author:** RSK World
**Website:** https://rskworld.in
**Email:** help@rskworld.in
**Phone:** +91 93305 39277

---

**Note:** This dataset is designed for learning and practicing data science, machine learning, and marketing analytics. Feel free to modify and extend the analysis scripts according to your needs.

customer_segmentation.py
Raw Download
Find: Go to:
"""
Customer Segmentation using Clustering Models
=============================================
This script performs advanced customer segmentation using various clustering algorithms
including K-Means, DBSCAN, and Hierarchical Clustering.

Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans, DBSCAN, AgglomerativeClustering
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.metrics import silhouette_score
import warnings
warnings.filterwarnings('ignore')

# Set style
sns.set_style("whitegrid")
plt.rcParams['figure.figsize'] = (14, 8)

def load_and_prepare_data(file_path='ecommerce_customers.csv'):
    """
    Load and prepare data for clustering
    """
    df = pd.read_csv(file_path)
    
    # Encode categorical variables
    le_gender = LabelEncoder()
    le_category = LabelEncoder()
    le_device = LabelEncoder()
    
    df['gender_encoded'] = le_gender.fit_transform(df['gender'])
    df['category_encoded'] = le_category.fit_transform(df['product_category_preference'])
    df['device_encoded'] = le_device.fit_transform(df['device_type'])
    
    return df, le_gender, le_category, le_device

def find_optimal_clusters(X, max_clusters=10):
    """
    Find optimal number of clusters using Elbow Method and Silhouette Score
    """
    inertias = []
    silhouette_scores = []
    K_range = range(2, max_clusters + 1)
    
    for k in K_range:
        kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
        kmeans.fit(X)
        inertias.append(kmeans.inertia_)
        silhouette_scores.append(silhouette_score(X, kmeans.labels_))
    
    # Find optimal k (highest silhouette score)
    optimal_k = K_range[np.argmax(silhouette_scores)]
    
    return optimal_k, inertias, silhouette_scores, K_range

def kmeans_segmentation(df, n_clusters=4):
    """
    Perform K-Means clustering
    """
    print("\n" + "="*60)
    print("K-MEANS CLUSTERING")
    print("="*60)
    
    # Select features
    features = ['annual_income', 'spending_score', 'purchase_frequency', 
                'avg_order_value', 'browsing_time_minutes']
    X = df[features].values
    
    # Standardize
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X)
    
    # Find optimal clusters
    optimal_k, inertias, sil_scores, K_range = find_optimal_clusters(X_scaled)
    print(f"\nOptimal number of clusters: {optimal_k}")
    print(f"Best Silhouette Score: {sil_scores[optimal_k-2]:.3f}")
    
    # Perform clustering with optimal k
    kmeans = KMeans(n_clusters=optimal_k, random_state=42, n_init=10)
    df['kmeans_cluster'] = kmeans.fit_predict(X_scaled)
    
    # Analyze clusters
    print("\nCluster Characteristics:")
    cluster_analysis = df.groupby('kmeans_cluster').agg({
        'annual_income': 'mean',
        'spending_score': 'mean',
        'purchase_frequency': 'mean',
        'avg_order_value': 'mean',
        'browsing_time_minutes': 'mean',
        'customer_id': 'count'
    }).round(2)
    cluster_analysis.columns = ['Avg Income', 'Avg Spending', 'Avg Freq', 
                                'Avg Order Value', 'Avg Browsing Time', 'Count']
    print(cluster_analysis)
    
    return df, kmeans, scaler

def dbscan_segmentation(df):
    """
    Perform DBSCAN clustering
    """
    print("\n" + "="*60)
    print("DBSCAN CLUSTERING")
    print("="*60)
    
    # Select features
    features = ['annual_income', 'spending_score', 'purchase_frequency', 
                'avg_order_value', 'browsing_time_minutes']
    X = df[features].values
    
    # Standardize
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X)
    
    # Perform DBSCAN
    dbscan = DBSCAN(eps=0.5, min_samples=5)
    df['dbscan_cluster'] = dbscan.fit_predict(X_scaled)
    
    # Analyze clusters
    n_clusters = len(set(df['dbscan_cluster'])) - (1 if -1 in df['dbscan_cluster'] else 0)
    n_noise = list(df['dbscan_cluster']).count(-1)
    
    print(f"\nNumber of clusters found: {n_clusters}")
    print(f"Number of noise points: {n_noise}")
    
    if n_clusters > 0:
        print("\nCluster Characteristics:")
        cluster_analysis = df[df['dbscan_cluster'] != -1].groupby('dbscan_cluster').agg({
            'annual_income': 'mean',
            'spending_score': 'mean',
            'purchase_frequency': 'mean',
            'avg_order_value': 'mean',
            'customer_id': 'count'
        }).round(2)
        print(cluster_analysis)
    
    return df, dbscan

def hierarchical_segmentation(df, n_clusters=4):
    """
    Perform Hierarchical Clustering
    """
    print("\n" + "="*60)
    print("HIERARCHICAL CLUSTERING")
    print("="*60)
    
    # Select features
    features = ['annual_income', 'spending_score', 'purchase_frequency', 
                'avg_order_value', 'browsing_time_minutes']
    X = df[features].values
    
    # Standardize
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X)
    
    # Perform Agglomerative Clustering
    hierarchical = AgglomerativeClustering(n_clusters=n_clusters, linkage='ward')
    df['hierarchical_cluster'] = hierarchical.fit_predict(X_scaled)
    
    # Analyze clusters
    print("\nCluster Characteristics:")
    cluster_analysis = df.groupby('hierarchical_cluster').agg({
        'annual_income': 'mean',
        'spending_score': 'mean',
        'purchase_frequency': 'mean',
        'avg_order_value': 'mean',
        'customer_id': 'count'
    }).round(2)
    cluster_analysis.columns = ['Avg Income', 'Avg Spending', 'Avg Freq', 
                                'Avg Order Value', 'Count']
    print(cluster_analysis)
    
    return df, hierarchical

def compare_segments(df):
    """
    Compare different segmentation methods
    """
    print("\n" + "="*60)
    print("SEGMENTATION COMPARISON")
    print("="*60)
    
    print("\nOriginal Segment Distribution:")
    print(df['segment'].value_counts())
    
    if 'kmeans_cluster' in df.columns:
        print("\nK-Means Cluster Distribution:")
        print(df['kmeans_cluster'].value_counts().sort_index())
    
    if 'dbscan_cluster' in df.columns:
        print("\nDBSCAN Cluster Distribution:")
        print(df['dbscan_cluster'].value_counts().sort_index())
    
    if 'hierarchical_cluster' in df.columns:
        print("\nHierarchical Cluster Distribution:")
        print(df['hierarchical_cluster'].value_counts().sort_index())

def generate_segment_profiles(df):
    """
    Generate detailed profiles for each segment
    """
    print("\n" + "="*60)
    print("SEGMENT PROFILES")
    print("="*60)
    
    if 'kmeans_cluster' in df.columns:
        print("\nK-Means Segment Profiles:")
        for cluster in sorted(df['kmeans_cluster'].unique()):
            cluster_data = df[df['kmeans_cluster'] == cluster]
            print(f"\n  Cluster {cluster} ({len(cluster_data)} customers):")
            print(f"    - Avg Income: ${cluster_data['annual_income'].mean():,.0f}")
            print(f"    - Avg Spending Score: {cluster_data['spending_score'].mean():.1f}")
            print(f"    - Avg Purchase Frequency: {cluster_data['purchase_frequency'].mean():.1f}")
            print(f"    - Top Category: {cluster_data['product_category_preference'].mode()[0]}")
            print(f"    - Top Device: {cluster_data['device_type'].mode()[0]}")

def main():
    """
    Main segmentation function
    """
    print("="*60)
    print("CUSTOMER SEGMENTATION ANALYSIS")
    print("RSK World - https://rskworld.in")
    print("="*60)
    
    # Load and prepare data
    df, le_gender, le_category, le_device = load_and_prepare_data()
    print(f"\nDataset loaded: {len(df)} customers")
    
    # Perform different clustering methods
    df, kmeans, scaler = kmeans_segmentation(df)
    df, dbscan = dbscan_segmentation(df)
    df, hierarchical = hierarchical_segmentation(df)
    
    # Compare and analyze
    compare_segments(df)
    generate_segment_profiles(df)
    
    # Save results
    output_file = 'customer_segmentation_results.csv'
    df.to_csv(output_file, index=False)
    print(f"\nSegmentation results saved to '{output_file}'")
    
    print("\n" + "="*60)
    print("SEGMENTATION COMPLETE")
    print("="*60)

if __name__ == "__main__":
    main()

258 lines•8.4 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer