Spaces:
Running
Running
Upload 2 files
Browse files- dataanalysis.ipynb +0 -0
- datacreation.ipynb +1076 -0
dataanalysis.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
datacreation.ipynb
ADDED
|
@@ -0,0 +1,1076 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {
|
| 6 |
+
"id": "4ba6aba8"
|
| 7 |
+
},
|
| 8 |
+
"source": [
|
| 9 |
+
"# π€ **Data Collection, Creation, Storage, and Processing**\n"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "markdown",
|
| 14 |
+
"metadata": {
|
| 15 |
+
"id": "jpASMyIQMaAq"
|
| 16 |
+
},
|
| 17 |
+
"source": [
|
| 18 |
+
"## **1.** π¦ Install required packages"
|
| 19 |
+
]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"cell_type": "code",
|
| 23 |
+
"execution_count": null,
|
| 24 |
+
"metadata": {
|
| 25 |
+
"colab": {
|
| 26 |
+
"base_uri": "https://localhost:8080/"
|
| 27 |
+
},
|
| 28 |
+
"id": "f48c8f8c",
|
| 29 |
+
"outputId": "7e50dd72-2e2a-47b1-e59b-ed7c5511d9cd"
|
| 30 |
+
},
|
| 31 |
+
"outputs": [
|
| 32 |
+
{
|
| 33 |
+
"output_type": "stream",
|
| 34 |
+
"name": "stdout",
|
| 35 |
+
"text": [
|
| 36 |
+
"Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.12/dist-packages (4.13.5)\n",
|
| 37 |
+
"Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (2.2.2)\n",
|
| 38 |
+
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.12/dist-packages (3.10.0)\n",
|
| 39 |
+
"Requirement already satisfied: seaborn in /usr/local/lib/python3.12/dist-packages (0.13.2)\n",
|
| 40 |
+
"Requirement already satisfied: numpy in /usr/local/lib/python3.12/dist-packages (2.0.2)\n",
|
| 41 |
+
"Requirement already satisfied: textblob in /usr/local/lib/python3.12/dist-packages (0.19.0)\n",
|
| 42 |
+
"Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.12/dist-packages (from beautifulsoup4) (2.8.3)\n",
|
| 43 |
+
"Requirement already satisfied: typing-extensions>=4.0.0 in /usr/local/lib/python3.12/dist-packages (from beautifulsoup4) (4.15.0)\n",
|
| 44 |
+
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas) (2.9.0.post0)\n",
|
| 45 |
+
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.2)\n",
|
| 46 |
+
"Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.3)\n",
|
| 47 |
+
"Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (1.3.3)\n",
|
| 48 |
+
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (0.12.1)\n",
|
| 49 |
+
"Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (4.62.1)\n",
|
| 50 |
+
"Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (1.5.0)\n",
|
| 51 |
+
"Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (26.0)\n",
|
| 52 |
+
"Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (11.3.0)\n",
|
| 53 |
+
"Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (3.3.2)\n",
|
| 54 |
+
"Requirement already satisfied: nltk>=3.9 in /usr/local/lib/python3.12/dist-packages (from textblob) (3.9.1)\n",
|
| 55 |
+
"Requirement already satisfied: click in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (8.3.1)\n",
|
| 56 |
+
"Requirement already satisfied: joblib in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (1.5.3)\n",
|
| 57 |
+
"Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (2025.11.3)\n",
|
| 58 |
+
"Requirement already satisfied: tqdm in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (4.67.3)\n",
|
| 59 |
+
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)\n"
|
| 60 |
+
]
|
| 61 |
+
}
|
| 62 |
+
],
|
| 63 |
+
"source": [
|
| 64 |
+
"!pip install beautifulsoup4 pandas matplotlib seaborn numpy textblob"
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"cell_type": "markdown",
|
| 69 |
+
"metadata": {
|
| 70 |
+
"id": "lquNYCbfL9IM"
|
| 71 |
+
},
|
| 72 |
+
"source": [
|
| 73 |
+
"## **2.** β Load the Superstore dataset from Kaggle\n",
|
| 74 |
+
"\n",
|
| 75 |
+
"Dataset source: [Superstore Dataset β Kaggle](https://www.kaggle.com/datasets/vivek468/superstore-dataset-final?resource=download)"
|
| 76 |
+
]
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"cell_type": "markdown",
|
| 80 |
+
"metadata": {
|
| 81 |
+
"id": "0IWuNpxxYDJF"
|
| 82 |
+
},
|
| 83 |
+
"source": [
|
| 84 |
+
"### *a. Initial setup*\n",
|
| 85 |
+
"Define the base url of the dataset source as well as how and what you will load"
|
| 86 |
+
]
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"cell_type": "code",
|
| 90 |
+
"execution_count": null,
|
| 91 |
+
"metadata": {
|
| 92 |
+
"id": "91d52125"
|
| 93 |
+
},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"import requests\n",
|
| 97 |
+
"from bs4 import BeautifulSoup\n",
|
| 98 |
+
"import pandas as pd\n",
|
| 99 |
+
"import time\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"# Dataset source\n",
|
| 102 |
+
"base_url = \"https://www.kaggle.com/datasets/vivek468/superstore-dataset-final?resource=download\"\n",
|
| 103 |
+
"headers = {\"User-Agent\": \"Mozilla/5.0\"}\n",
|
| 104 |
+
"\n",
|
| 105 |
+
"df_raw = pd.read_csv(\"Sample - Superstore.csv\", encoding=\"latin-1\")\n",
|
| 106 |
+
"df_raw[\"Order Date\"] = pd.to_datetime(df_raw[\"Order Date\"], format=\"%m/%d/%Y\")\n",
|
| 107 |
+
"df_raw[\"Ship Date\"] = pd.to_datetime(df_raw[\"Ship Date\"], format=\"%m/%d/%Y\")\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"sub_categories, avg_prices, avg_profits = [], [], []"
|
| 110 |
+
]
|
| 111 |
+
},
|
| 112 |
+
{
|
| 113 |
+
"cell_type": "markdown",
|
| 114 |
+
"metadata": {
|
| 115 |
+
"id": "oCdTsin2Yfp3"
|
| 116 |
+
},
|
| 117 |
+
"source": [
|
| 118 |
+
"### *b. Fill sub_categories, avg_prices, and avg_profits from the dataset*"
|
| 119 |
+
]
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"cell_type": "code",
|
| 123 |
+
"execution_count": null,
|
| 124 |
+
"metadata": {
|
| 125 |
+
"id": "xqO5Y3dnYhxt"
|
| 126 |
+
},
|
| 127 |
+
"outputs": [],
|
| 128 |
+
"source": [
|
| 129 |
+
"# Aggregate over all Sub-Categories\n",
|
| 130 |
+
"for sub_cat, group in df_raw.groupby(\"Sub-Category\"):\n",
|
| 131 |
+
" sub_categories.append(sub_cat)\n",
|
| 132 |
+
" avg_prices.append(round(group[\"Sales\"].sum() / group[\"Quantity\"].sum(), 2))\n",
|
| 133 |
+
" avg_profits.append(round(group[\"Profit\"].mean(), 2))\n",
|
| 134 |
+
"\n",
|
| 135 |
+
" time.sleep(0) # kept for structural parity"
|
| 136 |
+
]
|
| 137 |
+
},
|
| 138 |
+
{
|
| 139 |
+
"cell_type": "markdown",
|
| 140 |
+
"metadata": {
|
| 141 |
+
"id": "T0TOeRC4Yrnn"
|
| 142 |
+
},
|
| 143 |
+
"source": [
|
| 144 |
+
"### *c. βπ»πβοΈ Create a dataframe df_products that contains the now complete \"sub_category\", \"avg_price\", and \"avg_profit\" objects*"
|
| 145 |
+
]
|
| 146 |
+
},
|
| 147 |
+
{
|
| 148 |
+
"cell_type": "code",
|
| 149 |
+
"execution_count": null,
|
| 150 |
+
"metadata": {
|
| 151 |
+
"id": "l5FkkNhUYTHh"
|
| 152 |
+
},
|
| 153 |
+
"outputs": [],
|
| 154 |
+
"source": [
|
| 155 |
+
"# ποΈ Create DataFrame\n",
|
| 156 |
+
"df_products = pd.DataFrame({\n",
|
| 157 |
+
" \"sub_category\": sub_categories,\n",
|
| 158 |
+
" \"avg_price\": avg_prices,\n",
|
| 159 |
+
" \"avg_profit\": avg_profits\n",
|
| 160 |
+
"})"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "markdown",
|
| 165 |
+
"metadata": {
|
| 166 |
+
"id": "duI5dv3CZYvF"
|
| 167 |
+
},
|
| 168 |
+
"source": [
|
| 169 |
+
"### *d. Save dataframe either as a CSV or Excel file*"
|
| 170 |
+
]
|
| 171 |
+
},
|
| 172 |
+
{
|
| 173 |
+
"cell_type": "code",
|
| 174 |
+
"execution_count": null,
|
| 175 |
+
"metadata": {
|
| 176 |
+
"id": "lC1U_YHtZifh"
|
| 177 |
+
},
|
| 178 |
+
"outputs": [],
|
| 179 |
+
"source": [
|
| 180 |
+
"# πΎ Save to CSV\n",
|
| 181 |
+
"df_products.to_csv(\"superstore_data.csv\", index=False)\n"
|
| 182 |
+
]
|
| 183 |
+
},
|
| 184 |
+
{
|
| 185 |
+
"cell_type": "markdown",
|
| 186 |
+
"metadata": {
|
| 187 |
+
"id": "qMjRKMBQZlJi"
|
| 188 |
+
},
|
| 189 |
+
"source": [
|
| 190 |
+
"### *e. βπ»πβοΈ View first few lines*"
|
| 191 |
+
]
|
| 192 |
+
},
|
| 193 |
+
{
|
| 194 |
+
"cell_type": "code",
|
| 195 |
+
"execution_count": null,
|
| 196 |
+
"metadata": {
|
| 197 |
+
"colab": {
|
| 198 |
+
"base_uri": "https://localhost:8080/",
|
| 199 |
+
"height": 206
|
| 200 |
+
},
|
| 201 |
+
"id": "O_wIvTxYZqCK",
|
| 202 |
+
"outputId": "5c622472-47de-4352-daa9-c7da226d0c30"
|
| 203 |
+
},
|
| 204 |
+
"outputs": [
|
| 205 |
+
{
|
| 206 |
+
"output_type": "execute_result",
|
| 207 |
+
"data": {
|
| 208 |
+
"text/plain": [
|
| 209 |
+
" sub_category avg_price avg_profit\n",
|
| 210 |
+
"0 Accessories 57.42 55.81\n",
|
| 211 |
+
"1 Appliances 62.69 38.47\n",
|
| 212 |
+
"2 Art 9.09 8.15\n",
|
| 213 |
+
"3 Binders 33.07 20.72\n",
|
| 214 |
+
"4 Bookcases 124.61 -19.17"
|
| 215 |
+
],
|
| 216 |
+
"text/html": [
|
| 217 |
+
"\n",
|
| 218 |
+
" <div id=\"df-511757e8-63e2-4e0d-9c9c-fd889e0a92a4\" class=\"colab-df-container\">\n",
|
| 219 |
+
" <div>\n",
|
| 220 |
+
"<style scoped>\n",
|
| 221 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 222 |
+
" vertical-align: middle;\n",
|
| 223 |
+
" }\n",
|
| 224 |
+
"\n",
|
| 225 |
+
" .dataframe tbody tr th {\n",
|
| 226 |
+
" vertical-align: top;\n",
|
| 227 |
+
" }\n",
|
| 228 |
+
"\n",
|
| 229 |
+
" .dataframe thead th {\n",
|
| 230 |
+
" text-align: right;\n",
|
| 231 |
+
" }\n",
|
| 232 |
+
"</style>\n",
|
| 233 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 234 |
+
" <thead>\n",
|
| 235 |
+
" <tr style=\"text-align: right;\">\n",
|
| 236 |
+
" <th></th>\n",
|
| 237 |
+
" <th>sub_category</th>\n",
|
| 238 |
+
" <th>avg_price</th>\n",
|
| 239 |
+
" <th>avg_profit</th>\n",
|
| 240 |
+
" </tr>\n",
|
| 241 |
+
" </thead>\n",
|
| 242 |
+
" <tbody>\n",
|
| 243 |
+
" <tr>\n",
|
| 244 |
+
" <th>0</th>\n",
|
| 245 |
+
" <td>Accessories</td>\n",
|
| 246 |
+
" <td>57.42</td>\n",
|
| 247 |
+
" <td>55.81</td>\n",
|
| 248 |
+
" </tr>\n",
|
| 249 |
+
" <tr>\n",
|
| 250 |
+
" <th>1</th>\n",
|
| 251 |
+
" <td>Appliances</td>\n",
|
| 252 |
+
" <td>62.69</td>\n",
|
| 253 |
+
" <td>38.47</td>\n",
|
| 254 |
+
" </tr>\n",
|
| 255 |
+
" <tr>\n",
|
| 256 |
+
" <th>2</th>\n",
|
| 257 |
+
" <td>Art</td>\n",
|
| 258 |
+
" <td>9.09</td>\n",
|
| 259 |
+
" <td>8.15</td>\n",
|
| 260 |
+
" </tr>\n",
|
| 261 |
+
" <tr>\n",
|
| 262 |
+
" <th>3</th>\n",
|
| 263 |
+
" <td>Binders</td>\n",
|
| 264 |
+
" <td>33.07</td>\n",
|
| 265 |
+
" <td>20.72</td>\n",
|
| 266 |
+
" </tr>\n",
|
| 267 |
+
" <tr>\n",
|
| 268 |
+
" <th>4</th>\n",
|
| 269 |
+
" <td>Bookcases</td>\n",
|
| 270 |
+
" <td>124.61</td>\n",
|
| 271 |
+
" <td>-19.17</td>\n",
|
| 272 |
+
" </tr>\n",
|
| 273 |
+
" </tbody>\n",
|
| 274 |
+
"</table>\n",
|
| 275 |
+
"</div>\n",
|
| 276 |
+
" <div class=\"colab-df-buttons\">\n",
|
| 277 |
+
"\n",
|
| 278 |
+
" <div class=\"colab-df-container\">\n",
|
| 279 |
+
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-511757e8-63e2-4e0d-9c9c-fd889e0a92a4')\"\n",
|
| 280 |
+
" title=\"Convert this dataframe to an interactive table.\"\n",
|
| 281 |
+
" style=\"display:none;\">\n",
|
| 282 |
+
"\n",
|
| 283 |
+
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
|
| 284 |
+
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
|
| 285 |
+
" </svg>\n",
|
| 286 |
+
" </button>\n",
|
| 287 |
+
"\n",
|
| 288 |
+
" <style>\n",
|
| 289 |
+
" .colab-df-container {\n",
|
| 290 |
+
" display:flex;\n",
|
| 291 |
+
" gap: 12px;\n",
|
| 292 |
+
" }\n",
|
| 293 |
+
"\n",
|
| 294 |
+
" .colab-df-convert {\n",
|
| 295 |
+
" background-color: #E8F0FE;\n",
|
| 296 |
+
" border: none;\n",
|
| 297 |
+
" border-radius: 50%;\n",
|
| 298 |
+
" cursor: pointer;\n",
|
| 299 |
+
" display: none;\n",
|
| 300 |
+
" fill: #1967D2;\n",
|
| 301 |
+
" height: 32px;\n",
|
| 302 |
+
" padding: 0 0 0 0;\n",
|
| 303 |
+
" width: 32px;\n",
|
| 304 |
+
" }\n",
|
| 305 |
+
"\n",
|
| 306 |
+
" .colab-df-convert:hover {\n",
|
| 307 |
+
" background-color: #E2EBFA;\n",
|
| 308 |
+
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
|
| 309 |
+
" fill: #174EA6;\n",
|
| 310 |
+
" }\n",
|
| 311 |
+
"\n",
|
| 312 |
+
" .colab-df-buttons div {\n",
|
| 313 |
+
" margin-bottom: 4px;\n",
|
| 314 |
+
" }\n",
|
| 315 |
+
"\n",
|
| 316 |
+
" [theme=dark] .colab-df-convert {\n",
|
| 317 |
+
" background-color: #3B4455;\n",
|
| 318 |
+
" fill: #D2E3FC;\n",
|
| 319 |
+
" }\n",
|
| 320 |
+
"\n",
|
| 321 |
+
" [theme=dark] .colab-df-convert:hover {\n",
|
| 322 |
+
" background-color: #434B5C;\n",
|
| 323 |
+
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
|
| 324 |
+
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
|
| 325 |
+
" fill: #FFFFFF;\n",
|
| 326 |
+
" }\n",
|
| 327 |
+
" </style>\n",
|
| 328 |
+
"\n",
|
| 329 |
+
" <script>\n",
|
| 330 |
+
" const buttonEl =\n",
|
| 331 |
+
" document.querySelector('#df-511757e8-63e2-4e0d-9c9c-fd889e0a92a4 button.colab-df-convert');\n",
|
| 332 |
+
" buttonEl.style.display =\n",
|
| 333 |
+
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
|
| 334 |
+
"\n",
|
| 335 |
+
" async function convertToInteractive(key) {\n",
|
| 336 |
+
" const element = document.querySelector('#df-511757e8-63e2-4e0d-9c9c-fd889e0a92a4');\n",
|
| 337 |
+
" const dataTable =\n",
|
| 338 |
+
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
|
| 339 |
+
" [key], {});\n",
|
| 340 |
+
" if (!dataTable) return;\n",
|
| 341 |
+
"\n",
|
| 342 |
+
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
|
| 343 |
+
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
|
| 344 |
+
" + ' to learn more about interactive tables.';\n",
|
| 345 |
+
" element.innerHTML = '';\n",
|
| 346 |
+
" dataTable['output_type'] = 'display_data';\n",
|
| 347 |
+
" await google.colab.output.renderOutput(dataTable, element);\n",
|
| 348 |
+
" const docLink = document.createElement('div');\n",
|
| 349 |
+
" docLink.innerHTML = docLinkHtml;\n",
|
| 350 |
+
" element.appendChild(docLink);\n",
|
| 351 |
+
" }\n",
|
| 352 |
+
" </script>\n",
|
| 353 |
+
" </div>\n",
|
| 354 |
+
"\n",
|
| 355 |
+
"\n",
|
| 356 |
+
" </div>\n",
|
| 357 |
+
" </div>\n"
|
| 358 |
+
],
|
| 359 |
+
"application/vnd.google.colaboratory.intrinsic+json": {
|
| 360 |
+
"type": "dataframe",
|
| 361 |
+
"variable_name": "df_products",
|
| 362 |
+
"summary": "{\n \"name\": \"df_products\",\n \"rows\": 17,\n \"fields\": [\n {\n \"column\": \"sub_category\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 17,\n \"samples\": [\n \"Accessories\",\n \"Appliances\",\n \"Chairs\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"avg_price\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 171.03515695739625,\n \"min\": 3.37,\n \"max\": 651.79,\n \"num_unique_values\": 17,\n \"samples\": [\n 57.42,\n 62.69,\n 138.76\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"avg_profit\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 200.76924667278809,\n \"min\": -55.48,\n \"max\": 837.47,\n \"num_unique_values\": 17,\n \"samples\": [\n 55.81,\n 38.47,\n 43.23\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
|
| 363 |
+
}
|
| 364 |
+
},
|
| 365 |
+
"metadata": {},
|
| 366 |
+
"execution_count": 8
|
| 367 |
+
}
|
| 368 |
+
],
|
| 369 |
+
"source": [
|
| 370 |
+
"df_products.head()"
|
| 371 |
+
]
|
| 372 |
+
},
|
| 373 |
+
{
|
| 374 |
+
"cell_type": "markdown",
|
| 375 |
+
"metadata": {
|
| 376 |
+
"id": "p-1Pr2szaqLk"
|
| 377 |
+
},
|
| 378 |
+
"source": [
|
| 379 |
+
"## **3.** π§© Create a meaningful connection between real & synthetic datasets"
|
| 380 |
+
]
|
| 381 |
+
},
|
| 382 |
+
{
|
| 383 |
+
"cell_type": "markdown",
|
| 384 |
+
"metadata": {
|
| 385 |
+
"id": "SIaJUGIpaH4V"
|
| 386 |
+
},
|
| 387 |
+
"source": [
|
| 388 |
+
"### *a. Initial setup*"
|
| 389 |
+
]
|
| 390 |
+
},
|
| 391 |
+
{
|
| 392 |
+
"cell_type": "code",
|
| 393 |
+
"execution_count": null,
|
| 394 |
+
"metadata": {
|
| 395 |
+
"id": "-gPXGcRPuV_9"
|
| 396 |
+
},
|
| 397 |
+
"outputs": [],
|
| 398 |
+
"source": [
|
| 399 |
+
"import numpy as np\n",
|
| 400 |
+
"import random\n",
|
| 401 |
+
"from datetime import datetime\n",
|
| 402 |
+
"import warnings\n",
|
| 403 |
+
"\n",
|
| 404 |
+
"warnings.filterwarnings(\"ignore\")\n",
|
| 405 |
+
"random.seed(2025)\n",
|
| 406 |
+
"np.random.seed(2025)"
|
| 407 |
+
]
|
| 408 |
+
},
|
| 409 |
+
{
|
| 410 |
+
"cell_type": "markdown",
|
| 411 |
+
"metadata": {
|
| 412 |
+
"id": "pY4yCoIuaQqp"
|
| 413 |
+
},
|
| 414 |
+
"source": [
|
| 415 |
+
"### *b. Generate popularity scores based on avg_profit (with some randomness) with a generate_popularity_score function*"
|
| 416 |
+
]
|
| 417 |
+
},
|
| 418 |
+
{
|
| 419 |
+
"cell_type": "code",
|
| 420 |
+
"execution_count": null,
|
| 421 |
+
"metadata": {
|
| 422 |
+
"id": "mnd5hdAbaNjz"
|
| 423 |
+
},
|
| 424 |
+
"outputs": [],
|
| 425 |
+
"source": [
|
| 426 |
+
"def generate_popularity_score(avg_profit):\n",
|
| 427 |
+
" if avg_profit >= 50:\n",
|
| 428 |
+
" base = 4\n",
|
| 429 |
+
" elif avg_profit >= 10:\n",
|
| 430 |
+
" base = 3\n",
|
| 431 |
+
" elif avg_profit >= 0:\n",
|
| 432 |
+
" base = 2\n",
|
| 433 |
+
" else:\n",
|
| 434 |
+
" base = 1\n",
|
| 435 |
+
" trend_factor = random.choices([-1, 0, 1], weights=[1, 3, 2])[0]\n",
|
| 436 |
+
" return int(np.clip(base + trend_factor, 1, 5))"
|
| 437 |
+
]
|
| 438 |
+
},
|
| 439 |
+
{
|
| 440 |
+
"cell_type": "markdown",
|
| 441 |
+
"metadata": {
|
| 442 |
+
"id": "n4-TaNTFgPak"
|
| 443 |
+
},
|
| 444 |
+
"source": [
|
| 445 |
+
"### *c. βπ»πβοΈ Run the function to create a \"popularity_score\" column from \"avg_profit\"*"
|
| 446 |
+
]
|
| 447 |
+
},
|
| 448 |
+
{
|
| 449 |
+
"cell_type": "code",
|
| 450 |
+
"execution_count": null,
|
| 451 |
+
"metadata": {
|
| 452 |
+
"id": "V-G3OCUCgR07"
|
| 453 |
+
},
|
| 454 |
+
"outputs": [],
|
| 455 |
+
"source": [
|
| 456 |
+
"df_products[\"popularity_score\"] = df_products[\"avg_profit\"].apply(generate_popularity_score)"
|
| 457 |
+
]
|
| 458 |
+
},
|
| 459 |
+
{
|
| 460 |
+
"cell_type": "markdown",
|
| 461 |
+
"metadata": {
|
| 462 |
+
"id": "HnngRNTgacYt"
|
| 463 |
+
},
|
| 464 |
+
"source": [
|
| 465 |
+
"### *d. Decide on the sentiment_label based on the popularity score with a get_sentiment function*"
|
| 466 |
+
]
|
| 467 |
+
},
|
| 468 |
+
{
|
| 469 |
+
"cell_type": "code",
|
| 470 |
+
"execution_count": null,
|
| 471 |
+
"metadata": {
|
| 472 |
+
"id": "kUtWmr8maZLZ"
|
| 473 |
+
},
|
| 474 |
+
"outputs": [],
|
| 475 |
+
"source": [
|
| 476 |
+
"def get_sentiment(popularity_score):\n",
|
| 477 |
+
" if popularity_score <= 2:\n",
|
| 478 |
+
" return \"negative\"\n",
|
| 479 |
+
" elif popularity_score == 3:\n",
|
| 480 |
+
" return \"neutral\"\n",
|
| 481 |
+
" else:\n",
|
| 482 |
+
" return \"positive\""
|
| 483 |
+
]
|
| 484 |
+
},
|
| 485 |
+
{
|
| 486 |
+
"cell_type": "markdown",
|
| 487 |
+
"metadata": {
|
| 488 |
+
"id": "HF9F9HIzgT7Z"
|
| 489 |
+
},
|
| 490 |
+
"source": [
|
| 491 |
+
"### *e. βπ»πβοΈ Run the function to create a \"sentiment_label\" column from \"popularity_score\"*"
|
| 492 |
+
]
|
| 493 |
+
},
|
| 494 |
+
{
|
| 495 |
+
"cell_type": "code",
|
| 496 |
+
"execution_count": null,
|
| 497 |
+
"metadata": {
|
| 498 |
+
"id": "tafQj8_7gYCG"
|
| 499 |
+
},
|
| 500 |
+
"outputs": [],
|
| 501 |
+
"source": [
|
| 502 |
+
"df_products[\"sentiment_label\"] = df_products[\"popularity_score\"].apply(get_sentiment)"
|
| 503 |
+
]
|
| 504 |
+
},
|
| 505 |
+
{
|
| 506 |
+
"cell_type": "markdown",
|
| 507 |
+
"metadata": {
|
| 508 |
+
"id": "T8AdKkmASq9a"
|
| 509 |
+
},
|
| 510 |
+
"source": [
|
| 511 |
+
"## **4.** π Generate synthetic sub-category sales data of 18 months"
|
| 512 |
+
]
|
| 513 |
+
},
|
| 514 |
+
{
|
| 515 |
+
"cell_type": "markdown",
|
| 516 |
+
"metadata": {
|
| 517 |
+
"id": "OhXbdGD5fH0c"
|
| 518 |
+
},
|
| 519 |
+
"source": [
|
| 520 |
+
"### *a. Create a generate_sales_profit function that would generate sales patterns based on sentiment_label (with some randomness)*"
|
| 521 |
+
]
|
| 522 |
+
},
|
| 523 |
+
{
|
| 524 |
+
"cell_type": "code",
|
| 525 |
+
"execution_count": null,
|
| 526 |
+
"metadata": {
|
| 527 |
+
"id": "qkVhYPXGbgEn"
|
| 528 |
+
},
|
| 529 |
+
"outputs": [],
|
| 530 |
+
"source": [
|
| 531 |
+
"def generate_sales_profile(sentiment):\n",
|
| 532 |
+
" months = pd.date_range(end=datetime.today(), periods=18, freq=\"M\")\n",
|
| 533 |
+
"\n",
|
| 534 |
+
" if sentiment == \"positive\":\n",
|
| 535 |
+
" base = random.randint(200, 300)\n",
|
| 536 |
+
" trend = np.linspace(base, base + random.randint(20, 60), len(months))\n",
|
| 537 |
+
" elif sentiment == \"negative\":\n",
|
| 538 |
+
" base = random.randint(20, 80)\n",
|
| 539 |
+
" trend = np.linspace(base, base - random.randint(10, 30), len(months))\n",
|
| 540 |
+
" else: # neutral\n",
|
| 541 |
+
" base = random.randint(80, 160)\n",
|
| 542 |
+
" trend = np.full(len(months), base + random.randint(-10, 10))\n",
|
| 543 |
+
"\n",
|
| 544 |
+
" seasonality = 10 * np.sin(np.linspace(0, 3 * np.pi, len(months)))\n",
|
| 545 |
+
" noise = np.random.normal(0, 5, len(months))\n",
|
| 546 |
+
" monthly_sales = np.clip(trend + seasonality + noise, a_min=0, a_max=None).astype(int)\n",
|
| 547 |
+
"\n",
|
| 548 |
+
" return list(zip(months.strftime(\"%Y-%m\"), monthly_sales))"
|
| 549 |
+
]
|
| 550 |
+
},
|
| 551 |
+
{
|
| 552 |
+
"cell_type": "markdown",
|
| 553 |
+
"metadata": {
|
| 554 |
+
"id": "L2ak1HlcgoTe"
|
| 555 |
+
},
|
| 556 |
+
"source": [
|
| 557 |
+
"### *b. Run the function as part of building sales_data*"
|
| 558 |
+
]
|
| 559 |
+
},
|
| 560 |
+
{
|
| 561 |
+
"cell_type": "code",
|
| 562 |
+
"execution_count": null,
|
| 563 |
+
"metadata": {
|
| 564 |
+
"id": "SlJ24AUafoDB"
|
| 565 |
+
},
|
| 566 |
+
"outputs": [],
|
| 567 |
+
"source": [
|
| 568 |
+
"sales_data = []\n",
|
| 569 |
+
"for _, row in df_products.iterrows():\n",
|
| 570 |
+
" records = generate_sales_profile(row[\"sentiment_label\"])\n",
|
| 571 |
+
" for month, units in records:\n",
|
| 572 |
+
" sales_data.append({\n",
|
| 573 |
+
" \"sub_category\": row[\"sub_category\"],\n",
|
| 574 |
+
" \"month\": month,\n",
|
| 575 |
+
" \"units_sold\": units,\n",
|
| 576 |
+
" \"sentiment_label\": row[\"sentiment_label\"]\n",
|
| 577 |
+
" })"
|
| 578 |
+
]
|
| 579 |
+
},
|
| 580 |
+
{
|
| 581 |
+
"cell_type": "markdown",
|
| 582 |
+
"metadata": {
|
| 583 |
+
"id": "4IXZKcCSgxnq"
|
| 584 |
+
},
|
| 585 |
+
"source": [
|
| 586 |
+
"### *c. βπ»πβοΈ Create a df_sales DataFrame from sales_data*"
|
| 587 |
+
]
|
| 588 |
+
},
|
| 589 |
+
{
|
| 590 |
+
"cell_type": "code",
|
| 591 |
+
"execution_count": null,
|
| 592 |
+
"metadata": {
|
| 593 |
+
"id": "wcN6gtiZg-ws"
|
| 594 |
+
},
|
| 595 |
+
"outputs": [],
|
| 596 |
+
"source": [
|
| 597 |
+
"df_sales = pd.DataFrame(sales_data)"
|
| 598 |
+
]
|
| 599 |
+
},
|
| 600 |
+
{
|
| 601 |
+
"cell_type": "markdown",
|
| 602 |
+
"metadata": {
|
| 603 |
+
"id": "EhIjz9WohAmZ"
|
| 604 |
+
},
|
| 605 |
+
"source": [
|
| 606 |
+
"### *d. Save df_sales as synthetic_sales_data.csv & view first few lines*"
|
| 607 |
+
]
|
| 608 |
+
},
|
| 609 |
+
{
|
| 610 |
+
"cell_type": "code",
|
| 611 |
+
"execution_count": null,
|
| 612 |
+
"metadata": {
|
| 613 |
+
"colab": {
|
| 614 |
+
"base_uri": "https://localhost:8080/"
|
| 615 |
+
},
|
| 616 |
+
"id": "MzbZvLcAhGaH",
|
| 617 |
+
"outputId": "683d930c-27a4-4925-b01f-af8490fa8b9b"
|
| 618 |
+
},
|
| 619 |
+
"outputs": [
|
| 620 |
+
{
|
| 621 |
+
"output_type": "stream",
|
| 622 |
+
"name": "stdout",
|
| 623 |
+
"text": [
|
| 624 |
+
" sub_category month units_sold sentiment_label\n",
|
| 625 |
+
"0 Accessories 2024-10 223 positive\n",
|
| 626 |
+
"1 Accessories 2024-11 234 positive\n",
|
| 627 |
+
"2 Accessories 2024-12 229 positive\n",
|
| 628 |
+
"3 Accessories 2025-01 236 positive\n",
|
| 629 |
+
"4 Accessories 2025-02 239 positive\n"
|
| 630 |
+
]
|
| 631 |
+
}
|
| 632 |
+
],
|
| 633 |
+
"source": [
|
| 634 |
+
"df_sales.to_csv(\"synthetic_sales_data.csv\", index=False)\n",
|
| 635 |
+
"\n",
|
| 636 |
+
"print(df_sales.head())"
|
| 637 |
+
]
|
| 638 |
+
},
|
| 639 |
+
{
|
| 640 |
+
"cell_type": "markdown",
|
| 641 |
+
"metadata": {
|
| 642 |
+
"id": "7g9gqBgQMtJn"
|
| 643 |
+
},
|
| 644 |
+
"source": [
|
| 645 |
+
"## **5.** π― Generate synthetic customer reviews"
|
| 646 |
+
]
|
| 647 |
+
},
|
| 648 |
+
{
|
| 649 |
+
"cell_type": "markdown",
|
| 650 |
+
"metadata": {
|
| 651 |
+
"id": "Gi4y9M9KuDWx"
|
| 652 |
+
},
|
| 653 |
+
"source": [
|
| 654 |
+
"### *a. βπ»πβοΈ Ask ChatGPT to create a list of 50 distinct generic retail product review texts for the sentiment labels \"positive\", \"neutral\", and \"negative\" called synthetic_reviews_by_sentiment*"
|
| 655 |
+
]
|
| 656 |
+
},
|
| 657 |
+
{
|
| 658 |
+
"cell_type": "code",
|
| 659 |
+
"execution_count": null,
|
| 660 |
+
"metadata": {
|
| 661 |
+
"id": "b3cd2a50"
|
| 662 |
+
},
|
| 663 |
+
"outputs": [],
|
| 664 |
+
"source": [
|
| 665 |
+
"synthetic_reviews_by_sentiment = {\n",
|
| 666 |
+
" \"positive\": [\n",
|
| 667 |
+
" \"This product line is exactly what we needed β quality and value combined.\",\n",
|
| 668 |
+
" \"Consistently impressed with the performance across every item in this range.\",\n",
|
| 669 |
+
" \"Our team relies on these products daily β they never let us down.\",\n",
|
| 670 |
+
" \"Top-tier quality that justifies every cent spent.\",\n",
|
| 671 |
+
" \"Outstanding selection that meets every requirement our office has.\",\n",
|
| 672 |
+
" \"Remarkable durability and design. Will definitely reorder.\",\n",
|
| 673 |
+
" \"These products set the bar β nothing else comes close.\",\n",
|
| 674 |
+
" \"Every purchase from this category has exceeded expectations.\",\n",
|
| 675 |
+
" \"Huge uplift in team productivity since we started stocking these.\",\n",
|
| 676 |
+
" \"Perfect balance of price and quality. Highly recommended.\",\n",
|
| 677 |
+
" \"A consistent bestseller in our store β customers keep coming back.\",\n",
|
| 678 |
+
" \"Rarely see products deliver at this level β truly impressive.\",\n",
|
| 679 |
+
" \"Our highest-rated category by a wide margin.\",\n",
|
| 680 |
+
" \"Revenue from this range keeps growing quarter over quarter.\",\n",
|
| 681 |
+
" \"Customers praise reliability and style in equal measure.\",\n",
|
| 682 |
+
" \"Flawless functionality and great aesthetics β perfect combo.\",\n",
|
| 683 |
+
" \"Demand for this sub-category is always strong.\",\n",
|
| 684 |
+
" \"These items turn first-time buyers into loyal repeat customers.\",\n",
|
| 685 |
+
" \"Low return rates and high satisfaction scores tell the whole story.\",\n",
|
| 686 |
+
" \"The profit margins from this category are the envy of the floor.\",\n",
|
| 687 |
+
" \"Never overstocked, never understocked β a category manager's dream.\",\n",
|
| 688 |
+
" \"Positive word of mouth keeps driving new customers our way.\",\n",
|
| 689 |
+
" \"An anchor category that props up overall store performance.\",\n",
|
| 690 |
+
" \"Premium feel at a competitive price β customers notice.\",\n",
|
| 691 |
+
" \"Strong sell-through rate with minimal markdowns needed.\",\n",
|
| 692 |
+
" \"Year-on-year growth in this sub-category continues to impress.\",\n",
|
| 693 |
+
" \"Staff love recommending these β they practically sell themselves.\",\n",
|
| 694 |
+
" \"Margin-accretive with broad appeal across all customer segments.\",\n",
|
| 695 |
+
" \"Dependable supply chain and consistent quality from this range.\",\n",
|
| 696 |
+
" \"A strategic priority that keeps delivering returns.\",\n",
|
| 697 |
+
" \"These products make every planogram look good.\",\n",
|
| 698 |
+
" \"Our highest NPS scores come from buyers of this category.\",\n",
|
| 699 |
+
" \"Inventory turnover in this range is best in class.\",\n",
|
| 700 |
+
" \"High basket attachment β customers rarely buy just one.\",\n",
|
| 701 |
+
" \"Category leadership is clearly visible in the sales data.\",\n",
|
| 702 |
+
" \"Priced right, built right, and always in demand.\",\n",
|
| 703 |
+
" \"Zero complaints this quarter β quality control is excellent.\",\n",
|
| 704 |
+
" \"Seasonal lifts are strong and predictable for this sub-category.\",\n",
|
| 705 |
+
" \"The data backs what the floor team feels: this range is thriving.\",\n",
|
| 706 |
+
" \"Promotional uplift is consistently strong when we feature this.\",\n",
|
| 707 |
+
" \"Low shrinkage and high margins make this our star performer.\",\n",
|
| 708 |
+
" \"Vendor relationship is excellent β orders arrive on time.\",\n",
|
| 709 |
+
" \"Shelf velocity is outstanding β we rarely see excess stock.\",\n",
|
| 710 |
+
" \"A go-to category for cross-sell opportunities.\",\n",
|
| 711 |
+
" \"Customers actively seek this sub-category out.\",\n",
|
| 712 |
+
" \"Strong online and in-store performance in tandem.\",\n",
|
| 713 |
+
" \"This range punches above its weight in every metric.\",\n",
|
| 714 |
+
" \"Customer lifetime value is highest in this category.\",\n",
|
| 715 |
+
" \"Our data team flagged this as a priority to protect and grow.\",\n",
|
| 716 |
+
" \"Consistently the top contributor to monthly profit targets.\",\n",
|
| 717 |
+
" ],\n",
|
| 718 |
+
" \"neutral\": [\n",
|
| 719 |
+
" \"Decent products but nothing that really stands out.\",\n",
|
| 720 |
+
" \"Sells steadily but rarely generates excitement on the floor.\",\n",
|
| 721 |
+
" \"A serviceable category β not driving growth, not dragging it down.\",\n",
|
| 722 |
+
" \"Average performance across the board; room for improvement.\",\n",
|
| 723 |
+
" \"Customers buy when they need to, but won't seek it out.\",\n",
|
| 724 |
+
" \"Margins are acceptable but could be healthier.\",\n",
|
| 725 |
+
" \"We hold stock at standard levels β no need to over-invest.\",\n",
|
| 726 |
+
" \"Reliable but uninspiring category for us.\",\n",
|
| 727 |
+
" \"Turnover is consistent; just not a headline performer.\",\n",
|
| 728 |
+
" \"Some lines do well, others just sit there.\",\n",
|
| 729 |
+
" \"Not a category that drives footfall but serves a purpose.\",\n",
|
| 730 |
+
" \"Could benefit from a range refresh to boost interest.\",\n",
|
| 731 |
+
" \"Sales are predictable but plateau-ed for two quarters.\",\n",
|
| 732 |
+
" \"Customers rate it fine β three stars is the norm.\",\n",
|
| 733 |
+
" \"We reorder on schedule but rarely see spikes in demand.\",\n",
|
| 734 |
+
" \"A middle-of-the-range performer in our assortment.\",\n",
|
| 735 |
+
" \"Acceptable quality with a price point that matches.\",\n",
|
| 736 |
+
" \"Would benefit from promotional support to lift velocity.\",\n",
|
| 737 |
+
" \"Returns are low but so is enthusiasm.\",\n",
|
| 738 |
+
" \"Not losing money here, but not winning either.\",\n",
|
| 739 |
+
" \"Lacks the wow factor that drives impulse purchases.\",\n",
|
| 740 |
+
" \"A category we maintain rather than invest in.\",\n",
|
| 741 |
+
" \"Steady demand with flat growth trajectory.\",\n",
|
| 742 |
+
" \"Fine as a filler category but not a focus area.\",\n",
|
| 743 |
+
" \"Moderate satisfaction scores β room to grow.\",\n",
|
| 744 |
+
" \"No complaints, but no praise either.\",\n",
|
| 745 |
+
" \"Neither the best nor worst in our assortment.\",\n",
|
| 746 |
+
" \"Metrics are in range β just not remarkable.\",\n",
|
| 747 |
+
" \"Could be optimised further to push margins up.\",\n",
|
| 748 |
+
" \"It does its job without causing problems.\",\n",
|
| 749 |
+
" \"We've seen better quarters and worse quarters from this range.\",\n",
|
| 750 |
+
" \"Shelf space allocation seems appropriate for the returns.\",\n",
|
| 751 |
+
" \"No urgent action needed but worth monitoring.\",\n",
|
| 752 |
+
" \"Customer feedback is uneventful β nothing to act on urgently.\",\n",
|
| 753 |
+
" \"Average sell-through rate with seasonal fluctuation.\",\n",
|
| 754 |
+
" \"An important category but not a strategic priority right now.\",\n",
|
| 755 |
+
" \"Returns are in line with expectations β nothing alarming.\",\n",
|
| 756 |
+
" \"Standard performance for a mature product category.\",\n",
|
| 757 |
+
" \"Moderate margin contribution across the range.\",\n",
|
| 758 |
+
" \"Sales are ticking along β no cause for alarm or celebration.\",\n",
|
| 759 |
+
" \"Inventory sits around target levels most weeks.\",\n",
|
| 760 |
+
" \"We manage this category on autopilot.\",\n",
|
| 761 |
+
" \"Neither growing the basket nor shrinking it.\",\n",
|
| 762 |
+
" \"Prices are competitive but not differentiated.\",\n",
|
| 763 |
+
" \"A solid but unremarkable part of our assortment.\",\n",
|
| 764 |
+
" \"Customer interest is stable, not building.\",\n",
|
| 765 |
+
" \"We'll keep it in range until the data tells us otherwise.\",\n",
|
| 766 |
+
" \"Balanced between fast movers and slow movers.\",\n",
|
| 767 |
+
" \"A 'wait and see' category for next quarter.\",\n",
|
| 768 |
+
" \"Performs as expected given market conditions.\",\n",
|
| 769 |
+
" ],\n",
|
| 770 |
+
" \"negative\": [\n",
|
| 771 |
+
" \"Consistently underperforming β needs urgent review.\",\n",
|
| 772 |
+
" \"Margins are being squeezed and sales volume isn't compensating.\",\n",
|
| 773 |
+
" \"We're carrying too much stock in a category customers avoid.\",\n",
|
| 774 |
+
" \"High return rate is eating into what little margin we have.\",\n",
|
| 775 |
+
" \"Customer feedback is predominantly negative for this range.\",\n",
|
| 776 |
+
" \"This sub-category drags down our overall category scorecard.\",\n",
|
| 777 |
+
" \"Markdown frequency is too high β a sign of poor demand.\",\n",
|
| 778 |
+
" \"Demand has been declining for three consecutive months.\",\n",
|
| 779 |
+
" \"Slow sell-through is causing costly clearance cycles.\",\n",
|
| 780 |
+
" \"The vendor quality issues are now showing up in reviews.\",\n",
|
| 781 |
+
" \"We're losing customers to competitors in this category.\",\n",
|
| 782 |
+
" \"Profitability is negative on several key lines.\",\n",
|
| 783 |
+
" \"Poor basket attachment β rarely part of a multi-item purchase.\",\n",
|
| 784 |
+
" \"Staff are reluctant to recommend this range to customers.\",\n",
|
| 785 |
+
" \"Shrinkage and damages are above average in this sub-category.\",\n",
|
| 786 |
+
" \"Supply chain delays have damaged customer trust in this range.\",\n",
|
| 787 |
+
" \"We're overstocked with no clear path to clear inventory.\",\n",
|
| 788 |
+
" \"Worst NPS scores in the store come from this category.\",\n",
|
| 789 |
+
" \"Customers complain about value for money consistently.\",\n",
|
| 790 |
+
" \"A legacy category that no longer meets modern customer needs.\",\n",
|
| 791 |
+
" \"The data makes it clear: this sub-category needs rationalising.\",\n",
|
| 792 |
+
" \"Low traffic and poor conversion make this a liability.\",\n",
|
| 793 |
+
" \"Promotional support hasn't moved the needle at all.\",\n",
|
| 794 |
+
" \"Price reductions have failed to stimulate demand.\",\n",
|
| 795 |
+
" \"A drain on resources β time to consider ranging out.\",\n",
|
| 796 |
+
" \"Multiple customers have cited quality issues in recent weeks.\",\n",
|
| 797 |
+
" \"Inventory obsolescence risk is high in this sub-category.\",\n",
|
| 798 |
+
" \"We need a full range review to fix this underperformance.\",\n",
|
| 799 |
+
" \"Contribution margin is insufficient to justify space allocation.\",\n",
|
| 800 |
+
" \"Consistent poor performance across all three KPIs: sales, margin, units.\",\n",
|
| 801 |
+
" \"The category has no clear differentiation in our assortment.\",\n",
|
| 802 |
+
" \"Weak competitive positioning with no obvious fix.\",\n",
|
| 803 |
+
" \"Customer returns have doubled in the past quarter.\",\n",
|
| 804 |
+
" \"Our worst performing lines sit in this sub-category.\",\n",
|
| 805 |
+
" \"Brand perception damage is spilling over from this range.\",\n",
|
| 806 |
+
" \"Order quantities have been cut as confidence has dropped.\",\n",
|
| 807 |
+
" \"We've tried relaunching this β it hasn't worked.\",\n",
|
| 808 |
+
" \"No seasonal uplift to speak of β flat and declining.\",\n",
|
| 809 |
+
" \"Even discounting hasn't driven meaningful volume.\",\n",
|
| 810 |
+
" \"The opportunity cost of this shelf space is significant.\",\n",
|
| 811 |
+
" \"Vendor reliability has been poor β impacting availability.\",\n",
|
| 812 |
+
" \"Category performance is a red flag in every monthly report.\",\n",
|
| 813 |
+
" \"Stock provision risk is increasing as demand erodes.\",\n",
|
| 814 |
+
" \"A persistent weak spot that needs a decisive decision.\",\n",
|
| 815 |
+
" \"Write-downs in this category are becoming a regular occurrence.\",\n",
|
| 816 |
+
" \"Customer satisfaction surveys single this out for criticism.\",\n",
|
| 817 |
+
" \"No investment case can be made for this sub-category currently.\",\n",
|
| 818 |
+
" \"Foot traffic drops off near this section of the store.\",\n",
|
| 819 |
+
" \"Management escalations about this category are increasing.\",\n",
|
| 820 |
+
" \"The exit scenario is being actively evaluated.\",\n",
|
| 821 |
+
" ],\n",
|
| 822 |
+
"}"
|
| 823 |
+
]
|
| 824 |
+
},
|
| 825 |
+
{
|
| 826 |
+
"cell_type": "markdown",
|
| 827 |
+
"metadata": {
|
| 828 |
+
"id": "fQhfVaDmuULT"
|
| 829 |
+
},
|
| 830 |
+
"source": [
|
| 831 |
+
"### *b. Generate 10 reviews per sub-category using random sampling from the corresponding 50*"
|
| 832 |
+
]
|
| 833 |
+
},
|
| 834 |
+
{
|
| 835 |
+
"cell_type": "code",
|
| 836 |
+
"execution_count": null,
|
| 837 |
+
"metadata": {
|
| 838 |
+
"id": "l2SRc3PjuTGM"
|
| 839 |
+
},
|
| 840 |
+
"outputs": [],
|
| 841 |
+
"source": [
|
| 842 |
+
"review_rows = []\n",
|
| 843 |
+
"for _, row in df_products.iterrows():\n",
|
| 844 |
+
" sub_category = row['sub_category']\n",
|
| 845 |
+
" sentiment_label = row['sentiment_label']\n",
|
| 846 |
+
" review_pool = synthetic_reviews_by_sentiment[sentiment_label]\n",
|
| 847 |
+
" sampled_reviews = random.sample(review_pool, 10)\n",
|
| 848 |
+
" for review_text in sampled_reviews:\n",
|
| 849 |
+
" review_rows.append({\n",
|
| 850 |
+
" \"sub_category\": sub_category,\n",
|
| 851 |
+
" \"sentiment_label\": sentiment_label,\n",
|
| 852 |
+
" \"review_text\": review_text,\n",
|
| 853 |
+
" \"avg_profit\": row['avg_profit'],\n",
|
| 854 |
+
" \"popularity_score\": row['popularity_score']\n",
|
| 855 |
+
" })"
|
| 856 |
+
]
|
| 857 |
+
},
|
| 858 |
+
{
|
| 859 |
+
"cell_type": "markdown",
|
| 860 |
+
"metadata": {
|
| 861 |
+
"id": "bmJMXF-Bukdm"
|
| 862 |
+
},
|
| 863 |
+
"source": [
|
| 864 |
+
"### *c. Create the final dataframe df_reviews & save it as synthetic_superstore_reviews.csv*"
|
| 865 |
+
]
|
| 866 |
+
},
|
| 867 |
+
{
|
| 868 |
+
"cell_type": "code",
|
| 869 |
+
"execution_count": null,
|
| 870 |
+
"metadata": {
|
| 871 |
+
"id": "ZUKUqZsuumsp"
|
| 872 |
+
},
|
| 873 |
+
"outputs": [],
|
| 874 |
+
"source": [
|
| 875 |
+
"df_reviews = pd.DataFrame(review_rows)\n",
|
| 876 |
+
"df_reviews.to_csv(\"synthetic_superstore_reviews.csv\", index=False)"
|
| 877 |
+
]
|
| 878 |
+
},
|
| 879 |
+
{
|
| 880 |
+
"cell_type": "code",
|
| 881 |
+
"execution_count": null,
|
| 882 |
+
"metadata": {
|
| 883 |
+
"colab": {
|
| 884 |
+
"base_uri": "https://localhost:8080/"
|
| 885 |
+
},
|
| 886 |
+
"id": "3946e521",
|
| 887 |
+
"outputId": "bdb1ca17-6b82-46b9-e790-cd5b20312507"
|
| 888 |
+
},
|
| 889 |
+
"outputs": [
|
| 890 |
+
{
|
| 891 |
+
"output_type": "stream",
|
| 892 |
+
"name": "stdout",
|
| 893 |
+
"text": [
|
| 894 |
+
"β
Wrote synthetic_title_level_features.csv\n",
|
| 895 |
+
"β
Wrote synthetic_monthly_revenue_series.csv\n"
|
| 896 |
+
]
|
| 897 |
+
}
|
| 898 |
+
],
|
| 899 |
+
"source": [
|
| 900 |
+
"\n",
|
| 901 |
+
"# ============================================================\n",
|
| 902 |
+
"# β
Create \"R-ready\" derived inputs (root-level files)\n",
|
| 903 |
+
"# ============================================================\n",
|
| 904 |
+
"# These two files make the R notebook robust and fast:\n",
|
| 905 |
+
"# 1) synthetic_title_level_features.csv -> regression-ready, one row per sub-category\n",
|
| 906 |
+
"# 2) synthetic_monthly_revenue_series.csv -> forecasting-ready, one row per month\n",
|
| 907 |
+
"\n",
|
| 908 |
+
"import numpy as np\n",
|
| 909 |
+
"\n",
|
| 910 |
+
"def _safe_num(s):\n",
|
| 911 |
+
" return pd.to_numeric(\n",
|
| 912 |
+
" pd.Series(s).astype(str).str.replace(r\"[^0-9.]\", \"\", regex=True),\n",
|
| 913 |
+
" errors=\"coerce\"\n",
|
| 914 |
+
" )\n",
|
| 915 |
+
"\n",
|
| 916 |
+
"# --- Clean product metadata (avg_price/avg_profit) ---\n",
|
| 917 |
+
"df_books_r = df_products.copy()\n",
|
| 918 |
+
"if \"avg_price\" in df_books_r.columns:\n",
|
| 919 |
+
" df_books_r[\"avg_price\"] = _safe_num(df_books_r[\"avg_price\"])\n",
|
| 920 |
+
"if \"avg_profit\" in df_books_r.columns:\n",
|
| 921 |
+
" df_books_r[\"avg_profit\"] = _safe_num(df_books_r[\"avg_profit\"])\n",
|
| 922 |
+
"\n",
|
| 923 |
+
"df_books_r[\"sub_category\"] = df_books_r[\"sub_category\"].astype(str).str.strip()\n",
|
| 924 |
+
"\n",
|
| 925 |
+
"# --- Clean sales ---\n",
|
| 926 |
+
"df_sales_r = df_sales.copy()\n",
|
| 927 |
+
"df_sales_r[\"sub_category\"] = df_sales_r[\"sub_category\"].astype(str).str.strip()\n",
|
| 928 |
+
"df_sales_r[\"month\"] = pd.to_datetime(df_sales_r[\"month\"], errors=\"coerce\")\n",
|
| 929 |
+
"df_sales_r[\"units_sold\"] = _safe_num(df_sales_r[\"units_sold\"])\n",
|
| 930 |
+
"\n",
|
| 931 |
+
"# --- Clean reviews ---\n",
|
| 932 |
+
"df_reviews_r = df_reviews.copy()\n",
|
| 933 |
+
"df_reviews_r[\"sub_category\"] = df_reviews_r[\"sub_category\"].astype(str).str.strip()\n",
|
| 934 |
+
"df_reviews_r[\"sentiment_label\"] = df_reviews_r[\"sentiment_label\"].astype(str).str.lower().str.strip()\n",
|
| 935 |
+
"if \"avg_profit\" in df_reviews_r.columns:\n",
|
| 936 |
+
" df_reviews_r[\"avg_profit\"] = _safe_num(df_reviews_r[\"avg_profit\"])\n",
|
| 937 |
+
"if \"popularity_score\" in df_reviews_r.columns:\n",
|
| 938 |
+
" df_reviews_r[\"popularity_score\"] = _safe_num(df_reviews_r[\"popularity_score\"])\n",
|
| 939 |
+
"\n",
|
| 940 |
+
"# --- Sentiment shares per sub-category (from reviews) ---\n",
|
| 941 |
+
"sent_counts = (\n",
|
| 942 |
+
" df_reviews_r.groupby([\"sub_category\", \"sentiment_label\"])\n",
|
| 943 |
+
" .size()\n",
|
| 944 |
+
" .unstack(fill_value=0)\n",
|
| 945 |
+
")\n",
|
| 946 |
+
"for lab in [\"positive\", \"neutral\", \"negative\"]:\n",
|
| 947 |
+
" if lab not in sent_counts.columns:\n",
|
| 948 |
+
" sent_counts[lab] = 0\n",
|
| 949 |
+
"\n",
|
| 950 |
+
"sent_counts[\"total_reviews\"] = sent_counts[[\"positive\", \"neutral\", \"negative\"]].sum(axis=1)\n",
|
| 951 |
+
"den = sent_counts[\"total_reviews\"].replace(0, np.nan)\n",
|
| 952 |
+
"sent_counts[\"share_positive\"] = sent_counts[\"positive\"] / den\n",
|
| 953 |
+
"sent_counts[\"share_neutral\"] = sent_counts[\"neutral\"] / den\n",
|
| 954 |
+
"sent_counts[\"share_negative\"] = sent_counts[\"negative\"] / den\n",
|
| 955 |
+
"sent_counts = sent_counts.reset_index()\n",
|
| 956 |
+
"\n",
|
| 957 |
+
"# --- Sales aggregation per sub-category ---\n",
|
| 958 |
+
"sales_by_title = (\n",
|
| 959 |
+
" df_sales_r.dropna(subset=[\"sub_category\"])\n",
|
| 960 |
+
" .groupby(\"sub_category\", as_index=False)\n",
|
| 961 |
+
" .agg(\n",
|
| 962 |
+
" months_observed=(\"month\", \"nunique\"),\n",
|
| 963 |
+
" avg_units_sold=(\"units_sold\", \"mean\"),\n",
|
| 964 |
+
" total_units_sold=(\"units_sold\", \"sum\"),\n",
|
| 965 |
+
" )\n",
|
| 966 |
+
")\n",
|
| 967 |
+
"\n",
|
| 968 |
+
"# --- Sub-category-level features (join sales + products + sentiment) ---\n",
|
| 969 |
+
"df_title = (\n",
|
| 970 |
+
" sales_by_title\n",
|
| 971 |
+
" .merge(df_books_r[[\"sub_category\", \"avg_price\", \"avg_profit\"]], on=\"sub_category\", how=\"left\")\n",
|
| 972 |
+
" .merge(sent_counts[[\"sub_category\", \"share_positive\", \"share_neutral\", \"share_negative\", \"total_reviews\"]],\n",
|
| 973 |
+
" on=\"sub_category\", how=\"left\")\n",
|
| 974 |
+
")\n",
|
| 975 |
+
"\n",
|
| 976 |
+
"df_title[\"avg_revenue\"] = df_title[\"avg_units_sold\"] * df_title[\"avg_price\"]\n",
|
| 977 |
+
"df_title[\"total_revenue\"] = df_title[\"total_units_sold\"] * df_title[\"avg_price\"]\n",
|
| 978 |
+
"\n",
|
| 979 |
+
"df_title.to_csv(\"synthetic_title_level_features.csv\", index=False)\n",
|
| 980 |
+
"print(\"β
Wrote synthetic_title_level_features.csv\")\n",
|
| 981 |
+
"\n",
|
| 982 |
+
"# --- Monthly revenue series (proxy: units_sold * avg_price) ---\n",
|
| 983 |
+
"monthly_rev = (\n",
|
| 984 |
+
" df_sales_r.merge(df_books_r[[\"sub_category\", \"avg_price\"]], on=\"sub_category\", how=\"left\")\n",
|
| 985 |
+
")\n",
|
| 986 |
+
"monthly_rev[\"revenue\"] = monthly_rev[\"units_sold\"] * monthly_rev[\"avg_price\"]\n",
|
| 987 |
+
"\n",
|
| 988 |
+
"df_monthly = (\n",
|
| 989 |
+
" monthly_rev.dropna(subset=[\"month\"])\n",
|
| 990 |
+
" .groupby(\"month\", as_index=False)[\"revenue\"]\n",
|
| 991 |
+
" .sum()\n",
|
| 992 |
+
" .rename(columns={\"revenue\": \"total_revenue\"})\n",
|
| 993 |
+
" .sort_values(\"month\")\n",
|
| 994 |
+
")\n",
|
| 995 |
+
"# if revenue is all NA (e.g., missing price), fallback to units_sold as a teaching proxy\n",
|
| 996 |
+
"if df_monthly[\"total_revenue\"].notna().sum() == 0:\n",
|
| 997 |
+
" df_monthly = (\n",
|
| 998 |
+
" df_sales_r.dropna(subset=[\"month\"])\n",
|
| 999 |
+
" .groupby(\"month\", as_index=False)[\"units_sold\"]\n",
|
| 1000 |
+
" .sum()\n",
|
| 1001 |
+
" .rename(columns={\"units_sold\": \"total_revenue\"})\n",
|
| 1002 |
+
" .sort_values(\"month\")\n",
|
| 1003 |
+
" )\n",
|
| 1004 |
+
"\n",
|
| 1005 |
+
"df_monthly[\"month\"] = pd.to_datetime(df_monthly[\"month\"], errors=\"coerce\").dt.strftime(\"%Y-%m-%d\")\n",
|
| 1006 |
+
"df_monthly.to_csv(\"synthetic_monthly_revenue_series.csv\", index=False)\n",
|
| 1007 |
+
"print(\"β
Wrote synthetic_monthly_revenue_series.csv\")\n"
|
| 1008 |
+
]
|
| 1009 |
+
},
|
| 1010 |
+
{
|
| 1011 |
+
"cell_type": "markdown",
|
| 1012 |
+
"metadata": {
|
| 1013 |
+
"id": "RYvGyVfXuo54"
|
| 1014 |
+
},
|
| 1015 |
+
"source": [
|
| 1016 |
+
"### *d. βπ»πβοΈ View the first few lines*"
|
| 1017 |
+
]
|
| 1018 |
+
},
|
| 1019 |
+
{
|
| 1020 |
+
"cell_type": "code",
|
| 1021 |
+
"execution_count": null,
|
| 1022 |
+
"metadata": {
|
| 1023 |
+
"colab": {
|
| 1024 |
+
"base_uri": "https://localhost:8080/"
|
| 1025 |
+
},
|
| 1026 |
+
"id": "xfE8NMqOurKo",
|
| 1027 |
+
"outputId": "0155e713-b16c-43e0-bc90-46183e48ce67"
|
| 1028 |
+
},
|
| 1029 |
+
"outputs": [
|
| 1030 |
+
{
|
| 1031 |
+
"output_type": "stream",
|
| 1032 |
+
"name": "stdout",
|
| 1033 |
+
"text": [
|
| 1034 |
+
" sub_category sentiment_label \\\n",
|
| 1035 |
+
"0 Accessories positive \n",
|
| 1036 |
+
"1 Accessories positive \n",
|
| 1037 |
+
"2 Accessories positive \n",
|
| 1038 |
+
"3 Accessories positive \n",
|
| 1039 |
+
"4 Accessories positive \n",
|
| 1040 |
+
"\n",
|
| 1041 |
+
" review_text avg_profit \\\n",
|
| 1042 |
+
"0 Category leadership is clearly visible in the ... 55.81 \n",
|
| 1043 |
+
"1 A strategic priority that keeps delivering ret... 55.81 \n",
|
| 1044 |
+
"2 Year-on-year growth in this sub-category conti... 55.81 \n",
|
| 1045 |
+
"3 Customers actively seek this sub-category out. 55.81 \n",
|
| 1046 |
+
"4 Outstanding selection that meets every require... 55.81 \n",
|
| 1047 |
+
"\n",
|
| 1048 |
+
" popularity_score \n",
|
| 1049 |
+
"0 4 \n",
|
| 1050 |
+
"1 4 \n",
|
| 1051 |
+
"2 4 \n",
|
| 1052 |
+
"3 4 \n",
|
| 1053 |
+
"4 4 \n"
|
| 1054 |
+
]
|
| 1055 |
+
}
|
| 1056 |
+
],
|
| 1057 |
+
"source": [
|
| 1058 |
+
"print(df_reviews.head())"
|
| 1059 |
+
]
|
| 1060 |
+
}
|
| 1061 |
+
],
|
| 1062 |
+
"metadata": {
|
| 1063 |
+
"colab": {
|
| 1064 |
+
"provenance": []
|
| 1065 |
+
},
|
| 1066 |
+
"kernelspec": {
|
| 1067 |
+
"display_name": "Python 3",
|
| 1068 |
+
"name": "python3"
|
| 1069 |
+
},
|
| 1070 |
+
"language_info": {
|
| 1071 |
+
"name": "python"
|
| 1072 |
+
}
|
| 1073 |
+
},
|
| 1074 |
+
"nbformat": 4,
|
| 1075 |
+
"nbformat_minor": 0
|
| 1076 |
+
}
|