{ "cells": [ { "cell_type": "markdown", "id": "faad7818", "metadata": {}, "source": [ "# Synthetic Dummy Data Generator (UMKM)\n", "Notebook ini membuat data sintetis yang meniru pola data bisnis riil UMKM untuk analisis operasional, NLP, dan klasifikasi bisnis.\n", "\n", "Fitur utama yang dihasilkan:\n", "\n", "- Monthly_Revenue (IDR)\n", "- Net_Profit_Margin (%)\n", "- Burn_Rate_Ratio\n", "- Transaction_Count\n", "- Avg_Historical_Rating\n", "- Review_Text\n", "- Review_Volatility\n", "- Business_Tenure_Months\n", "- Repeat_Order_Rate (%)\n", "- Digital_Adoption_Score\n", "- Peak_Hour_Latency\n", "- Location_Competitiveness\n", "- Sentiment_Score (-1.0 s/d 1.0)\n", "- Class (Elite, Growth, Struggling, Critical)\n", "\n", "Karakteristik realisme yang dimodelkan:\n", "\n", "- Korelasi antar fitur operasional (contoh: adopsi digital cenderung meningkatkan retensi dan rating)\n", "- Trade-off bisnis (kompetisi tinggi dan latency tinggi menekan profitabilitas)\n", "- Distribusi finansial tidak simetris (AOV lognormal)\n", "- Noise terkontrol agar data tidak terlalu \"rapi\"\n", "- Review teks konsisten dengan sinyal kualitas (rating, volatility, latency)\n", "- Sentiment score diekstrak dari `Review_Text`\n", "\n", "Catatan target:\n", "\n", "- Variabel target adalah `Class` (kolom paling kanan)\n", "- Klasifikasi menggunakan threshold berbasis persentil agar distribusi kelas lebih seimbang\n", "- Urutan kelas: `Elite`, `Growth`, `Struggling`, `Critical`" ] }, { "cell_type": "code", "execution_count": 29, "id": "af962614", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated 150000 rows -> synthetic_umkm_data.csv\n", "\n", "Preview:\n" ] }, { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "index", "rawType": "int64", "type": "integer" }, { "name": "ID", "rawType": "int32", "type": "integer" }, { "name": "Monthly_Revenue", "rawType": "int32", "type": "integer" }, { "name": "Net_Profit_Margin (%)", "rawType": "float64", "type": "float" }, { "name": "Burn_Rate_Ratio", "rawType": "float64", "type": "float" }, { "name": "Transaction_Count", "rawType": "int32", "type": "integer" }, { "name": "Avg_Historical_Rating", "rawType": "float64", "type": "float" }, { "name": "Review_Text", "rawType": "object", "type": "string" }, { "name": "Review_Volatility", "rawType": "float64", "type": "float" }, { "name": "Business_Tenure_Months", "rawType": "int32", "type": "integer" }, { "name": "Repeat_Order_Rate (%)", "rawType": "float64", "type": "float" }, { "name": "Digital_Adoption_Score", "rawType": "float64", "type": "float" }, { "name": "Peak_Hour_Latency", "rawType": "object", "type": "string" }, { "name": "Location_Competitiveness", "rawType": "int32", "type": "integer" }, { "name": "Sentiment_Score", "rawType": "float64", "type": "float" }, { "name": "Class", "rawType": "object", "type": "string" } ], "ref": "f3816b53-8a85-4cea-a3c9-d8cfc2031baa", "rows": [ [ "0", "1", "6680716", "22.72", "0.811", "161", "4.75", "Transaksi digital lancar, proses checkout tidak ribet. Ad eius dolore qui.", "0.313", "105", "19.4", "4.24", "Low", "9", "-0.25", "Growth" ], [ "1", "2", "5819101", "4.46", "0.968", "104", "4.21", "Harga dan kualitas seimbang, pengalaman biasa saja. Assumenda in adipisci dolor magnam ad dicta.", "0.632", "95", "14.87", "1.27", "Med", "10", "0.0", "Growth" ], [ "2", "3", "5236404", "-10.12", "1.047", "102", "3.51", "Pelayanan standar, masih bisa ditingkatkan.", "0.47", "17", "21.0", "3.37", "Med", "8", "0.0", "Struggling" ], [ "3", "4", "8043552", "0.04", "0.969", "99", "4.33", "Transaksi digital lancar, proses checkout tidak ribet.", "0.206", "109", "30.62", "5.41", "Low", "13", "-0.25", "Growth" ], [ "4", "5", "6071256", "4.22", "0.954", "115", "4.34", "Selalu repeat order karena kualitasnya terjaga. In ipsum eius sit quis cum in.", "0.232", "74", "20.87", "2.67", "Low", "7", "0.25", "Growth" ], [ "5", "6", "6683141", "29.68", "0.727", "108", "4.54", "Pengiriman cepat, admin komunikatif. Culpa vero excepturi at atque.", "0.185", "23", "26.35", "5.59", "Low", "16", "0.55", "Elite" ], [ "6", "7", "14123932", "15.28", "0.86", "167", "4.54", "Produk cukup baik, kadang waktu tunggu agak lama. Nobis rem quas modi voluptate fugiat.", "0.434", "105", "22.15", "3.95", "Med", "6", "0.0", "Growth" ], [ "7", "8", "8483571", "8.51", "0.862", "180", "4.83", "Selalu repeat order karena kualitasnya terjaga.", "0.346", "124", "23.17", "7.59", "Low", "10", "0.25", "Growth" ], [ "8", "9", "14900709", "6.0", "0.908", "135", "4.86", "Selalu repeat order karena kualitasnya terjaga. Eos occaecati suscipit facere deleniti architecto optio.", "0.285", "77", "15.85", "6.56", "Low", "7", "0.25", "Growth" ], [ "9", "10", "9232562", "-13.64", "1.085", "89", "4.39", "Transaksi digital lancar, proses checkout tidak ribet.", "0.182", "90", "17.3", "3.22", "Low", "9", "-0.25", "Struggling" ] ], "shape": { "columns": 15, "rows": 10 } }, "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDMonthly_RevenueNet_Profit_Margin (%)Burn_Rate_RatioTransaction_CountAvg_Historical_RatingReview_TextReview_VolatilityBusiness_Tenure_MonthsRepeat_Order_Rate (%)Digital_Adoption_ScorePeak_Hour_LatencyLocation_CompetitivenessSentiment_ScoreClass
01668071622.720.8111614.75Transaksi digital lancar, proses checkout tida...0.31310519.404.24Low9-0.25Growth
1258191014.460.9681044.21Harga dan kualitas seimbang, pengalaman biasa ...0.6329514.871.27Med100.00Growth
235236404-10.121.0471023.51Pelayanan standar, masih bisa ditingkatkan.0.4701721.003.37Med80.00Struggling
3480435520.040.969994.33Transaksi digital lancar, proses checkout tida...0.20610930.625.41Low13-0.25Growth
4560712564.220.9541154.34Selalu repeat order karena kualitasnya terjaga...0.2327420.872.67Low70.25Growth
56668314129.680.7271084.54Pengiriman cepat, admin komunikatif. Culpa ver...0.1852326.355.59Low160.55Elite
671412393215.280.8601674.54Produk cukup baik, kadang waktu tunggu agak la...0.43410522.153.95Med60.00Growth
7884835718.510.8621804.83Selalu repeat order karena kualitasnya terjaga.0.34612423.177.59Low100.25Growth
89149007096.000.9081354.86Selalu repeat order karena kualitasnya terjaga...0.2857715.856.56Low70.25Growth
9109232562-13.641.085894.39Transaksi digital lancar, proses checkout tida...0.1829017.303.22Low9-0.25Struggling
\n", "
" ], "text/plain": [ " ID Monthly_Revenue Net_Profit_Margin (%) Burn_Rate_Ratio \\\n", "0 1 6680716 22.72 0.811 \n", "1 2 5819101 4.46 0.968 \n", "2 3 5236404 -10.12 1.047 \n", "3 4 8043552 0.04 0.969 \n", "4 5 6071256 4.22 0.954 \n", "5 6 6683141 29.68 0.727 \n", "6 7 14123932 15.28 0.860 \n", "7 8 8483571 8.51 0.862 \n", "8 9 14900709 6.00 0.908 \n", "9 10 9232562 -13.64 1.085 \n", "\n", " Transaction_Count Avg_Historical_Rating \\\n", "0 161 4.75 \n", "1 104 4.21 \n", "2 102 3.51 \n", "3 99 4.33 \n", "4 115 4.34 \n", "5 108 4.54 \n", "6 167 4.54 \n", "7 180 4.83 \n", "8 135 4.86 \n", "9 89 4.39 \n", "\n", " Review_Text Review_Volatility \\\n", "0 Transaksi digital lancar, proses checkout tida... 0.313 \n", "1 Harga dan kualitas seimbang, pengalaman biasa ... 0.632 \n", "2 Pelayanan standar, masih bisa ditingkatkan. 0.470 \n", "3 Transaksi digital lancar, proses checkout tida... 0.206 \n", "4 Selalu repeat order karena kualitasnya terjaga... 0.232 \n", "5 Pengiriman cepat, admin komunikatif. Culpa ver... 0.185 \n", "6 Produk cukup baik, kadang waktu tunggu agak la... 0.434 \n", "7 Selalu repeat order karena kualitasnya terjaga. 0.346 \n", "8 Selalu repeat order karena kualitasnya terjaga... 0.285 \n", "9 Transaksi digital lancar, proses checkout tida... 0.182 \n", "\n", " Business_Tenure_Months Repeat_Order_Rate (%) Digital_Adoption_Score \\\n", "0 105 19.40 4.24 \n", "1 95 14.87 1.27 \n", "2 17 21.00 3.37 \n", "3 109 30.62 5.41 \n", "4 74 20.87 2.67 \n", "5 23 26.35 5.59 \n", "6 105 22.15 3.95 \n", "7 124 23.17 7.59 \n", "8 77 15.85 6.56 \n", "9 90 17.30 3.22 \n", "\n", " Peak_Hour_Latency Location_Competitiveness Sentiment_Score Class \n", "0 Low 9 -0.25 Growth \n", "1 Med 10 0.00 Growth \n", "2 Med 8 0.00 Struggling \n", "3 Low 13 -0.25 Growth \n", "4 Low 7 0.25 Growth \n", "5 Low 16 0.55 Elite \n", "6 Med 6 0.00 Growth \n", "7 Low 10 0.25 Growth \n", "8 Low 7 0.25 Growth \n", "9 Low 9 -0.25 Struggling " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Summary stats:\n" ] }, { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "index", "rawType": "object", "type": "string" }, { "name": "count", "rawType": "object", "type": "unknown" }, { "name": "unique", "rawType": "object", "type": "unknown" }, { "name": "top", "rawType": "object", "type": "unknown" }, { "name": "freq", "rawType": "object", "type": "unknown" }, { "name": "mean", "rawType": "object", "type": "unknown" }, { "name": "std", "rawType": "object", "type": "unknown" }, { "name": "min", "rawType": "object", "type": "unknown" }, { "name": "25%", "rawType": "object", "type": "unknown" }, { "name": "50%", "rawType": "object", "type": "unknown" }, { "name": "75%", "rawType": "object", "type": "unknown" }, { "name": "max", "rawType": "object", "type": "unknown" } ], "ref": "97626024-31ed-4e88-8d10-a524d1d63c40", "rows": [ [ "ID", "150000.0", null, null, null, "75000.5", "43301.414526548666", "1.0", "37500.75", "75000.5", "112500.25", "150000.0" ], [ "Monthly_Revenue", "150000.0", null, null, null, "8451726.379766667", "5291163.126671218", "1500000.0", "4745883.75", "7245678.5", "10830255.25", "82067536.0" ], [ "Net_Profit_Margin (%)", "150000.0", null, null, null, "1.8422716666666668", "15.002406129333442", "-35.0", "-8.43", "2.16", "12.31", "45.0" ], [ "Burn_Rate_Ratio", "150000.0", null, null, null, "0.9698845333333335", "0.14403925496022033", "0.437", "0.869", "0.966", "1.067", "1.55" ], [ "Transaction_Count", "150000.0", null, null, null, "117.76666666666667", "42.61849251746928", "9.0", "86.0", "117.0", "149.0", "285.0" ], [ "Avg_Historical_Rating", "150000.0", null, null, null, "4.061106533333334", "0.5216979171021869", "1.5", "3.77", "4.1", "4.41", "5.0" ], [ "Review_Text", "150000", "45139", "Produk cukup baik, kadang waktu tunggu agak lama.", "11632", null, null, null, null, null, null, null ], [ "Review_Volatility", "150000.0", null, null, null, "0.4072028", "0.16680582218068832", "0.06", "0.278", "0.405", "0.526", "0.99" ], [ "Business_Tenure_Months", "150000.0", null, null, null, "91.00684", "51.10473615861304", "3.0", "47.0", "91.0", "135.0", "179.0" ], [ "Repeat_Order_Rate (%)", "150000.0", null, null, null, "19.98052113333334", "8.021928124041764", "2.0", "14.45", "19.95", "25.43", "54.06" ], [ "Digital_Adoption_Score", "150000.0", null, null, null, "3.5468941333333333", "1.670303095351794", "1.0", "2.26", "3.48", "4.69", "10.0" ], [ "Peak_Hour_Latency", "150000", "3", "Med", "68695", null, null, null, null, null, null, null ], [ "Location_Competitiveness", "150000.0", null, null, null, "8.998806666666667", "2.8286024669263194", "1.0", "7.0", "9.0", "11.0", "23.0" ], [ "Sentiment_Score", "150000.0", null, null, null, "-0.018946333333333332", "0.32053378095387564", "-0.65", "-0.25", "0.0", "0.0", "0.8" ], [ "Class", "150000", "4", "Growth", "85678", null, null, null, null, null, null, null ] ], "shape": { "columns": 11, "rows": 15 } }, "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countuniquetopfreqmeanstdmin25%50%75%max
ID150000.0NaNNaNNaN75000.543301.4145271.037500.7575000.5112500.25150000.0
Monthly_Revenue150000.0NaNNaNNaN8451726.3797675291163.1266711500000.04745883.757245678.510830255.2582067536.0
Net_Profit_Margin (%)150000.0NaNNaNNaN1.84227215.002406-35.0-8.432.1612.3145.0
Burn_Rate_Ratio150000.0NaNNaNNaN0.9698850.1440390.4370.8690.9661.0671.55
Transaction_Count150000.0NaNNaNNaN117.76666742.6184939.086.0117.0149.0285.0
Avg_Historical_Rating150000.0NaNNaNNaN4.0611070.5216981.53.774.14.415.0
Review_Text15000045139Produk cukup baik, kadang waktu tunggu agak lama.11632NaNNaNNaNNaNNaNNaNNaN
Review_Volatility150000.0NaNNaNNaN0.4072030.1668060.060.2780.4050.5260.99
Business_Tenure_Months150000.0NaNNaNNaN91.0068451.1047363.047.091.0135.0179.0
Repeat_Order_Rate (%)150000.0NaNNaNNaN19.9805218.0219282.014.4519.9525.4354.06
Digital_Adoption_Score150000.0NaNNaNNaN3.5468941.6703031.02.263.484.6910.0
Peak_Hour_Latency1500003Med68695NaNNaNNaNNaNNaNNaNNaN
Location_Competitiveness150000.0NaNNaNNaN8.9988072.8286021.07.09.011.023.0
Sentiment_Score150000.0NaNNaNNaN-0.0189460.320534-0.65-0.250.00.00.8
Class1500004Growth85678NaNNaNNaNNaNNaNNaNNaN
\n", "
" ], "text/plain": [ " count unique \\\n", "ID 150000.0 NaN \n", "Monthly_Revenue 150000.0 NaN \n", "Net_Profit_Margin (%) 150000.0 NaN \n", "Burn_Rate_Ratio 150000.0 NaN \n", "Transaction_Count 150000.0 NaN \n", "Avg_Historical_Rating 150000.0 NaN \n", "Review_Text 150000 45139 \n", "Review_Volatility 150000.0 NaN \n", "Business_Tenure_Months 150000.0 NaN \n", "Repeat_Order_Rate (%) 150000.0 NaN \n", "Digital_Adoption_Score 150000.0 NaN \n", "Peak_Hour_Latency 150000 3 \n", "Location_Competitiveness 150000.0 NaN \n", "Sentiment_Score 150000.0 NaN \n", "Class 150000 4 \n", "\n", " top \\\n", "ID NaN \n", "Monthly_Revenue NaN \n", "Net_Profit_Margin (%) NaN \n", "Burn_Rate_Ratio NaN \n", "Transaction_Count NaN \n", "Avg_Historical_Rating NaN \n", "Review_Text Produk cukup baik, kadang waktu tunggu agak lama. \n", "Review_Volatility NaN \n", "Business_Tenure_Months NaN \n", "Repeat_Order_Rate (%) NaN \n", "Digital_Adoption_Score NaN \n", "Peak_Hour_Latency Med \n", "Location_Competitiveness NaN \n", "Sentiment_Score NaN \n", "Class Growth \n", "\n", " freq mean std min \\\n", "ID NaN 75000.5 43301.414527 1.0 \n", "Monthly_Revenue NaN 8451726.379767 5291163.126671 1500000.0 \n", "Net_Profit_Margin (%) NaN 1.842272 15.002406 -35.0 \n", "Burn_Rate_Ratio NaN 0.969885 0.144039 0.437 \n", "Transaction_Count NaN 117.766667 42.618493 9.0 \n", "Avg_Historical_Rating NaN 4.061107 0.521698 1.5 \n", "Review_Text 11632 NaN NaN NaN \n", "Review_Volatility NaN 0.407203 0.166806 0.06 \n", "Business_Tenure_Months NaN 91.00684 51.104736 3.0 \n", "Repeat_Order_Rate (%) NaN 19.980521 8.021928 2.0 \n", "Digital_Adoption_Score NaN 3.546894 1.670303 1.0 \n", "Peak_Hour_Latency 68695 NaN NaN NaN \n", "Location_Competitiveness NaN 8.998807 2.828602 1.0 \n", "Sentiment_Score NaN -0.018946 0.320534 -0.65 \n", "Class 85678 NaN NaN NaN \n", "\n", " 25% 50% 75% max \n", "ID 37500.75 75000.5 112500.25 150000.0 \n", "Monthly_Revenue 4745883.75 7245678.5 10830255.25 82067536.0 \n", "Net_Profit_Margin (%) -8.43 2.16 12.31 45.0 \n", "Burn_Rate_Ratio 0.869 0.966 1.067 1.55 \n", "Transaction_Count 86.0 117.0 149.0 285.0 \n", "Avg_Historical_Rating 3.77 4.1 4.41 5.0 \n", "Review_Text NaN NaN NaN NaN \n", "Review_Volatility 0.278 0.405 0.526 0.99 \n", "Business_Tenure_Months 47.0 91.0 135.0 179.0 \n", "Repeat_Order_Rate (%) 14.45 19.95 25.43 54.06 \n", "Digital_Adoption_Score 2.26 3.48 4.69 10.0 \n", "Peak_Hour_Latency NaN NaN NaN NaN \n", "Location_Competitiveness 7.0 9.0 11.0 23.0 \n", "Sentiment_Score -0.25 0.0 0.0 0.8 \n", "Class NaN NaN NaN NaN " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Class counts:\n", "Class\n", "Growth 85678\n", "Struggling 41571\n", "Critical 12561\n", "Elite 10190\n", "Name: count, dtype: int64\n" ] } ], "source": [ "import random\n", "from typing import List\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from faker import Faker\n", "\n", "# Reproducibility\n", "SEED = 42\n", "np.random.seed(SEED)\n", "random.seed(SEED)\n", "fake = Faker(\"id_ID\")\n", "Faker.seed(SEED)\n", "\n", "# Config\n", "N_SAMPLES = 150000\n", "OUTPUT_CSV = \"synthetic_umkm_data.csv\"\n", "\n", "# Review templates aligned with sentiment\n", "POSITIVE_REVIEWS = [\n", " \"Pelayanan cepat dan ramah, pesanan selalu tepat.\",\n", " \"Kualitas produk konsisten, harga masih masuk akal.\",\n", " \"Aplikasi pemesanan mudah dipakai dan responsif.\",\n", " \"Pengiriman cepat, admin komunikatif.\",\n", " \"Selalu repeat order karena kualitasnya terjaga.\",\n", " \"Transaksi digital lancar, proses checkout tidak ribet.\",\n", "]\n", "\n", "NEUTRAL_REVIEWS = [\n", " \"Produk cukup baik, kadang waktu tunggu agak lama.\",\n", " \"Pelayanan standar, masih bisa ditingkatkan.\",\n", " \"Harga dan kualitas seimbang, pengalaman biasa saja.\",\n", " \"Kadang stok kosong saat jam ramai.\",\n", " \"Secara umum oke, hanya respon chat kadang lambat.\",\n", "]\n", "\n", "NEGATIVE_REVIEWS = [\n", " \"Pesanan sering terlambat saat jam sibuk.\",\n", " \"Kualitas tidak konsisten, kadang bagus kadang kurang.\",\n", " \"Respons admin lambat dan informasi kurang jelas.\",\n", " \"Proses pembayaran sering bermasalah.\",\n", " \"Harga naik tapi layanan tidak membaik.\",\n", " \"Sudah beberapa kali order, pengalaman makin menurun.\",\n", "]\n", "\n", "\n", "def clamp(x: np.ndarray, low: float, high: float) -> np.ndarray:\n", " return np.clip(x, low, high)\n", "\n", "\n", "def pick_review(rating: float, volatility: float, latency: str) -> str:\n", " \"\"\"Generate review text coherent with quality signal.\"\"\"\n", " base_pool: List[str]\n", "\n", " if rating >= 4.2 and volatility < 0.45 and latency == \"Low\":\n", " base_pool = POSITIVE_REVIEWS\n", " elif rating < 3.4 or latency == \"High\":\n", " base_pool = NEGATIVE_REVIEWS\n", " else:\n", " base_pool = NEUTRAL_REVIEWS\n", "\n", " text = random.choice(base_pool)\n", "\n", " # Add slight random variation so reviews don't look templated\n", " if random.random() < 0.3:\n", " text += \" \" + fake.sentence(nb_words=6)\n", " return text\n", "\n", "\n", "def calculate_sentiment_score(review_text: str) -> float:\n", " \"\"\"Convert review text into sentiment score in range [-1.0, 1.0].\"\"\"\n", " review_lower = review_text.lower()\n", "\n", " positive_keywords = {\n", " \"cepat\": 0.30,\n", " \"ramah\": 0.30,\n", " \"mudah\": 0.25,\n", " \"responsif\": 0.30,\n", " \"lancar\": 0.25,\n", " \"komunikatif\": 0.25,\n", " \"terjaga\": 0.25,\n", " \"konsisten\": 0.20,\n", " \"tepat\": 0.20,\n", " }\n", " negative_keywords = {\n", " \"lambat\": -0.30,\n", " \"tidak\": -0.20,\n", " \"kurang\": -0.25,\n", " \"bermasalah\": -0.35,\n", " \"terlambat\": -0.35,\n", " \"ribet\": -0.30,\n", " \"buruk\": -0.40,\n", " \"menurun\": -0.30,\n", " }\n", "\n", " score = 0.0\n", " for word, weight in positive_keywords.items():\n", " if word in review_lower:\n", " score += weight\n", " for word, weight in negative_keywords.items():\n", " if word in review_lower:\n", " score += weight\n", "\n", " return float(clamp(np.array([score]), -1.0, 1.0)[0])\n", "\n", "\n", "# 1) Business maturity and competitiveness\n", "business_tenure = np.random.randint(3, 180, size=N_SAMPLES) # months\n", "location_competitiveness = np.random.poisson(lam=8, size=N_SAMPLES) + 1\n", "\n", "# 2) Digital adoption (1-10), positively related with tenure (up to a limit)\n", "base_digital = 3.3 + 0.02 * np.sqrt(business_tenure)\n", "noise_digital = np.random.normal(0, 1.8, N_SAMPLES)\n", "digital_adoption = clamp(base_digital + noise_digital, 1, 10)\n", "\n", "# 3) Transaction count depends on maturity, digital, and local competition\n", "transaction_lambda = (\n", " 50\n", " + 0.65 * business_tenure\n", " + 8.5 * digital_adoption\n", " - 2.4 * location_competitiveness\n", " + np.random.normal(0, 18, N_SAMPLES)\n", ")\n", "transaction_lambda = clamp(transaction_lambda, 20, 900)\n", "transaction_count = np.random.poisson(transaction_lambda).astype(int)\n", "transaction_count = np.maximum(transaction_count, 5)\n", "\n", "# 4) Average order value (AOV) and monthly revenue\n", "aov = np.random.lognormal(mean=np.log(65000), sigma=0.45, size=N_SAMPLES)\n", "aov = clamp(aov, 12000, 450000)\n", "\n", "monthly_revenue = transaction_count * aov\n", "seasonality_noise = np.random.normal(1.0, 0.08, N_SAMPLES)\n", "monthly_revenue = monthly_revenue * seasonality_noise\n", "monthly_revenue = clamp(monthly_revenue, 1_500_000, 850_000_000)\n", "\n", "# 5) Peak hour latency category influenced by transaction pressure and digital adoption\n", "latency_score = (\n", " 0.0045 * transaction_count\n", " - 0.28 * digital_adoption\n", " + 0.09 * location_competitiveness\n", " + np.random.normal(0, 0.9, N_SAMPLES)\n", ")\n", "\n", "peak_hour_latency = np.where(\n", " latency_score < 0.0,\n", " \"Low\",\n", " np.where(latency_score < 1.3, \"Med\", \"High\")\n", ")\n", "\n", "# 6) Burn rate ratio (expense/revenue)\n", "latency_penalty = np.select(\n", " [peak_hour_latency == \"Low\", peak_hour_latency == \"Med\", peak_hour_latency == \"High\"],\n", " [0.0, 0.10, 0.22],\n", " default=0.10,\n", ")\n", "\n", "burn_rate_ratio = (\n", " 0.80\n", " + 0.015 * location_competitiveness\n", " - 0.014 * digital_adoption\n", " + latency_penalty\n", " + np.random.normal(0, 0.10, N_SAMPLES)\n", ")\n", "burn_rate_ratio = clamp(burn_rate_ratio, 0.40, 1.55)\n", "\n", "# 7) Net profit margin (%), inverse relation with burn rate\n", "net_profit_margin = (\n", " (1 - burn_rate_ratio) * 100\n", " + 0.55 * (digital_adoption - 5)\n", " - 0.18 * np.log1p(location_competitiveness)\n", " + np.random.normal(0, 3.2, N_SAMPLES)\n", ")\n", "net_profit_margin = clamp(net_profit_margin, -35, 45)\n", "\n", "# 8) Repeat order rate (%), boosted by digital adoption and tenure\n", "repeat_order_rate = (\n", " 16\n", " + 1.9 * digital_adoption\n", " + 0.03 * business_tenure\n", " - 0.6 * location_competitiveness\n", " + np.random.normal(0, 7.0, N_SAMPLES)\n", ")\n", "repeat_order_rate = clamp(repeat_order_rate, 2, 90)\n", "\n", "# 9) Review volatility\n", "review_volatility = (\n", " 0.24\n", " + 0.18 * (peak_hour_latency == \"Med\").astype(float)\n", " + 0.34 * (peak_hour_latency == \"High\").astype(float)\n", " + 0.06 * (burn_rate_ratio > 1.0).astype(float)\n", " + np.random.normal(0, 0.09, N_SAMPLES)\n", ")\n", "review_volatility = clamp(review_volatility, 0.06, 1.30)\n", "\n", "# 10) Average historical rating (1-5)\n", "avg_historical_rating = (\n", " 3.95\n", " + 0.08 * digital_adoption\n", " + 0.016 * net_profit_margin\n", " - 0.38 * review_volatility\n", " - 0.12 * (peak_hour_latency == \"High\").astype(float)\n", " + np.random.normal(0, 0.26, N_SAMPLES)\n", ")\n", "avg_historical_rating = clamp(avg_historical_rating, 1.0, 5.0)\n", "\n", "# 11) Review text generation coherent with rating/volatility/latency\n", "review_text = [\n", " pick_review(rating=r, volatility=v, latency=l)\n", " for r, v, l in zip(avg_historical_rating, review_volatility, peak_hour_latency)\n", "]\n", "\n", "# 12) Sentiment score derived from review text\n", "sentiment_scores = np.array([calculate_sentiment_score(text) for text in review_text])\n", "\n", "# Optional: post-adjustment for severe deficit businesses\n", "deficit_mask = burn_rate_ratio > 1.25\n", "avg_historical_rating[deficit_mask] = np.minimum(\n", " avg_historical_rating[deficit_mask],\n", " np.random.uniform(1.5, 3.5, deficit_mask.sum()),\n", ")\n", "repeat_order_rate[deficit_mask] = np.minimum(\n", " repeat_order_rate[deficit_mask],\n", " np.random.uniform(3, 30, deficit_mask.sum()),\n", ")\n", "\n", "# 13) Target Class with percentile-based thresholds (balanced by design)\n", "target_class = np.full(N_SAMPLES, \"Growth\", dtype=object)\n", "\n", "elite_mask = (\n", " (net_profit_margin > np.percentile(net_profit_margin, 70))\n", " & (burn_rate_ratio < np.percentile(burn_rate_ratio, 25))\n", " & (repeat_order_rate > np.percentile(repeat_order_rate, 70))\n", " & (avg_historical_rating > np.percentile(avg_historical_rating, 75))\n", ")\n", "\n", "critical_mask = (\n", " (burn_rate_ratio > np.percentile(burn_rate_ratio, 92))\n", " | ((business_tenure < 7) & (location_competitiveness >= 12))\n", " | ((net_profit_margin < np.percentile(net_profit_margin, 5)) & (avg_historical_rating < 3.0))\n", ")\n", "\n", "struggling_mask = (\n", " ((net_profit_margin < np.percentile(net_profit_margin, 35)) & (burn_rate_ratio > np.percentile(burn_rate_ratio, 60)))\n", " | ((peak_hour_latency == \"High\") & (avg_historical_rating < np.percentile(avg_historical_rating, 40)))\n", " | ((burn_rate_ratio > np.percentile(burn_rate_ratio, 75)) & (avg_historical_rating < np.percentile(avg_historical_rating, 65)))\n", ")\n", "\n", "target_class[elite_mask] = \"Elite\"\n", "target_class[struggling_mask] = \"Struggling\"\n", "target_class[critical_mask] = \"Critical\"\n", "\n", "# Final DataFrame (class at the rightmost position)\n", "df = pd.DataFrame(\n", " {\n", " \"ID\": np.arange(1, N_SAMPLES + 1),\n", " \"Monthly_Revenue\": np.round(monthly_revenue, 0).astype(int),\n", " \"Net_Profit_Margin (%)\": np.round(net_profit_margin, 2),\n", " \"Burn_Rate_Ratio\": np.round(burn_rate_ratio, 3),\n", " \"Transaction_Count\": transaction_count.astype(int),\n", " \"Avg_Historical_Rating\": np.round(avg_historical_rating, 2),\n", " \"Review_Text\": review_text,\n", " \"Review_Volatility\": np.round(review_volatility, 3),\n", " \"Business_Tenure_Months\": business_tenure.astype(int),\n", " \"Repeat_Order_Rate (%)\": np.round(repeat_order_rate, 2),\n", " \"Digital_Adoption_Score\": np.round(digital_adoption, 2),\n", " \"Peak_Hour_Latency\": peak_hour_latency,\n", " \"Location_Competitiveness\": location_competitiveness.astype(int),\n", " \"Sentiment_Score\": np.round(sentiment_scores, 3),\n", " \"Class\": target_class,\n", " }\n", ")\n", "\n", "# Save and preview\n", "df.to_csv(OUTPUT_CSV, index=False)\n", "\n", "print(f\"Generated {len(df)} rows -> {OUTPUT_CSV}\")\n", "print(\"\\nPreview:\")\n", "display(df.head(10))\n", "\n", "print(\"\\nSummary stats:\")\n", "display(df.describe(include=\"all\").transpose())\n", "\n", "print(\"\\nClass counts:\")\n", "print(df[\"Class\"].value_counts())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }