rosejubin commited on
Commit
4922209
·
verified ·
1 Parent(s): 790b9bd

Upload 2 files

Browse files
Files changed (1) hide show
  1. datacreation.ipynb +1160 -0
datacreation.ipynb ADDED
@@ -0,0 +1,1160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": 1,
24
+ "metadata": {
25
+ "colab": {
26
+ "base_uri": "https://localhost:8080/"
27
+ },
28
+ "id": "f48c8f8c",
29
+ "outputId": "6f1cf1e6-ceaf-40ea-a311-540c348e8487"
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.** ⛏ Web-scrape all book titles, prices, and ratings from books.toscrape.com"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "markdown",
78
+ "metadata": {
79
+ "id": "0IWuNpxxYDJF"
80
+ },
81
+ "source": [
82
+ "### *a. Initial setup*\n",
83
+ "Define the base url of the website you will scrape as well as how and what you will scrape"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": 2,
89
+ "metadata": {
90
+ "id": "91d52125"
91
+ },
92
+ "outputs": [],
93
+ "source": [
94
+ "import requests\n",
95
+ "from bs4 import BeautifulSoup\n",
96
+ "import pandas as pd\n",
97
+ "import time\n",
98
+ "\n",
99
+ "base_url = \"https://books.toscrape.com/catalogue/page-{}.html\"\n",
100
+ "headers = {\"User-Agent\": \"Mozilla/5.0\"}\n",
101
+ "\n",
102
+ "titles, prices, ratings = [], [], []"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "markdown",
107
+ "metadata": {
108
+ "id": "oCdTsin2Yfp3"
109
+ },
110
+ "source": [
111
+ "### *b. Fill titles, prices, and ratings from the web pages*"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": 3,
117
+ "metadata": {
118
+ "id": "xqO5Y3dnYhxt"
119
+ },
120
+ "outputs": [],
121
+ "source": [
122
+ "# Loop through all 50 pages\n",
123
+ "for page in range(1, 51):\n",
124
+ " url = base_url.format(page)\n",
125
+ " response = requests.get(url, headers=headers)\n",
126
+ " soup = BeautifulSoup(response.content, \"html.parser\")\n",
127
+ " books = soup.find_all(\"article\", class_=\"product_pod\")\n",
128
+ "\n",
129
+ " for book in books:\n",
130
+ " titles.append(book.h3.a[\"title\"])\n",
131
+ " prices.append(float(book.find(\"p\", class_=\"price_color\").text[1:]))\n",
132
+ " ratings.append(book.p.get(\"class\")[1])\n",
133
+ "\n",
134
+ " time.sleep(0.5) # polite scraping delay"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "markdown",
139
+ "metadata": {
140
+ "id": "T0TOeRC4Yrnn"
141
+ },
142
+ "source": [
143
+ "### *c. ✋🏻🛑⛔️ Create a dataframe df_books that contains the now complete \"title\", \"price\", and \"rating\" objects*"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": 3,
149
+ "metadata": {
150
+ "id": "l5FkkNhUYTHh"
151
+ },
152
+ "outputs": [],
153
+ "source": []
154
+ },
155
+ {
156
+ "cell_type": "markdown",
157
+ "metadata": {
158
+ "id": "duI5dv3CZYvF"
159
+ },
160
+ "source": [
161
+ "### *d. Save web-scraped dataframe either as a CSV or Excel file*"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "code",
166
+ "execution_count": 4,
167
+ "metadata": {
168
+ "id": "lC1U_YHtZifh"
169
+ },
170
+ "outputs": [],
171
+ "source": [
172
+ "df_books = pd.DataFrame({\"title\": titles, \"price\": prices, \"rating\": ratings})\n",
173
+ "\n",
174
+ "# 💾 Save to CSV\n",
175
+ "df_books.to_csv(\"books_data.csv\", index=False)\n",
176
+ "\n",
177
+ "# 💾 Or save to Excel\n",
178
+ "# df_books.to_excel(\"books_data.xlsx\", index=False)"
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "markdown",
183
+ "metadata": {
184
+ "id": "qMjRKMBQZlJi"
185
+ },
186
+ "source": [
187
+ "### *e. ✋🏻🛑⛔️ View first fiew lines*"
188
+ ]
189
+ },
190
+ {
191
+ "cell_type": "code",
192
+ "execution_count": 5,
193
+ "metadata": {
194
+ "colab": {
195
+ "base_uri": "https://localhost:8080/",
196
+ "height": 206
197
+ },
198
+ "id": "O_wIvTxYZqCK",
199
+ "outputId": "bc746ebc-566c-4d2a-ac66-645dc0dbeedd"
200
+ },
201
+ "outputs": [
202
+ {
203
+ "output_type": "execute_result",
204
+ "data": {
205
+ "text/plain": [
206
+ " title price rating\n",
207
+ "0 A Light in the Attic 51.77 Three\n",
208
+ "1 Tipping the Velvet 53.74 One\n",
209
+ "2 Soumission 50.10 One\n",
210
+ "3 Sharp Objects 47.82 Four\n",
211
+ "4 Sapiens: A Brief History of Humankind 54.23 Five"
212
+ ],
213
+ "text/html": [
214
+ "\n",
215
+ " <div id=\"df-5493d9de-260a-4af5-baa9-ae858976e928\" class=\"colab-df-container\">\n",
216
+ " <div>\n",
217
+ "<style scoped>\n",
218
+ " .dataframe tbody tr th:only-of-type {\n",
219
+ " vertical-align: middle;\n",
220
+ " }\n",
221
+ "\n",
222
+ " .dataframe tbody tr th {\n",
223
+ " vertical-align: top;\n",
224
+ " }\n",
225
+ "\n",
226
+ " .dataframe thead th {\n",
227
+ " text-align: right;\n",
228
+ " }\n",
229
+ "</style>\n",
230
+ "<table border=\"1\" class=\"dataframe\">\n",
231
+ " <thead>\n",
232
+ " <tr style=\"text-align: right;\">\n",
233
+ " <th></th>\n",
234
+ " <th>title</th>\n",
235
+ " <th>price</th>\n",
236
+ " <th>rating</th>\n",
237
+ " </tr>\n",
238
+ " </thead>\n",
239
+ " <tbody>\n",
240
+ " <tr>\n",
241
+ " <th>0</th>\n",
242
+ " <td>A Light in the Attic</td>\n",
243
+ " <td>51.77</td>\n",
244
+ " <td>Three</td>\n",
245
+ " </tr>\n",
246
+ " <tr>\n",
247
+ " <th>1</th>\n",
248
+ " <td>Tipping the Velvet</td>\n",
249
+ " <td>53.74</td>\n",
250
+ " <td>One</td>\n",
251
+ " </tr>\n",
252
+ " <tr>\n",
253
+ " <th>2</th>\n",
254
+ " <td>Soumission</td>\n",
255
+ " <td>50.10</td>\n",
256
+ " <td>One</td>\n",
257
+ " </tr>\n",
258
+ " <tr>\n",
259
+ " <th>3</th>\n",
260
+ " <td>Sharp Objects</td>\n",
261
+ " <td>47.82</td>\n",
262
+ " <td>Four</td>\n",
263
+ " </tr>\n",
264
+ " <tr>\n",
265
+ " <th>4</th>\n",
266
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
267
+ " <td>54.23</td>\n",
268
+ " <td>Five</td>\n",
269
+ " </tr>\n",
270
+ " </tbody>\n",
271
+ "</table>\n",
272
+ "</div>\n",
273
+ " <div class=\"colab-df-buttons\">\n",
274
+ "\n",
275
+ " <div class=\"colab-df-container\">\n",
276
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-5493d9de-260a-4af5-baa9-ae858976e928')\"\n",
277
+ " title=\"Convert this dataframe to an interactive table.\"\n",
278
+ " style=\"display:none;\">\n",
279
+ "\n",
280
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
281
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
282
+ " </svg>\n",
283
+ " </button>\n",
284
+ "\n",
285
+ " <style>\n",
286
+ " .colab-df-container {\n",
287
+ " display:flex;\n",
288
+ " gap: 12px;\n",
289
+ " }\n",
290
+ "\n",
291
+ " .colab-df-convert {\n",
292
+ " background-color: #E8F0FE;\n",
293
+ " border: none;\n",
294
+ " border-radius: 50%;\n",
295
+ " cursor: pointer;\n",
296
+ " display: none;\n",
297
+ " fill: #1967D2;\n",
298
+ " height: 32px;\n",
299
+ " padding: 0 0 0 0;\n",
300
+ " width: 32px;\n",
301
+ " }\n",
302
+ "\n",
303
+ " .colab-df-convert:hover {\n",
304
+ " background-color: #E2EBFA;\n",
305
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
306
+ " fill: #174EA6;\n",
307
+ " }\n",
308
+ "\n",
309
+ " .colab-df-buttons div {\n",
310
+ " margin-bottom: 4px;\n",
311
+ " }\n",
312
+ "\n",
313
+ " [theme=dark] .colab-df-convert {\n",
314
+ " background-color: #3B4455;\n",
315
+ " fill: #D2E3FC;\n",
316
+ " }\n",
317
+ "\n",
318
+ " [theme=dark] .colab-df-convert:hover {\n",
319
+ " background-color: #434B5C;\n",
320
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
321
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
322
+ " fill: #FFFFFF;\n",
323
+ " }\n",
324
+ " </style>\n",
325
+ "\n",
326
+ " <script>\n",
327
+ " const buttonEl =\n",
328
+ " document.querySelector('#df-5493d9de-260a-4af5-baa9-ae858976e928 button.colab-df-convert');\n",
329
+ " buttonEl.style.display =\n",
330
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
331
+ "\n",
332
+ " async function convertToInteractive(key) {\n",
333
+ " const element = document.querySelector('#df-5493d9de-260a-4af5-baa9-ae858976e928');\n",
334
+ " const dataTable =\n",
335
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
336
+ " [key], {});\n",
337
+ " if (!dataTable) return;\n",
338
+ "\n",
339
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
340
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
341
+ " + ' to learn more about interactive tables.';\n",
342
+ " element.innerHTML = '';\n",
343
+ " dataTable['output_type'] = 'display_data';\n",
344
+ " await google.colab.output.renderOutput(dataTable, element);\n",
345
+ " const docLink = document.createElement('div');\n",
346
+ " docLink.innerHTML = docLinkHtml;\n",
347
+ " element.appendChild(docLink);\n",
348
+ " }\n",
349
+ " </script>\n",
350
+ " </div>\n",
351
+ "\n",
352
+ "\n",
353
+ " </div>\n",
354
+ " </div>\n"
355
+ ],
356
+ "application/vnd.google.colaboratory.intrinsic+json": {
357
+ "type": "dataframe",
358
+ "variable_name": "df_books",
359
+ "summary": "{\n \"name\": \"df_books\",\n \"rows\": 1000,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 999,\n \"samples\": [\n \"The Grownup\",\n \"Persepolis: The Story of a Childhood (Persepolis #1-2)\",\n \"Ayumi's Violin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"price\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.446689669952772,\n \"min\": 10.0,\n \"max\": 59.99,\n \"num_unique_values\": 903,\n \"samples\": [\n 19.73,\n 55.65,\n 46.31\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"One\",\n \"Two\",\n \"Four\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
360
+ }
361
+ },
362
+ "metadata": {},
363
+ "execution_count": 5
364
+ }
365
+ ],
366
+ "source": [
367
+ "df_books.head()"
368
+ ]
369
+ },
370
+ {
371
+ "cell_type": "markdown",
372
+ "metadata": {
373
+ "id": "p-1Pr2szaqLk"
374
+ },
375
+ "source": [
376
+ "## **3.** 🧩 Create a meaningful connection between real & synthetic datasets"
377
+ ]
378
+ },
379
+ {
380
+ "cell_type": "markdown",
381
+ "metadata": {
382
+ "id": "SIaJUGIpaH4V"
383
+ },
384
+ "source": [
385
+ "### *a. Initial setup*"
386
+ ]
387
+ },
388
+ {
389
+ "cell_type": "code",
390
+ "execution_count": 6,
391
+ "metadata": {
392
+ "id": "-gPXGcRPuV_9"
393
+ },
394
+ "outputs": [],
395
+ "source": [
396
+ "import numpy as np\n",
397
+ "import random\n",
398
+ "from datetime import datetime\n",
399
+ "import warnings\n",
400
+ "\n",
401
+ "warnings.filterwarnings(\"ignore\")\n",
402
+ "random.seed(2025)\n",
403
+ "np.random.seed(2025)"
404
+ ]
405
+ },
406
+ {
407
+ "cell_type": "markdown",
408
+ "metadata": {
409
+ "id": "pY4yCoIuaQqp"
410
+ },
411
+ "source": [
412
+ "### *b. Generate popularity scores based on rating (with some randomness) with a generate_popularity_score function*"
413
+ ]
414
+ },
415
+ {
416
+ "cell_type": "code",
417
+ "execution_count": 7,
418
+ "metadata": {
419
+ "id": "mnd5hdAbaNjz"
420
+ },
421
+ "outputs": [],
422
+ "source": [
423
+ "def generate_popularity_score(rating):\n",
424
+ " base = {\"One\": 2, \"Two\": 3, \"Three\": 3, \"Four\": 4, \"Five\": 4}.get(rating, 3)\n",
425
+ " trend_factor = random.choices([-1, 0, 1], weights=[1, 3, 2])[0]\n",
426
+ " return int(np.clip(base + trend_factor, 1, 5))"
427
+ ]
428
+ },
429
+ {
430
+ "cell_type": "markdown",
431
+ "metadata": {
432
+ "id": "n4-TaNTFgPak"
433
+ },
434
+ "source": [
435
+ "### *c. ✋🏻🛑⛔️ Run the function to create a \"popularity_score\" column from \"rating\"*"
436
+ ]
437
+ },
438
+ {
439
+ "cell_type": "code",
440
+ "execution_count": 8,
441
+ "metadata": {
442
+ "id": "V-G3OCUCgR07"
443
+ },
444
+ "outputs": [],
445
+ "source": [
446
+ "import numpy as np\n",
447
+ "import random\n",
448
+ "\n",
449
+ "def generate_popularity_score(rating):\n",
450
+ " base = {\"One\": 2, \"Two\": 3, \"Three\": 3, \"Four\": 4, \"Five\": 4}.get(rating, 3)\n",
451
+ " trend_factor = random.choices([-1, 0, 1], weights=[1, 3, 2])[0]\n",
452
+ " return int(np.clip(base + trend_factor, 1, 5))\n",
453
+ "\n",
454
+ "df_books[\"popularity_score\"] = df_books[\"rating\"].apply(generate_popularity_score)"
455
+ ]
456
+ },
457
+ {
458
+ "cell_type": "markdown",
459
+ "metadata": {
460
+ "id": "HnngRNTgacYt"
461
+ },
462
+ "source": [
463
+ "### *d. Decide on the sentiment_label based on the popularity score with a get_sentiment function*"
464
+ ]
465
+ },
466
+ {
467
+ "cell_type": "code",
468
+ "execution_count": 9,
469
+ "metadata": {
470
+ "id": "kUtWmr8maZLZ"
471
+ },
472
+ "outputs": [],
473
+ "source": [
474
+ "def get_sentiment(popularity_score):\n",
475
+ " if popularity_score <= 2:\n",
476
+ " return \"negative\"\n",
477
+ " elif popularity_score == 3:\n",
478
+ " return \"neutral\"\n",
479
+ " else:\n",
480
+ " return \"positive\""
481
+ ]
482
+ },
483
+ {
484
+ "cell_type": "markdown",
485
+ "metadata": {
486
+ "id": "HF9F9HIzgT7Z"
487
+ },
488
+ "source": [
489
+ "### *e. ✋🏻🛑⛔️ Run the function to create a \"sentiment_label\" column from \"popularity_score\"*"
490
+ ]
491
+ },
492
+ {
493
+ "cell_type": "code",
494
+ "execution_count": 10,
495
+ "metadata": {
496
+ "id": "tafQj8_7gYCG"
497
+ },
498
+ "outputs": [],
499
+ "source": [
500
+ "def get_sentiment(popularity_score):\n",
501
+ " if popularity_score <= 2:\n",
502
+ " return \"negative\"\n",
503
+ " elif popularity_score == 3:\n",
504
+ " return \"neutral\"\n",
505
+ " else:\n",
506
+ " return \"positive\"\n",
507
+ "\n",
508
+ "df_books[\"sentiment_label\"] = df_books[\"popularity_score\"].apply(get_sentiment)"
509
+ ]
510
+ },
511
+ {
512
+ "cell_type": "markdown",
513
+ "metadata": {
514
+ "id": "T8AdKkmASq9a"
515
+ },
516
+ "source": [
517
+ "## **4.** 📈 Generate synthetic book sales data of 18 months"
518
+ ]
519
+ },
520
+ {
521
+ "cell_type": "markdown",
522
+ "metadata": {
523
+ "id": "OhXbdGD5fH0c"
524
+ },
525
+ "source": [
526
+ "### *a. Create a generate_sales_profit function that would generate sales patterns based on sentiment_label (with some randomness)*"
527
+ ]
528
+ },
529
+ {
530
+ "cell_type": "code",
531
+ "execution_count": 11,
532
+ "metadata": {
533
+ "id": "qkVhYPXGbgEn"
534
+ },
535
+ "outputs": [],
536
+ "source": [
537
+ "def generate_sales_profile(sentiment):\n",
538
+ " months = pd.date_range(end=datetime.today(), periods=18, freq=\"M\")\n",
539
+ "\n",
540
+ " if sentiment == \"positive\":\n",
541
+ " base = random.randint(200, 300)\n",
542
+ " trend = np.linspace(base, base + random.randint(20, 60), len(months))\n",
543
+ " elif sentiment == \"negative\":\n",
544
+ " base = random.randint(20, 80)\n",
545
+ " trend = np.linspace(base, base - random.randint(10, 30), len(months))\n",
546
+ " else: # neutral\n",
547
+ " base = random.randint(80, 160)\n",
548
+ " trend = np.full(len(months), base + random.randint(-10, 10))\n",
549
+ "\n",
550
+ " seasonality = 10 * np.sin(np.linspace(0, 3 * np.pi, len(months)))\n",
551
+ " noise = np.random.normal(0, 5, len(months))\n",
552
+ " monthly_sales = np.clip(trend + seasonality + noise, a_min=0, a_max=None).astype(int)\n",
553
+ "\n",
554
+ " return list(zip(months.strftime(\"%Y-%m\"), monthly_sales))"
555
+ ]
556
+ },
557
+ {
558
+ "cell_type": "markdown",
559
+ "metadata": {
560
+ "id": "L2ak1HlcgoTe"
561
+ },
562
+ "source": [
563
+ "### *b. Run the function as part of building sales_data*"
564
+ ]
565
+ },
566
+ {
567
+ "cell_type": "code",
568
+ "execution_count": 12,
569
+ "metadata": {
570
+ "id": "SlJ24AUafoDB"
571
+ },
572
+ "outputs": [],
573
+ "source": [
574
+ "sales_data = []\n",
575
+ "for _, row in df_books.iterrows():\n",
576
+ " records = generate_sales_profile(row[\"sentiment_label\"])\n",
577
+ " for month, units in records:\n",
578
+ " sales_data.append({\n",
579
+ " \"title\": row[\"title\"],\n",
580
+ " \"month\": month,\n",
581
+ " \"units_sold\": units,\n",
582
+ " \"sentiment_label\": row[\"sentiment_label\"]\n",
583
+ " })"
584
+ ]
585
+ },
586
+ {
587
+ "cell_type": "markdown",
588
+ "metadata": {
589
+ "id": "4IXZKcCSgxnq"
590
+ },
591
+ "source": [
592
+ "### *c. ✋🏻🛑⛔️ Create a df_sales DataFrame from sales_data*"
593
+ ]
594
+ },
595
+ {
596
+ "cell_type": "code",
597
+ "execution_count": 13,
598
+ "metadata": {
599
+ "id": "wcN6gtiZg-ws"
600
+ },
601
+ "outputs": [],
602
+ "source": [
603
+ "import pandas as pd\n",
604
+ "from datetime import datetime\n",
605
+ "import numpy as np\n",
606
+ "import random\n",
607
+ "\n",
608
+ "def generate_sales_profile(sentiment):\n",
609
+ " # Changed 'M' to 'ME' to address FutureWarning\n",
610
+ " months = pd.date_range(end=datetime.today(), periods=18, freq=\"ME\")\n",
611
+ "\n",
612
+ " if sentiment == \"positive\":\n",
613
+ " base = random.randint(200, 300)\n",
614
+ " trend = np.linspace(base, base + random.randint(20, 60), len(months))\n",
615
+ " elif sentiment == \"negative\":\n",
616
+ " base = random.randint(20, 80)\n",
617
+ " trend = np.linspace(base, base - random.randint(10, 30), len(months))\n",
618
+ " else: # neutral\n",
619
+ " base = random.randint(80, 160)\n",
620
+ " trend = np.full(len(months), base + random.randint(-10, 10))\n",
621
+ "\n",
622
+ " seasonality = 10 * np.sin(np.linspace(0, 3 * np.pi, len(months)))\n",
623
+ " noise = np.random.normal(0, 5, len(months))\n",
624
+ " monthly_sales = np.clip(trend + seasonality + noise, a_min=0, a_max=None).astype(int)\n",
625
+ "\n",
626
+ " return list(zip(months.strftime(\"%Y-%m\"), monthly_sales))\n",
627
+ "\n",
628
+ "sales_data = []\n",
629
+ "for _, row in df_books.iterrows():\n",
630
+ " records = generate_sales_profile(row[\"sentiment_label\"])\n",
631
+ " for month, units in records:\n",
632
+ " sales_data.append({\n",
633
+ " \"title\": row[\"title\"],\n",
634
+ " \"month\": month,\n",
635
+ " \"units_sold\": units,\n",
636
+ " \"sentiment_label\": row[\"sentiment_label\"]\n",
637
+ " })\n",
638
+ "\n",
639
+ "df_sales = pd.DataFrame(sales_data)"
640
+ ]
641
+ },
642
+ {
643
+ "cell_type": "markdown",
644
+ "metadata": {
645
+ "id": "EhIjz9WohAmZ"
646
+ },
647
+ "source": [
648
+ "### *d. Save df_sales as synthetic_sales_data.csv & view first few lines*"
649
+ ]
650
+ },
651
+ {
652
+ "cell_type": "code",
653
+ "execution_count": 14,
654
+ "metadata": {
655
+ "colab": {
656
+ "base_uri": "https://localhost:8080/"
657
+ },
658
+ "id": "MzbZvLcAhGaH",
659
+ "outputId": "946661f7-909f-4725-9b28-1941e3720507"
660
+ },
661
+ "outputs": [
662
+ {
663
+ "output_type": "stream",
664
+ "name": "stdout",
665
+ "text": [
666
+ " title month units_sold sentiment_label\n",
667
+ "0 A Light in the Attic 2024-10 126 neutral\n",
668
+ "1 A Light in the Attic 2024-11 137 neutral\n",
669
+ "2 A Light in the Attic 2024-12 134 neutral\n",
670
+ "3 A Light in the Attic 2025-01 134 neutral\n",
671
+ "4 A Light in the Attic 2025-02 127 neutral\n"
672
+ ]
673
+ }
674
+ ],
675
+ "source": [
676
+ "df_sales.to_csv(\"synthetic_sales_data.csv\", index=False)\n",
677
+ "\n",
678
+ "print(df_sales.head())"
679
+ ]
680
+ },
681
+ {
682
+ "cell_type": "markdown",
683
+ "metadata": {
684
+ "id": "7g9gqBgQMtJn"
685
+ },
686
+ "source": [
687
+ "## **5.** 🎯 Generate synthetic customer reviews"
688
+ ]
689
+ },
690
+ {
691
+ "cell_type": "markdown",
692
+ "metadata": {
693
+ "id": "Gi4y9M9KuDWx"
694
+ },
695
+ "source": [
696
+ "### *a. ✋🏻🛑⛔️ Ask ChatGPT to create a list of 50 distinct generic book review texts for the sentiment labels \"positive\", \"neutral\", and \"negative\" called synthetic_reviews_by_sentiment*"
697
+ ]
698
+ },
699
+ {
700
+ "cell_type": "code",
701
+ "execution_count": 15,
702
+ "metadata": {
703
+ "id": "b3cd2a50"
704
+ },
705
+ "outputs": [],
706
+ "source": [
707
+ "synthetic_reviews_by_sentiment = {\n",
708
+ " \"positive\": [\n",
709
+ " \"A compelling and heartwarming read that stayed with me long after I finished.\",\n",
710
+ " \"Brilliantly written! The characters were unforgettable and the plot was engaging.\",\n",
711
+ " \"One of the best books I've read this year — inspiring and emotionally rich.\",\n",
712
+ " ],\n",
713
+ " \"neutral\": [\n",
714
+ " \"An average book — not great, but not bad either.\",\n",
715
+ " \"Some parts really stood out, others felt a bit flat.\",\n",
716
+ " \"It was okay overall. A decent way to pass the time.\",\n",
717
+ " ],\n",
718
+ " \"negative\": [\n",
719
+ " \"I struggled to get through this one — it just didn’t grab me.\",\n",
720
+ " \"The plot was confusing and the characters felt underdeveloped.\",\n",
721
+ " \"Disappointing. I had high hopes, but they weren't met.\",\n",
722
+ " ]\n",
723
+ "}"
724
+ ]
725
+ },
726
+ {
727
+ "cell_type": "markdown",
728
+ "metadata": {
729
+ "id": "fQhfVaDmuULT"
730
+ },
731
+ "source": [
732
+ "### *b. Generate 10 reviews per book using random sampling from the corresponding 50*"
733
+ ]
734
+ },
735
+ {
736
+ "cell_type": "code",
737
+ "execution_count": 16,
738
+ "metadata": {
739
+ "id": "l2SRc3PjuTGM"
740
+ },
741
+ "outputs": [],
742
+ "source": [
743
+ "review_rows = []\n",
744
+ "for _, row in df_books.iterrows():\n",
745
+ " title = row['title']\n",
746
+ " sentiment_label = row['sentiment_label']\n",
747
+ " review_pool = synthetic_reviews_by_sentiment[sentiment_label]\n",
748
+ " # Adjusting sample size to prevent ValueError, since review_pool currently has only 3 items\n",
749
+ " sampled_reviews = random.sample(review_pool, min(10, len(review_pool)))\n",
750
+ " for review_text in sampled_reviews:\n",
751
+ " review_rows.append({\n",
752
+ " \"title\": title,\n",
753
+ " \"sentiment_label\": sentiment_label,\n",
754
+ " \"review_text\": review_text,\n",
755
+ " \"rating\": row['rating'],\n",
756
+ " \"popularity_score\": row['popularity_score']\n",
757
+ " })"
758
+ ]
759
+ },
760
+ {
761
+ "cell_type": "markdown",
762
+ "metadata": {
763
+ "id": "bmJMXF-Bukdm"
764
+ },
765
+ "source": [
766
+ "### *c. Create the final dataframe df_reviews & save it as synthetic_book_reviews.csv*"
767
+ ]
768
+ },
769
+ {
770
+ "cell_type": "code",
771
+ "execution_count": 17,
772
+ "metadata": {
773
+ "id": "ZUKUqZsuumsp"
774
+ },
775
+ "outputs": [],
776
+ "source": [
777
+ "df_reviews = pd.DataFrame(review_rows)\n",
778
+ "df_reviews.to_csv(\"synthetic_book_reviews.csv\", index=False)"
779
+ ]
780
+ },
781
+ {
782
+ "cell_type": "markdown",
783
+ "source": [
784
+ "### *c. inputs for R*"
785
+ ],
786
+ "metadata": {
787
+ "id": "_602pYUS3gY5"
788
+ }
789
+ },
790
+ {
791
+ "cell_type": "code",
792
+ "execution_count": 18,
793
+ "metadata": {
794
+ "colab": {
795
+ "base_uri": "https://localhost:8080/"
796
+ },
797
+ "id": "3946e521",
798
+ "outputId": "f201dc0b-1c17-4531-e00f-bf126b146810"
799
+ },
800
+ "outputs": [
801
+ {
802
+ "output_type": "stream",
803
+ "name": "stdout",
804
+ "text": [
805
+ "✅ Wrote synthetic_title_level_features.csv\n",
806
+ "✅ Wrote synthetic_monthly_revenue_series.csv\n"
807
+ ]
808
+ }
809
+ ],
810
+ "source": [
811
+ "import numpy as np\n",
812
+ "\n",
813
+ "def _safe_num(s):\n",
814
+ " return pd.to_numeric(\n",
815
+ " pd.Series(s).astype(str).str.replace(r\"[^0-9.]\", \"\", regex=True),\n",
816
+ " errors=\"coerce\"\n",
817
+ " )\n",
818
+ "\n",
819
+ "# --- Clean book metadata (price/rating) ---\n",
820
+ "df_books_r = df_books.copy()\n",
821
+ "if \"price\" in df_books_r.columns:\n",
822
+ " df_books_r[\"price\"] = _safe_num(df_books_r[\"price\"])\n",
823
+ "if \"rating\" in df_books_r.columns:\n",
824
+ " df_books_r[\"rating\"] = _safe_num(df_books_r[\"rating\"])\n",
825
+ "\n",
826
+ "df_books_r[\"title\"] = df_books_r[\"title\"].astype(str).str.strip()\n",
827
+ "\n",
828
+ "# --- Clean sales ---\n",
829
+ "df_sales_r = df_sales.copy()\n",
830
+ "df_sales_r[\"title\"] = df_sales_r[\"title\"].astype(str).str.strip()\n",
831
+ "df_sales_r[\"month\"] = pd.to_datetime(df_sales_r[\"month\"], errors=\"coerce\")\n",
832
+ "df_sales_r[\"units_sold\"] = _safe_num(df_sales_r[\"units_sold\"])\n",
833
+ "\n",
834
+ "# --- Clean reviews ---\n",
835
+ "df_reviews_r = df_reviews.copy()\n",
836
+ "df_reviews_r[\"title\"] = df_reviews_r[\"title\"].astype(str).str.strip()\n",
837
+ "df_reviews_r[\"sentiment_label\"] = df_reviews_r[\"sentiment_label\"].astype(str).str.lower().str.strip()\n",
838
+ "if \"rating\" in df_reviews_r.columns:\n",
839
+ " df_reviews_r[\"rating\"] = _safe_num(df_reviews_r[\"rating\"])\n",
840
+ "if \"popularity_score\" in df_reviews_r.columns:\n",
841
+ " df_reviews_r[\"popularity_score\"] = _safe_num(df_reviews_r[\"popularity_score\"])\n",
842
+ "\n",
843
+ "# --- Sentiment shares per title (from reviews) ---\n",
844
+ "sent_counts = (\n",
845
+ " df_reviews_r.groupby([\"title\", \"sentiment_label\"])\n",
846
+ " .size()\n",
847
+ " .unstack(fill_value=0)\n",
848
+ ")\n",
849
+ "for lab in [\"positive\", \"neutral\", \"negative\"]:\n",
850
+ " if lab not in sent_counts.columns:\n",
851
+ " sent_counts[lab] = 0\n",
852
+ "\n",
853
+ "sent_counts[\"total_reviews\"] = sent_counts[[\"positive\", \"neutral\", \"negative\"]].sum(axis=1)\n",
854
+ "den = sent_counts[\"total_reviews\"].replace(0, np.nan)\n",
855
+ "sent_counts[\"share_positive\"] = sent_counts[\"positive\"] / den\n",
856
+ "sent_counts[\"share_neutral\"] = sent_counts[\"neutral\"] / den\n",
857
+ "sent_counts[\"share_negative\"] = sent_counts[\"negative\"] / den\n",
858
+ "sent_counts = sent_counts.reset_index()\n",
859
+ "\n",
860
+ "# --- Sales aggregation per title ---\n",
861
+ "sales_by_title = (\n",
862
+ " df_sales_r.dropna(subset=[\"title\"])\n",
863
+ " .groupby(\"title\", as_index=False)\n",
864
+ " .agg(\n",
865
+ " months_observed=(\"month\", \"nunique\"),\n",
866
+ " avg_units_sold=(\"units_sold\", \"mean\"),\n",
867
+ " total_units_sold=(\"units_sold\", \"sum\"),\n",
868
+ " )\n",
869
+ ")\n",
870
+ "\n",
871
+ "# --- Title-level features (join sales + books + sentiment) ---\n",
872
+ "df_title = (\n",
873
+ " sales_by_title\n",
874
+ " .merge(df_books_r[[\"title\", \"price\", \"rating\"]], on=\"title\", how=\"left\")\n",
875
+ " .merge(sent_counts[[\"title\", \"share_positive\", \"share_neutral\", \"share_negative\", \"total_reviews\"]],\n",
876
+ " on=\"title\", how=\"left\")\n",
877
+ ")\n",
878
+ "\n",
879
+ "df_title[\"avg_revenue\"] = df_title[\"avg_units_sold\"] * df_title[\"price\"]\n",
880
+ "df_title[\"total_revenue\"] = df_title[\"total_units_sold\"] * df_title[\"price\"]\n",
881
+ "\n",
882
+ "df_title.to_csv(\"synthetic_title_level_features.csv\", index=False)\n",
883
+ "print(\"✅ Wrote synthetic_title_level_features.csv\")\n",
884
+ "\n",
885
+ "# --- Monthly revenue series (proxy: units_sold * price) ---\n",
886
+ "monthly_rev = (\n",
887
+ " df_sales_r.merge(df_books_r[[\"title\", \"price\"]], on=\"title\", how=\"left\")\n",
888
+ ")\n",
889
+ "monthly_rev[\"revenue\"] = monthly_rev[\"units_sold\"] * monthly_rev[\"price\"]\n",
890
+ "\n",
891
+ "df_monthly = (\n",
892
+ " monthly_rev.dropna(subset=[\"month\"])\n",
893
+ " .groupby(\"month\", as_index=False)[\"revenue\"]\n",
894
+ " .sum()\n",
895
+ " .rename(columns={\"revenue\": \"total_revenue\"})\n",
896
+ " .sort_values(\"month\")\n",
897
+ ")\n",
898
+ "# if revenue is all NA (e.g., missing price), fallback to units_sold as a teaching proxy\n",
899
+ "if df_monthly[\"total_revenue\"].notna().sum() == 0:\n",
900
+ " df_monthly = (\n",
901
+ " df_sales_r.dropna(subset=[\"month\"])\n",
902
+ " .groupby(\"month\", as_index=False)[\"units_sold\"]\n",
903
+ " .sum()\n",
904
+ " .rename(columns={\"units_sold\": \"total_revenue\"})\n",
905
+ " .sort_values(\"month\")\n",
906
+ " )\n",
907
+ "\n",
908
+ "df_monthly[\"month\"] = pd.to_datetime(df_monthly[\"month\"], errors=\"coerce\").dt.strftime(\"%Y-%m-%d\")\n",
909
+ "df_monthly.to_csv(\"synthetic_monthly_revenue_series.csv\", index=False)\n",
910
+ "print(\"✅ Wrote synthetic_monthly_revenue_series.csv\")\n"
911
+ ]
912
+ },
913
+ {
914
+ "cell_type": "markdown",
915
+ "metadata": {
916
+ "id": "RYvGyVfXuo54"
917
+ },
918
+ "source": [
919
+ "### *d. ✋🏻🛑⛔️ View the first few lines*"
920
+ ]
921
+ },
922
+ {
923
+ "cell_type": "code",
924
+ "execution_count": 19,
925
+ "metadata": {
926
+ "colab": {
927
+ "base_uri": "https://localhost:8080/",
928
+ "height": 293
929
+ },
930
+ "id": "xfE8NMqOurKo",
931
+ "outputId": "b8aea9ba-38b5-45d4-f262-41596aa85e41"
932
+ },
933
+ "outputs": [
934
+ {
935
+ "output_type": "execute_result",
936
+ "data": {
937
+ "text/plain": [
938
+ " title sentiment_label \\\n",
939
+ "0 A Light in the Attic neutral \n",
940
+ "1 A Light in the Attic neutral \n",
941
+ "2 A Light in the Attic neutral \n",
942
+ "3 Tipping the Velvet negative \n",
943
+ "4 Tipping the Velvet negative \n",
944
+ "\n",
945
+ " review_text rating popularity_score \n",
946
+ "0 Some parts really stood out, others felt a bit... Three 3 \n",
947
+ "1 An average book — not great, but not bad either. Three 3 \n",
948
+ "2 It was okay overall. A decent way to pass the ... Three 3 \n",
949
+ "3 The plot was confusing and the characters felt... One 2 \n",
950
+ "4 Disappointing. I had high hopes, but they were... One 2 "
951
+ ],
952
+ "text/html": [
953
+ "\n",
954
+ " <div id=\"df-a9908bb6-b353-4446-9760-00caeb167a63\" class=\"colab-df-container\">\n",
955
+ " <div>\n",
956
+ "<style scoped>\n",
957
+ " .dataframe tbody tr th:only-of-type {\n",
958
+ " vertical-align: middle;\n",
959
+ " }\n",
960
+ "\n",
961
+ " .dataframe tbody tr th {\n",
962
+ " vertical-align: top;\n",
963
+ " }\n",
964
+ "\n",
965
+ " .dataframe thead th {\n",
966
+ " text-align: right;\n",
967
+ " }\n",
968
+ "</style>\n",
969
+ "<table border=\"1\" class=\"dataframe\">\n",
970
+ " <thead>\n",
971
+ " <tr style=\"text-align: right;\">\n",
972
+ " <th></th>\n",
973
+ " <th>title</th>\n",
974
+ " <th>sentiment_label</th>\n",
975
+ " <th>review_text</th>\n",
976
+ " <th>rating</th>\n",
977
+ " <th>popularity_score</th>\n",
978
+ " </tr>\n",
979
+ " </thead>\n",
980
+ " <tbody>\n",
981
+ " <tr>\n",
982
+ " <th>0</th>\n",
983
+ " <td>A Light in the Attic</td>\n",
984
+ " <td>neutral</td>\n",
985
+ " <td>Some parts really stood out, others felt a bit...</td>\n",
986
+ " <td>Three</td>\n",
987
+ " <td>3</td>\n",
988
+ " </tr>\n",
989
+ " <tr>\n",
990
+ " <th>1</th>\n",
991
+ " <td>A Light in the Attic</td>\n",
992
+ " <td>neutral</td>\n",
993
+ " <td>An average book — not great, but not bad either.</td>\n",
994
+ " <td>Three</td>\n",
995
+ " <td>3</td>\n",
996
+ " </tr>\n",
997
+ " <tr>\n",
998
+ " <th>2</th>\n",
999
+ " <td>A Light in the Attic</td>\n",
1000
+ " <td>neutral</td>\n",
1001
+ " <td>It was okay overall. A decent way to pass the ...</td>\n",
1002
+ " <td>Three</td>\n",
1003
+ " <td>3</td>\n",
1004
+ " </tr>\n",
1005
+ " <tr>\n",
1006
+ " <th>3</th>\n",
1007
+ " <td>Tipping the Velvet</td>\n",
1008
+ " <td>negative</td>\n",
1009
+ " <td>The plot was confusing and the characters felt...</td>\n",
1010
+ " <td>One</td>\n",
1011
+ " <td>2</td>\n",
1012
+ " </tr>\n",
1013
+ " <tr>\n",
1014
+ " <th>4</th>\n",
1015
+ " <td>Tipping the Velvet</td>\n",
1016
+ " <td>negative</td>\n",
1017
+ " <td>Disappointing. I had high hopes, but they were...</td>\n",
1018
+ " <td>One</td>\n",
1019
+ " <td>2</td>\n",
1020
+ " </tr>\n",
1021
+ " </tbody>\n",
1022
+ "</table>\n",
1023
+ "</div>\n",
1024
+ " <div class=\"colab-df-buttons\">\n",
1025
+ "\n",
1026
+ " <div class=\"colab-df-container\">\n",
1027
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-a9908bb6-b353-4446-9760-00caeb167a63')\"\n",
1028
+ " title=\"Convert this dataframe to an interactive table.\"\n",
1029
+ " style=\"display:none;\">\n",
1030
+ "\n",
1031
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
1032
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
1033
+ " </svg>\n",
1034
+ " </button>\n",
1035
+ "\n",
1036
+ " <style>\n",
1037
+ " .colab-df-container {\n",
1038
+ " display:flex;\n",
1039
+ " gap: 12px;\n",
1040
+ " }\n",
1041
+ "\n",
1042
+ " .colab-df-convert {\n",
1043
+ " background-color: #E8F0FE;\n",
1044
+ " border: none;\n",
1045
+ " border-radius: 50%;\n",
1046
+ " cursor: pointer;\n",
1047
+ " display: none;\n",
1048
+ " fill: #1967D2;\n",
1049
+ " height: 32px;\n",
1050
+ " padding: 0 0 0 0;\n",
1051
+ " width: 32px;\n",
1052
+ " }\n",
1053
+ "\n",
1054
+ " .colab-df-convert:hover {\n",
1055
+ " background-color: #E2EBFA;\n",
1056
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1057
+ " fill: #174EA6;\n",
1058
+ " }\n",
1059
+ "\n",
1060
+ " .colab-df-buttons div {\n",
1061
+ " margin-bottom: 4px;\n",
1062
+ " }\n",
1063
+ "\n",
1064
+ " [theme=dark] .colab-df-convert {\n",
1065
+ " background-color: #3B4455;\n",
1066
+ " fill: #D2E3FC;\n",
1067
+ " }\n",
1068
+ "\n",
1069
+ " [theme=dark] .colab-df-convert:hover {\n",
1070
+ " background-color: #434B5C;\n",
1071
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1072
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1073
+ " fill: #FFFFFF;\n",
1074
+ " }\n",
1075
+ " </style>\n",
1076
+ "\n",
1077
+ " <script>\n",
1078
+ " const buttonEl =\n",
1079
+ " document.querySelector('#df-a9908bb6-b353-4446-9760-00caeb167a63 button.colab-df-convert');\n",
1080
+ " buttonEl.style.display =\n",
1081
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1082
+ "\n",
1083
+ " async function convertToInteractive(key) {\n",
1084
+ " const element = document.querySelector('#df-a9908bb6-b353-4446-9760-00caeb167a63');\n",
1085
+ " const dataTable =\n",
1086
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1087
+ " [key], {});\n",
1088
+ " if (!dataTable) return;\n",
1089
+ "\n",
1090
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1091
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1092
+ " + ' to learn more about interactive tables.';\n",
1093
+ " element.innerHTML = '';\n",
1094
+ " dataTable['output_type'] = 'display_data';\n",
1095
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1096
+ " const docLink = document.createElement('div');\n",
1097
+ " docLink.innerHTML = docLinkHtml;\n",
1098
+ " element.appendChild(docLink);\n",
1099
+ " }\n",
1100
+ " </script>\n",
1101
+ " </div>\n",
1102
+ "\n",
1103
+ "\n",
1104
+ " </div>\n",
1105
+ " </div>\n"
1106
+ ],
1107
+ "application/vnd.google.colaboratory.intrinsic+json": {
1108
+ "type": "dataframe",
1109
+ "variable_name": "df_reviews",
1110
+ "summary": "{\n \"name\": \"df_reviews\",\n \"rows\": 3000,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 999,\n \"samples\": [\n \"The Grownup\",\n \"Persepolis: The Story of a Childhood (Persepolis #1-2)\",\n \"Ayumi's Violin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"sentiment_label\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"neutral\",\n \"negative\",\n \"positive\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"review_text\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 9,\n \"samples\": [\n \"A compelling and heartwarming read that stayed with me long after I finished.\",\n \"An average book \\u2014 not great, but not bad either.\",\n \"I struggled to get through this one \\u2014 it just didn\\u2019t grab me.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"One\",\n \"Two\",\n \"Four\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"popularity_score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 5,\n \"num_unique_values\": 5,\n \"samples\": [\n 2,\n 5,\n 4\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
1111
+ }
1112
+ },
1113
+ "metadata": {},
1114
+ "execution_count": 19
1115
+ }
1116
+ ],
1117
+ "source": [
1118
+ "df_reviews.head()"
1119
+ ]
1120
+ }
1121
+ ],
1122
+ "metadata": {
1123
+ "colab": {
1124
+ "collapsed_sections": [
1125
+ "jpASMyIQMaAq",
1126
+ "lquNYCbfL9IM",
1127
+ "0IWuNpxxYDJF",
1128
+ "oCdTsin2Yfp3",
1129
+ "T0TOeRC4Yrnn",
1130
+ "duI5dv3CZYvF",
1131
+ "qMjRKMBQZlJi",
1132
+ "p-1Pr2szaqLk",
1133
+ "SIaJUGIpaH4V",
1134
+ "pY4yCoIuaQqp",
1135
+ "n4-TaNTFgPak",
1136
+ "HnngRNTgacYt",
1137
+ "HF9F9HIzgT7Z",
1138
+ "T8AdKkmASq9a",
1139
+ "OhXbdGD5fH0c",
1140
+ "L2ak1HlcgoTe",
1141
+ "4IXZKcCSgxnq",
1142
+ "EhIjz9WohAmZ",
1143
+ "Gi4y9M9KuDWx",
1144
+ "fQhfVaDmuULT",
1145
+ "bmJMXF-Bukdm",
1146
+ "RYvGyVfXuo54"
1147
+ ],
1148
+ "provenance": []
1149
+ },
1150
+ "kernelspec": {
1151
+ "display_name": "Python 3",
1152
+ "name": "python3"
1153
+ },
1154
+ "language_info": {
1155
+ "name": "python"
1156
+ }
1157
+ },
1158
+ "nbformat": 4,
1159
+ "nbformat_minor": 0
1160
+ }