Spaces:
Build error
Build error
| import pandas as pd | |
| import numpy as np | |
| from datetime import datetime, timedelta | |
| from sklearn.datasets import make_classification, make_regression, load_iris, load_wine, load_diabetes | |
| def generate_customer_data(n_rows=500): | |
| """Generate a customer dataset with demographics and purchase history.""" | |
| # Generate synthetic features | |
| X, _ = make_classification(n_samples=n_rows, n_features=5, n_informative=3, | |
| n_redundant=1, n_classes=3, random_state=42) | |
| # Create data ranges | |
| ages = np.round(X[:, 0] * 30 + 25).astype(int) # Age between 25-55 | |
| income = np.round(X[:, 1] * 75000 + 30000, -2) # Income between 30k-105k | |
| # Create customer IDs and regions | |
| customer_ids = [f'C{i:05d}' for i in range(1, n_rows+1)] | |
| regions = np.random.choice(['North', 'South', 'East', 'West', 'Central'], size=n_rows) | |
| # Generate dates for customer since | |
| start_date = datetime(2015, 1, 1) | |
| random_days = np.random.randint(0, 365*7, size=n_rows) # Within last 7 years | |
| customer_since = [start_date + timedelta(days=int(days)) for days in random_days] # Convert numpy.int32 to int | |
| customer_since = [d.strftime('%Y-%m-%d') for d in customer_since] | |
| # Generate purchase metrics | |
| purchases_90days = np.random.poisson(lam=3, size=n_rows) | |
| average_order_value = np.round(np.random.gamma(shape=5, scale=20, size=n_rows), 2) | |
| total_spent = np.round(np.random.gamma(shape=10, scale=100, size=n_rows), 2) | |
| # Generate customer segments and status | |
| segments = np.random.choice(['New', 'Regular', 'VIP', 'Inactive'], size=n_rows, | |
| p=[0.2, 0.5, 0.2, 0.1]) | |
| status = np.random.choice(['Active', 'Inactive', 'Churned'], size=n_rows, | |
| p=[0.7, 0.2, 0.1]) | |
| # Create the dataframe | |
| customers_df = pd.DataFrame({ | |
| 'CustomerID': customer_ids, | |
| 'Age': ages, | |
| 'Region': regions, | |
| 'Income': income, | |
| 'CustomerSince': customer_since, | |
| 'Purchases90Days': purchases_90days, | |
| 'AverageOrderValue': average_order_value, | |
| 'TotalSpent': total_spent, | |
| 'Segment': segments, | |
| 'Status': status | |
| }) | |
| # Add some missing values | |
| mask = np.random.random(n_rows) < 0.05 | |
| customers_df.loc[mask, 'Income'] = np.nan | |
| return customers_df | |
| def generate_product_data(n_rows=200): | |
| """Generate a product catalog dataset with categories, prices, and inventory.""" | |
| # Generate synthetic features | |
| X, _ = make_regression(n_samples=n_rows, n_features=4, random_state=42) | |
| # Create product IDs and categories | |
| product_ids = [f'P{i:04d}' for i in range(1, n_rows+1)] | |
| main_categories = ['Electronics', 'Clothing', 'Home', 'Sports', 'Books'] | |
| categories = np.random.choice(main_categories, size=n_rows) | |
| # Generate sub-categories based on main category | |
| subcategories = [] | |
| for cat in categories: | |
| if cat == 'Electronics': | |
| subcategories.append(np.random.choice(['Phones', 'Computers', 'Accessories', 'Audio'])) | |
| elif cat == 'Clothing': | |
| subcategories.append(np.random.choice(['Men', 'Women', 'Kids', 'Footwear'])) | |
| elif cat == 'Home': | |
| subcategories.append(np.random.choice(['Kitchen', 'Furniture', 'Decor', 'Bath'])) | |
| elif cat == 'Sports': | |
| subcategories.append(np.random.choice(['Fitness', 'Outdoor', 'Team Sports', 'Apparel'])) | |
| else: # Books | |
| subcategories.append(np.random.choice(['Fiction', 'Non-fiction', 'Children', 'Academic'])) | |
| # Generate product names | |
| adjectives = ['Premium', 'Deluxe', 'Basic', 'Essential', 'Advanced', 'Pro', 'Ultra', 'Lite'] | |
| product_types = ['Widget', 'Device', 'Set', 'Kit', 'Pack', 'Bundle', 'Collection', 'System'] | |
| product_names = [f"{np.random.choice(adjectives)} {subcat} {np.random.choice(product_types)}" | |
| for subcat in subcategories] | |
| # Generate numeric data | |
| prices = np.round(np.abs(X[:, 0]) * 100 + 20, 2) # Price between $20-$120 | |
| costs = np.round(prices * np.random.uniform(0.4, 0.7, size=n_rows), 2) | |
| inventory = np.random.poisson(lam=20, size=n_rows) # Inventory levels | |
| # Generate dates for product launch | |
| start_date = datetime(2018, 1, 1) | |
| random_days = np.random.randint(0, 365*4, size=n_rows) # Within last 4 years | |
| launch_dates = [start_date + timedelta(days=int(days)) for days in random_days] # Convert numpy.int32 to int | |
| launch_dates = [d.strftime('%Y-%m-%d') for d in launch_dates] | |
| # Create ratings and other metrics | |
| ratings = np.round(np.random.uniform(2.5, 5.0, size=n_rows), 1) | |
| reorder_point = np.random.randint(5, 15, size=n_rows) | |
| # Create the dataframe | |
| products_df = pd.DataFrame({ | |
| 'ProductID': product_ids, | |
| 'ProductName': product_names, | |
| 'Category': categories, | |
| 'Subcategory': subcategories, | |
| 'Price': prices, | |
| 'Cost': costs, | |
| 'LaunchDate': launch_dates, | |
| 'CurrentInventory': inventory, | |
| 'ReorderPoint': reorder_point, | |
| 'Rating': ratings | |
| }) | |
| return products_df | |
| def generate_website_analytics(n_rows=700): | |
| """Generate website analytics data with page views, bounce rates, etc.""" | |
| # Generate dates for the time series | |
| end_date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) | |
| start_date = end_date - timedelta(days=n_rows-1) | |
| dates = [start_date + timedelta(days=int(i)) for i in range(n_rows)] # Convert to int | |
| dates = [d.strftime('%Y-%m-%d') for d in dates] | |
| # Create page types | |
| page_types = ['Home', 'Product', 'Category', 'Blog', 'About', 'Contact', 'Checkout'] | |
| pages = np.random.choice(page_types, size=n_rows) | |
| # Generate device types with probabilities | |
| devices = np.random.choice(['Desktop', 'Mobile', 'Tablet'], | |
| size=n_rows, p=[0.45, 0.45, 0.1]) | |
| # Generate sources | |
| sources = np.random.choice(['Organic Search', 'Paid Search', 'Direct', 'Social', 'Email', 'Referral'], | |
| size=n_rows, p=[0.35, 0.2, 0.2, 0.15, 0.05, 0.05]) | |
| # Generate metrics | |
| base_visits = 1000 | |
| # Create seasonal pattern with weekend peaks | |
| weekday_factor = np.array([(1.2 if i % 7 >= 5 else 1.0) for i in range(n_rows)]) | |
| # Create upward trend | |
| trend_factor = np.linspace(0.8, 1.2, n_rows) | |
| # Random daily variation | |
| random_factor = np.random.normal(1, 0.1, size=n_rows) | |
| # Combine factors for visits | |
| visits = np.round(base_visits * weekday_factor * trend_factor * random_factor).astype(int) | |
| # Other metrics | |
| bounce_rates = np.round(np.random.beta(2, 5, size=n_rows) * 100, 1) # Bounce rates (%) | |
| avg_session_duration = np.round(np.random.gamma(5, 30, size=n_rows), 0) # Duration in seconds | |
| conversion_rates = np.round(np.random.beta(1.5, 20, size=n_rows) * 100, 2) # Conversion rates (%) | |
| # Create the dataframe | |
| analytics_df = pd.DataFrame({ | |
| 'Date': dates, | |
| 'PageType': pages, | |
| 'Device': devices, | |
| 'Source': sources, | |
| 'Visits': visits, | |
| 'BounceRate': bounce_rates, | |
| 'AvgSessionDuration': avg_session_duration, | |
| 'ConversionRate': conversion_rates | |
| }) | |
| return analytics_df | |
| def generate_marketing_campaign_data(n_rows=150): | |
| """Generate marketing campaign performance data.""" | |
| # Generate campaign IDs and types | |
| campaign_ids = [f'CAMP{i:03d}' for i in range(1, n_rows+1)] | |
| campaign_types = np.random.choice(['Email', 'Social', 'Search', 'Display', 'Video'], size=n_rows) | |
| # Generate dates | |
| end_date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) | |
| start_date = end_date - timedelta(days=365) # Last year | |
| random_days = np.random.randint(0, 365, size=n_rows) | |
| campaign_dates = [start_date + timedelta(days=int(days)) for days in random_days] # Convert to int | |
| campaign_dates = [d.strftime('%Y-%m-%d') for d in campaign_dates] | |
| # Generate target audience | |
| audience = np.random.choice(['New Customers', 'Existing Customers', 'All', 'VIP', 'Inactive'], size=n_rows) | |
| # Generate metrics based on campaign type | |
| impressions = np.zeros(n_rows) | |
| clicks = np.zeros(n_rows) | |
| conversions = np.zeros(n_rows) | |
| spend = np.zeros(n_rows) | |
| for i, c_type in enumerate(campaign_types): | |
| if c_type == 'Email': | |
| impressions[i] = np.random.randint(5000, 20000) | |
| clicks[i] = np.random.binomial(n=int(impressions[i]), p=0.03) | |
| conversions[i] = np.random.binomial(n=int(clicks[i]), p=0.1) | |
| spend[i] = np.random.uniform(500, 1500) | |
| elif c_type == 'Social': | |
| impressions[i] = np.random.randint(10000, 50000) | |
| clicks[i] = np.random.binomial(n=int(impressions[i]), p=0.02) | |
| conversions[i] = np.random.binomial(n=int(clicks[i]), p=0.08) | |
| spend[i] = np.random.uniform(1000, 3000) | |
| elif c_type == 'Search': | |
| impressions[i] = np.random.randint(2000, 10000) | |
| clicks[i] = np.random.binomial(n=int(impressions[i]), p=0.05) | |
| conversions[i] = np.random.binomial(n=int(clicks[i]), p=0.12) | |
| spend[i] = np.random.uniform(1500, 5000) | |
| elif c_type == 'Display': | |
| impressions[i] = np.random.randint(30000, 100000) | |
| clicks[i] = np.random.binomial(n=int(impressions[i]), p=0.01) | |
| conversions[i] = np.random.binomial(n=int(clicks[i]), p=0.05) | |
| spend[i] = np.random.uniform(800, 2500) | |
| else: # Video | |
| impressions[i] = np.random.randint(8000, 30000) | |
| clicks[i] = np.random.binomial(n=int(impressions[i]), p=0.015) | |
| conversions[i] = np.random.binomial(n=int(clicks[i]), p=0.07) | |
| spend[i] = np.random.uniform(2000, 6000) | |
| # Calculate derived metrics | |
| ctr = np.round(clicks / impressions * 100, 2) # Click-through rate (%) | |
| cvr = np.round(conversions / clicks * 100, 2) # Conversion rate (%) | |
| cpc = np.round(spend / clicks, 2) # Cost per click | |
| cpa = np.round(spend / conversions, 2) # Cost per acquisition | |
| # Handle division by zero | |
| cpc = np.where(clicks == 0, 0, cpc) | |
| cpa = np.where(conversions == 0, 0, cpa) | |
| # Generate revenue (as a multiple of conversions with some variance) | |
| avg_order_values = np.random.uniform(50, 200, size=n_rows) | |
| revenue = np.round(conversions * avg_order_values, 2) | |
| roi = np.round((revenue - spend) / spend * 100, 2) # ROI (%) | |
| roi = np.where(spend == 0, 0, roi) | |
| # Create the dataframe | |
| campaigns_df = pd.DataFrame({ | |
| 'CampaignID': campaign_ids, | |
| 'CampaignType': campaign_types, | |
| 'Date': campaign_dates, | |
| 'TargetAudience': audience, | |
| 'Impressions': impressions.astype(int), | |
| 'Clicks': clicks.astype(int), | |
| 'Conversions': conversions.astype(int), | |
| 'Spend': np.round(spend, 2), | |
| 'CTR': ctr, | |
| 'CVR': cvr, | |
| 'CPC': cpc, | |
| 'CPA': cpa, | |
| 'Revenue': revenue, | |
| 'ROI': roi | |
| }) | |
| return campaigns_df | |
| def generate_ml_datasets(): | |
| """Generate a dictionary containing popular ML datasets.""" | |
| datasets = {} | |
| # Get Iris dataset | |
| iris = load_iris(as_frame=True) | |
| datasets['iris'] = iris.frame | |
| # Get Wine dataset | |
| wine = load_wine(as_frame=True) | |
| datasets['wine'] = wine.frame | |
| # Get Diabetes dataset | |
| diabetes = load_diabetes(as_frame=True) | |
| datasets['diabetes'] = diabetes.frame | |
| return datasets | |
| if __name__ == "__main__": | |
| # Generate all datasets | |
| customers = generate_customer_data() | |
| products = generate_product_data() | |
| web_analytics = generate_website_analytics() | |
| campaigns = generate_marketing_campaign_data() | |
| ml_datasets = generate_ml_datasets() | |
| # Save datasets to CSV files | |
| customers.to_csv('sample_customer_data.csv', index=False) | |
| products.to_csv('sample_product_data.csv', index=False) | |
| web_analytics.to_csv('sample_web_analytics.csv', index=False) | |
| campaigns.to_csv('sample_marketing_campaigns.csv', index=False) | |
| # Save ML datasets | |
| for name, dataset in ml_datasets.items(): | |
| dataset.to_csv(f'sample_{name}_data.csv', index=False) | |
| print("All additional sample datasets generated successfully!") | |
| print(f"Generated {len(customers)} customer records") | |
| print(f"Generated {len(products)} product records") | |
| print(f"Generated {len(web_analytics)} web analytics records") | |
| print(f"Generated {len(campaigns)} marketing campaign records") | |
| print(f"Generated ML datasets: {', '.join(ml_datasets.keys())}") |