1_Data_Creation_(2).ipynb ADDED
@@ -0,0 +1,2017 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": 39,
24
+ "metadata": {
25
+ "colab": {
26
+ "base_uri": "https://localhost:8080/"
27
+ },
28
+ "id": "f48c8f8c",
29
+ "outputId": "4f65d7d2-075c-46e2-b9a5-c824813a6712"
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.61.1)\n",
50
+ "Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (1.4.9)\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": 40,
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": 41,
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": 42,
149
+ "metadata": {
150
+ "id": "l5FkkNhUYTHh",
151
+ "colab": {
152
+ "base_uri": "https://localhost:8080/",
153
+ "height": 206
154
+ },
155
+ "outputId": "8e8f7559-2792-473c-f77e-8cb166559adc"
156
+ },
157
+ "outputs": [
158
+ {
159
+ "output_type": "execute_result",
160
+ "data": {
161
+ "text/plain": [
162
+ " title price rating\n",
163
+ "0 A Light in the Attic 51.77 Three\n",
164
+ "1 Tipping the Velvet 53.74 One\n",
165
+ "2 Soumission 50.10 One\n",
166
+ "3 Sharp Objects 47.82 Four\n",
167
+ "4 Sapiens: A Brief History of Humankind 54.23 Five"
168
+ ],
169
+ "text/html": [
170
+ "\n",
171
+ " <div id=\"df-406c2ed0-2308-49c2-8d2e-757e0ff8561a\" class=\"colab-df-container\">\n",
172
+ " <div>\n",
173
+ "<style scoped>\n",
174
+ " .dataframe tbody tr th:only-of-type {\n",
175
+ " vertical-align: middle;\n",
176
+ " }\n",
177
+ "\n",
178
+ " .dataframe tbody tr th {\n",
179
+ " vertical-align: top;\n",
180
+ " }\n",
181
+ "\n",
182
+ " .dataframe thead th {\n",
183
+ " text-align: right;\n",
184
+ " }\n",
185
+ "</style>\n",
186
+ "<table border=\"1\" class=\"dataframe\">\n",
187
+ " <thead>\n",
188
+ " <tr style=\"text-align: right;\">\n",
189
+ " <th></th>\n",
190
+ " <th>title</th>\n",
191
+ " <th>price</th>\n",
192
+ " <th>rating</th>\n",
193
+ " </tr>\n",
194
+ " </thead>\n",
195
+ " <tbody>\n",
196
+ " <tr>\n",
197
+ " <th>0</th>\n",
198
+ " <td>A Light in the Attic</td>\n",
199
+ " <td>51.77</td>\n",
200
+ " <td>Three</td>\n",
201
+ " </tr>\n",
202
+ " <tr>\n",
203
+ " <th>1</th>\n",
204
+ " <td>Tipping the Velvet</td>\n",
205
+ " <td>53.74</td>\n",
206
+ " <td>One</td>\n",
207
+ " </tr>\n",
208
+ " <tr>\n",
209
+ " <th>2</th>\n",
210
+ " <td>Soumission</td>\n",
211
+ " <td>50.10</td>\n",
212
+ " <td>One</td>\n",
213
+ " </tr>\n",
214
+ " <tr>\n",
215
+ " <th>3</th>\n",
216
+ " <td>Sharp Objects</td>\n",
217
+ " <td>47.82</td>\n",
218
+ " <td>Four</td>\n",
219
+ " </tr>\n",
220
+ " <tr>\n",
221
+ " <th>4</th>\n",
222
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
223
+ " <td>54.23</td>\n",
224
+ " <td>Five</td>\n",
225
+ " </tr>\n",
226
+ " </tbody>\n",
227
+ "</table>\n",
228
+ "</div>\n",
229
+ " <div class=\"colab-df-buttons\">\n",
230
+ "\n",
231
+ " <div class=\"colab-df-container\">\n",
232
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-406c2ed0-2308-49c2-8d2e-757e0ff8561a')\"\n",
233
+ " title=\"Convert this dataframe to an interactive table.\"\n",
234
+ " style=\"display:none;\">\n",
235
+ "\n",
236
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
237
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
238
+ " </svg>\n",
239
+ " </button>\n",
240
+ "\n",
241
+ " <style>\n",
242
+ " .colab-df-container {\n",
243
+ " display:flex;\n",
244
+ " gap: 12px;\n",
245
+ " }\n",
246
+ "\n",
247
+ " .colab-df-convert {\n",
248
+ " background-color: #E8F0FE;\n",
249
+ " border: none;\n",
250
+ " border-radius: 50%;\n",
251
+ " cursor: pointer;\n",
252
+ " display: none;\n",
253
+ " fill: #1967D2;\n",
254
+ " height: 32px;\n",
255
+ " padding: 0 0 0 0;\n",
256
+ " width: 32px;\n",
257
+ " }\n",
258
+ "\n",
259
+ " .colab-df-convert:hover {\n",
260
+ " background-color: #E2EBFA;\n",
261
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
262
+ " fill: #174EA6;\n",
263
+ " }\n",
264
+ "\n",
265
+ " .colab-df-buttons div {\n",
266
+ " margin-bottom: 4px;\n",
267
+ " }\n",
268
+ "\n",
269
+ " [theme=dark] .colab-df-convert {\n",
270
+ " background-color: #3B4455;\n",
271
+ " fill: #D2E3FC;\n",
272
+ " }\n",
273
+ "\n",
274
+ " [theme=dark] .colab-df-convert:hover {\n",
275
+ " background-color: #434B5C;\n",
276
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
277
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
278
+ " fill: #FFFFFF;\n",
279
+ " }\n",
280
+ " </style>\n",
281
+ "\n",
282
+ " <script>\n",
283
+ " const buttonEl =\n",
284
+ " document.querySelector('#df-406c2ed0-2308-49c2-8d2e-757e0ff8561a button.colab-df-convert');\n",
285
+ " buttonEl.style.display =\n",
286
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
287
+ "\n",
288
+ " async function convertToInteractive(key) {\n",
289
+ " const element = document.querySelector('#df-406c2ed0-2308-49c2-8d2e-757e0ff8561a');\n",
290
+ " const dataTable =\n",
291
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
292
+ " [key], {});\n",
293
+ " if (!dataTable) return;\n",
294
+ "\n",
295
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
296
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
297
+ " + ' to learn more about interactive tables.';\n",
298
+ " element.innerHTML = '';\n",
299
+ " dataTable['output_type'] = 'display_data';\n",
300
+ " await google.colab.output.renderOutput(dataTable, element);\n",
301
+ " const docLink = document.createElement('div');\n",
302
+ " docLink.innerHTML = docLinkHtml;\n",
303
+ " element.appendChild(docLink);\n",
304
+ " }\n",
305
+ " </script>\n",
306
+ " </div>\n",
307
+ "\n",
308
+ "\n",
309
+ " </div>\n",
310
+ " </div>\n"
311
+ ],
312
+ "application/vnd.google.colaboratory.intrinsic+json": {
313
+ "type": "dataframe",
314
+ "variable_name": "df_books",
315
+ "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}"
316
+ }
317
+ },
318
+ "metadata": {},
319
+ "execution_count": 42
320
+ }
321
+ ],
322
+ "source": [
323
+ "import pandas as pd\n",
324
+ "\n",
325
+ "# Create dataframe\n",
326
+ "df_books = pd.DataFrame({\n",
327
+ " \"title\": titles,\n",
328
+ " \"price\": prices,\n",
329
+ " \"rating\": ratings\n",
330
+ "})\n",
331
+ "\n",
332
+ "# View first rows\n",
333
+ "df_books.head()"
334
+ ]
335
+ },
336
+ {
337
+ "cell_type": "markdown",
338
+ "metadata": {
339
+ "id": "duI5dv3CZYvF"
340
+ },
341
+ "source": [
342
+ "### *d. Save web-scraped dataframe either as a CSV or Excel file*"
343
+ ]
344
+ },
345
+ {
346
+ "cell_type": "code",
347
+ "execution_count": 43,
348
+ "metadata": {
349
+ "id": "lC1U_YHtZifh"
350
+ },
351
+ "outputs": [],
352
+ "source": [
353
+ "# Save to CSV\n",
354
+ "df_books.to_csv(\"books_data.csv\", index=False)\n",
355
+ "\n",
356
+ "# Or save to Excel\n",
357
+ "# df_books.to_excel(\"books_data.xlsx\", index=False)"
358
+ ]
359
+ },
360
+ {
361
+ "cell_type": "markdown",
362
+ "metadata": {
363
+ "id": "qMjRKMBQZlJi"
364
+ },
365
+ "source": [
366
+ "### *e. ✋🏻🛑⛔️ View first fiew lines*"
367
+ ]
368
+ },
369
+ {
370
+ "cell_type": "code",
371
+ "execution_count": 44,
372
+ "metadata": {
373
+ "colab": {
374
+ "base_uri": "https://localhost:8080/",
375
+ "height": 206
376
+ },
377
+ "id": "O_wIvTxYZqCK",
378
+ "outputId": "92b767d7-7954-41fd-f3d6-91caa841fd05"
379
+ },
380
+ "outputs": [
381
+ {
382
+ "output_type": "execute_result",
383
+ "data": {
384
+ "text/plain": [
385
+ " title price rating\n",
386
+ "0 A Light in the Attic 51.77 Three\n",
387
+ "1 Tipping the Velvet 53.74 One\n",
388
+ "2 Soumission 50.10 One\n",
389
+ "3 Sharp Objects 47.82 Four\n",
390
+ "4 Sapiens: A Brief History of Humankind 54.23 Five"
391
+ ],
392
+ "text/html": [
393
+ "\n",
394
+ " <div id=\"df-9fb165d7-438f-49c8-b673-c831d3ba64e9\" class=\"colab-df-container\">\n",
395
+ " <div>\n",
396
+ "<style scoped>\n",
397
+ " .dataframe tbody tr th:only-of-type {\n",
398
+ " vertical-align: middle;\n",
399
+ " }\n",
400
+ "\n",
401
+ " .dataframe tbody tr th {\n",
402
+ " vertical-align: top;\n",
403
+ " }\n",
404
+ "\n",
405
+ " .dataframe thead th {\n",
406
+ " text-align: right;\n",
407
+ " }\n",
408
+ "</style>\n",
409
+ "<table border=\"1\" class=\"dataframe\">\n",
410
+ " <thead>\n",
411
+ " <tr style=\"text-align: right;\">\n",
412
+ " <th></th>\n",
413
+ " <th>title</th>\n",
414
+ " <th>price</th>\n",
415
+ " <th>rating</th>\n",
416
+ " </tr>\n",
417
+ " </thead>\n",
418
+ " <tbody>\n",
419
+ " <tr>\n",
420
+ " <th>0</th>\n",
421
+ " <td>A Light in the Attic</td>\n",
422
+ " <td>51.77</td>\n",
423
+ " <td>Three</td>\n",
424
+ " </tr>\n",
425
+ " <tr>\n",
426
+ " <th>1</th>\n",
427
+ " <td>Tipping the Velvet</td>\n",
428
+ " <td>53.74</td>\n",
429
+ " <td>One</td>\n",
430
+ " </tr>\n",
431
+ " <tr>\n",
432
+ " <th>2</th>\n",
433
+ " <td>Soumission</td>\n",
434
+ " <td>50.10</td>\n",
435
+ " <td>One</td>\n",
436
+ " </tr>\n",
437
+ " <tr>\n",
438
+ " <th>3</th>\n",
439
+ " <td>Sharp Objects</td>\n",
440
+ " <td>47.82</td>\n",
441
+ " <td>Four</td>\n",
442
+ " </tr>\n",
443
+ " <tr>\n",
444
+ " <th>4</th>\n",
445
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
446
+ " <td>54.23</td>\n",
447
+ " <td>Five</td>\n",
448
+ " </tr>\n",
449
+ " </tbody>\n",
450
+ "</table>\n",
451
+ "</div>\n",
452
+ " <div class=\"colab-df-buttons\">\n",
453
+ "\n",
454
+ " <div class=\"colab-df-container\">\n",
455
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-9fb165d7-438f-49c8-b673-c831d3ba64e9')\"\n",
456
+ " title=\"Convert this dataframe to an interactive table.\"\n",
457
+ " style=\"display:none;\">\n",
458
+ "\n",
459
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
460
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
461
+ " </svg>\n",
462
+ " </button>\n",
463
+ "\n",
464
+ " <style>\n",
465
+ " .colab-df-container {\n",
466
+ " display:flex;\n",
467
+ " gap: 12px;\n",
468
+ " }\n",
469
+ "\n",
470
+ " .colab-df-convert {\n",
471
+ " background-color: #E8F0FE;\n",
472
+ " border: none;\n",
473
+ " border-radius: 50%;\n",
474
+ " cursor: pointer;\n",
475
+ " display: none;\n",
476
+ " fill: #1967D2;\n",
477
+ " height: 32px;\n",
478
+ " padding: 0 0 0 0;\n",
479
+ " width: 32px;\n",
480
+ " }\n",
481
+ "\n",
482
+ " .colab-df-convert:hover {\n",
483
+ " background-color: #E2EBFA;\n",
484
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
485
+ " fill: #174EA6;\n",
486
+ " }\n",
487
+ "\n",
488
+ " .colab-df-buttons div {\n",
489
+ " margin-bottom: 4px;\n",
490
+ " }\n",
491
+ "\n",
492
+ " [theme=dark] .colab-df-convert {\n",
493
+ " background-color: #3B4455;\n",
494
+ " fill: #D2E3FC;\n",
495
+ " }\n",
496
+ "\n",
497
+ " [theme=dark] .colab-df-convert:hover {\n",
498
+ " background-color: #434B5C;\n",
499
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
500
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
501
+ " fill: #FFFFFF;\n",
502
+ " }\n",
503
+ " </style>\n",
504
+ "\n",
505
+ " <script>\n",
506
+ " const buttonEl =\n",
507
+ " document.querySelector('#df-9fb165d7-438f-49c8-b673-c831d3ba64e9 button.colab-df-convert');\n",
508
+ " buttonEl.style.display =\n",
509
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
510
+ "\n",
511
+ " async function convertToInteractive(key) {\n",
512
+ " const element = document.querySelector('#df-9fb165d7-438f-49c8-b673-c831d3ba64e9');\n",
513
+ " const dataTable =\n",
514
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
515
+ " [key], {});\n",
516
+ " if (!dataTable) return;\n",
517
+ "\n",
518
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
519
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
520
+ " + ' to learn more about interactive tables.';\n",
521
+ " element.innerHTML = '';\n",
522
+ " dataTable['output_type'] = 'display_data';\n",
523
+ " await google.colab.output.renderOutput(dataTable, element);\n",
524
+ " const docLink = document.createElement('div');\n",
525
+ " docLink.innerHTML = docLinkHtml;\n",
526
+ " element.appendChild(docLink);\n",
527
+ " }\n",
528
+ " </script>\n",
529
+ " </div>\n",
530
+ "\n",
531
+ "\n",
532
+ " </div>\n",
533
+ " </div>\n"
534
+ ],
535
+ "application/vnd.google.colaboratory.intrinsic+json": {
536
+ "type": "dataframe",
537
+ "variable_name": "df_books",
538
+ "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}"
539
+ }
540
+ },
541
+ "metadata": {},
542
+ "execution_count": 44
543
+ }
544
+ ],
545
+ "source": [
546
+ "# View first few lines\n",
547
+ "df_books.head()"
548
+ ]
549
+ },
550
+ {
551
+ "cell_type": "markdown",
552
+ "metadata": {
553
+ "id": "p-1Pr2szaqLk"
554
+ },
555
+ "source": [
556
+ "## **3.** 🧩 Create a meaningful connection between real & synthetic datasets"
557
+ ]
558
+ },
559
+ {
560
+ "cell_type": "markdown",
561
+ "metadata": {
562
+ "id": "SIaJUGIpaH4V"
563
+ },
564
+ "source": [
565
+ "### *a. Initial setup*"
566
+ ]
567
+ },
568
+ {
569
+ "cell_type": "code",
570
+ "execution_count": 45,
571
+ "metadata": {
572
+ "id": "-gPXGcRPuV_9"
573
+ },
574
+ "outputs": [],
575
+ "source": [
576
+ "import numpy as np\n",
577
+ "import random\n",
578
+ "from datetime import datetime\n",
579
+ "import warnings\n",
580
+ "\n",
581
+ "warnings.filterwarnings(\"ignore\")\n",
582
+ "random.seed(2025)\n",
583
+ "np.random.seed(2025)"
584
+ ]
585
+ },
586
+ {
587
+ "cell_type": "markdown",
588
+ "metadata": {
589
+ "id": "pY4yCoIuaQqp"
590
+ },
591
+ "source": [
592
+ "### *b. Generate popularity scores based on rating (with some randomness) with a generate_popularity_score function*"
593
+ ]
594
+ },
595
+ {
596
+ "cell_type": "code",
597
+ "execution_count": 46,
598
+ "metadata": {
599
+ "id": "mnd5hdAbaNjz"
600
+ },
601
+ "outputs": [],
602
+ "source": [
603
+ "def generate_popularity_score(rating):\n",
604
+ " base = {\"One\": 2, \"Two\": 3, \"Three\": 3, \"Four\": 4, \"Five\": 4}.get(rating, 3)\n",
605
+ " trend_factor = random.choices([-1, 0, 1], weights=[1, 3, 2])[0]\n",
606
+ " return int(np.clip(base + trend_factor, 1, 5))"
607
+ ]
608
+ },
609
+ {
610
+ "cell_type": "markdown",
611
+ "metadata": {
612
+ "id": "n4-TaNTFgPak"
613
+ },
614
+ "source": [
615
+ "### *c. ✋🏻🛑⛔️ Run the function to create a \"popularity_score\" column from \"rating\"*"
616
+ ]
617
+ },
618
+ {
619
+ "cell_type": "code",
620
+ "execution_count": 47,
621
+ "metadata": {
622
+ "id": "V-G3OCUCgR07",
623
+ "colab": {
624
+ "base_uri": "https://localhost:8080/",
625
+ "height": 206
626
+ },
627
+ "outputId": "64821b96-7095-4e9e-b281-45fe4c349cd9"
628
+ },
629
+ "outputs": [
630
+ {
631
+ "output_type": "execute_result",
632
+ "data": {
633
+ "text/plain": [
634
+ " title price rating popularity_score\n",
635
+ "0 A Light in the Attic 51.77 Three 3\n",
636
+ "1 Tipping the Velvet 53.74 One 2\n",
637
+ "2 Soumission 50.10 One 2\n",
638
+ "3 Sharp Objects 47.82 Four 4\n",
639
+ "4 Sapiens: A Brief History of Humankind 54.23 Five 3"
640
+ ],
641
+ "text/html": [
642
+ "\n",
643
+ " <div id=\"df-fc04fa18-3a54-4a87-bae5-cecb75837863\" class=\"colab-df-container\">\n",
644
+ " <div>\n",
645
+ "<style scoped>\n",
646
+ " .dataframe tbody tr th:only-of-type {\n",
647
+ " vertical-align: middle;\n",
648
+ " }\n",
649
+ "\n",
650
+ " .dataframe tbody tr th {\n",
651
+ " vertical-align: top;\n",
652
+ " }\n",
653
+ "\n",
654
+ " .dataframe thead th {\n",
655
+ " text-align: right;\n",
656
+ " }\n",
657
+ "</style>\n",
658
+ "<table border=\"1\" class=\"dataframe\">\n",
659
+ " <thead>\n",
660
+ " <tr style=\"text-align: right;\">\n",
661
+ " <th></th>\n",
662
+ " <th>title</th>\n",
663
+ " <th>price</th>\n",
664
+ " <th>rating</th>\n",
665
+ " <th>popularity_score</th>\n",
666
+ " </tr>\n",
667
+ " </thead>\n",
668
+ " <tbody>\n",
669
+ " <tr>\n",
670
+ " <th>0</th>\n",
671
+ " <td>A Light in the Attic</td>\n",
672
+ " <td>51.77</td>\n",
673
+ " <td>Three</td>\n",
674
+ " <td>3</td>\n",
675
+ " </tr>\n",
676
+ " <tr>\n",
677
+ " <th>1</th>\n",
678
+ " <td>Tipping the Velvet</td>\n",
679
+ " <td>53.74</td>\n",
680
+ " <td>One</td>\n",
681
+ " <td>2</td>\n",
682
+ " </tr>\n",
683
+ " <tr>\n",
684
+ " <th>2</th>\n",
685
+ " <td>Soumission</td>\n",
686
+ " <td>50.10</td>\n",
687
+ " <td>One</td>\n",
688
+ " <td>2</td>\n",
689
+ " </tr>\n",
690
+ " <tr>\n",
691
+ " <th>3</th>\n",
692
+ " <td>Sharp Objects</td>\n",
693
+ " <td>47.82</td>\n",
694
+ " <td>Four</td>\n",
695
+ " <td>4</td>\n",
696
+ " </tr>\n",
697
+ " <tr>\n",
698
+ " <th>4</th>\n",
699
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
700
+ " <td>54.23</td>\n",
701
+ " <td>Five</td>\n",
702
+ " <td>3</td>\n",
703
+ " </tr>\n",
704
+ " </tbody>\n",
705
+ "</table>\n",
706
+ "</div>\n",
707
+ " <div class=\"colab-df-buttons\">\n",
708
+ "\n",
709
+ " <div class=\"colab-df-container\">\n",
710
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-fc04fa18-3a54-4a87-bae5-cecb75837863')\"\n",
711
+ " title=\"Convert this dataframe to an interactive table.\"\n",
712
+ " style=\"display:none;\">\n",
713
+ "\n",
714
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
715
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
716
+ " </svg>\n",
717
+ " </button>\n",
718
+ "\n",
719
+ " <style>\n",
720
+ " .colab-df-container {\n",
721
+ " display:flex;\n",
722
+ " gap: 12px;\n",
723
+ " }\n",
724
+ "\n",
725
+ " .colab-df-convert {\n",
726
+ " background-color: #E8F0FE;\n",
727
+ " border: none;\n",
728
+ " border-radius: 50%;\n",
729
+ " cursor: pointer;\n",
730
+ " display: none;\n",
731
+ " fill: #1967D2;\n",
732
+ " height: 32px;\n",
733
+ " padding: 0 0 0 0;\n",
734
+ " width: 32px;\n",
735
+ " }\n",
736
+ "\n",
737
+ " .colab-df-convert:hover {\n",
738
+ " background-color: #E2EBFA;\n",
739
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
740
+ " fill: #174EA6;\n",
741
+ " }\n",
742
+ "\n",
743
+ " .colab-df-buttons div {\n",
744
+ " margin-bottom: 4px;\n",
745
+ " }\n",
746
+ "\n",
747
+ " [theme=dark] .colab-df-convert {\n",
748
+ " background-color: #3B4455;\n",
749
+ " fill: #D2E3FC;\n",
750
+ " }\n",
751
+ "\n",
752
+ " [theme=dark] .colab-df-convert:hover {\n",
753
+ " background-color: #434B5C;\n",
754
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
755
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
756
+ " fill: #FFFFFF;\n",
757
+ " }\n",
758
+ " </style>\n",
759
+ "\n",
760
+ " <script>\n",
761
+ " const buttonEl =\n",
762
+ " document.querySelector('#df-fc04fa18-3a54-4a87-bae5-cecb75837863 button.colab-df-convert');\n",
763
+ " buttonEl.style.display =\n",
764
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
765
+ "\n",
766
+ " async function convertToInteractive(key) {\n",
767
+ " const element = document.querySelector('#df-fc04fa18-3a54-4a87-bae5-cecb75837863');\n",
768
+ " const dataTable =\n",
769
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
770
+ " [key], {});\n",
771
+ " if (!dataTable) return;\n",
772
+ "\n",
773
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
774
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
775
+ " + ' to learn more about interactive tables.';\n",
776
+ " element.innerHTML = '';\n",
777
+ " dataTable['output_type'] = 'display_data';\n",
778
+ " await google.colab.output.renderOutput(dataTable, element);\n",
779
+ " const docLink = document.createElement('div');\n",
780
+ " docLink.innerHTML = docLinkHtml;\n",
781
+ " element.appendChild(docLink);\n",
782
+ " }\n",
783
+ " </script>\n",
784
+ " </div>\n",
785
+ "\n",
786
+ "\n",
787
+ " </div>\n",
788
+ " </div>\n"
789
+ ],
790
+ "application/vnd.google.colaboratory.intrinsic+json": {
791
+ "type": "dataframe",
792
+ "variable_name": "df_books",
793
+ "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 \"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}"
794
+ }
795
+ },
796
+ "metadata": {},
797
+ "execution_count": 47
798
+ }
799
+ ],
800
+ "source": [
801
+ "import numpy as np\n",
802
+ "import random\n",
803
+ "\n",
804
+ "# Re-seed (optional but good practice)\n",
805
+ "random.seed(2025)\n",
806
+ "np.random.seed(2025)\n",
807
+ "\n",
808
+ "# Define the function\n",
809
+ "def generate_popularity_score(rating):\n",
810
+ " base = {\n",
811
+ " \"One\": 2,\n",
812
+ " \"Two\": 3,\n",
813
+ " \"Three\": 3,\n",
814
+ " \"Four\": 4,\n",
815
+ " \"Five\": 4\n",
816
+ " }.get(rating, 3)\n",
817
+ "\n",
818
+ " trend_factor = random.choices([-1, 0, 1], weights=[1, 3, 2])[0]\n",
819
+ "\n",
820
+ " return int(np.clip(base + trend_factor, 1, 5))\n",
821
+ "\n",
822
+ "# Create the new column\n",
823
+ "df_books[\"popularity_score\"] = df_books[\"rating\"].apply(generate_popularity_score)\n",
824
+ "\n",
825
+ "# View result\n",
826
+ "df_books.head()"
827
+ ]
828
+ },
829
+ {
830
+ "cell_type": "markdown",
831
+ "metadata": {
832
+ "id": "HnngRNTgacYt"
833
+ },
834
+ "source": [
835
+ "### *d. Decide on the sentiment_label based on the popularity score with a get_sentiment function*"
836
+ ]
837
+ },
838
+ {
839
+ "cell_type": "code",
840
+ "execution_count": 48,
841
+ "metadata": {
842
+ "id": "kUtWmr8maZLZ"
843
+ },
844
+ "outputs": [],
845
+ "source": [
846
+ "def get_sentiment(popularity_score):\n",
847
+ " if popularity_score <= 2:\n",
848
+ " return \"negative\"\n",
849
+ " elif popularity_score == 3:\n",
850
+ " return \"neutral\"\n",
851
+ " else:\n",
852
+ " return \"positive\""
853
+ ]
854
+ },
855
+ {
856
+ "cell_type": "markdown",
857
+ "metadata": {
858
+ "id": "HF9F9HIzgT7Z"
859
+ },
860
+ "source": [
861
+ "### *e. ✋🏻🛑⛔️ Run the function to create a \"sentiment_label\" column from \"popularity_score\"*"
862
+ ]
863
+ },
864
+ {
865
+ "cell_type": "code",
866
+ "execution_count": 49,
867
+ "metadata": {
868
+ "id": "tafQj8_7gYCG",
869
+ "colab": {
870
+ "base_uri": "https://localhost:8080/",
871
+ "height": 206
872
+ },
873
+ "outputId": "2c8fac9f-483d-4bf1-f232-37169b21eb06"
874
+ },
875
+ "outputs": [
876
+ {
877
+ "output_type": "execute_result",
878
+ "data": {
879
+ "text/plain": [
880
+ " title price rating popularity_score \\\n",
881
+ "0 A Light in the Attic 51.77 Three 3 \n",
882
+ "1 Tipping the Velvet 53.74 One 2 \n",
883
+ "2 Soumission 50.10 One 2 \n",
884
+ "3 Sharp Objects 47.82 Four 4 \n",
885
+ "4 Sapiens: A Brief History of Humankind 54.23 Five 3 \n",
886
+ "\n",
887
+ " sentiment_label \n",
888
+ "0 neutral \n",
889
+ "1 negative \n",
890
+ "2 negative \n",
891
+ "3 positive \n",
892
+ "4 neutral "
893
+ ],
894
+ "text/html": [
895
+ "\n",
896
+ " <div id=\"df-11331686-9ca2-426d-bdd8-8f640090a42b\" class=\"colab-df-container\">\n",
897
+ " <div>\n",
898
+ "<style scoped>\n",
899
+ " .dataframe tbody tr th:only-of-type {\n",
900
+ " vertical-align: middle;\n",
901
+ " }\n",
902
+ "\n",
903
+ " .dataframe tbody tr th {\n",
904
+ " vertical-align: top;\n",
905
+ " }\n",
906
+ "\n",
907
+ " .dataframe thead th {\n",
908
+ " text-align: right;\n",
909
+ " }\n",
910
+ "</style>\n",
911
+ "<table border=\"1\" class=\"dataframe\">\n",
912
+ " <thead>\n",
913
+ " <tr style=\"text-align: right;\">\n",
914
+ " <th></th>\n",
915
+ " <th>title</th>\n",
916
+ " <th>price</th>\n",
917
+ " <th>rating</th>\n",
918
+ " <th>popularity_score</th>\n",
919
+ " <th>sentiment_label</th>\n",
920
+ " </tr>\n",
921
+ " </thead>\n",
922
+ " <tbody>\n",
923
+ " <tr>\n",
924
+ " <th>0</th>\n",
925
+ " <td>A Light in the Attic</td>\n",
926
+ " <td>51.77</td>\n",
927
+ " <td>Three</td>\n",
928
+ " <td>3</td>\n",
929
+ " <td>neutral</td>\n",
930
+ " </tr>\n",
931
+ " <tr>\n",
932
+ " <th>1</th>\n",
933
+ " <td>Tipping the Velvet</td>\n",
934
+ " <td>53.74</td>\n",
935
+ " <td>One</td>\n",
936
+ " <td>2</td>\n",
937
+ " <td>negative</td>\n",
938
+ " </tr>\n",
939
+ " <tr>\n",
940
+ " <th>2</th>\n",
941
+ " <td>Soumission</td>\n",
942
+ " <td>50.10</td>\n",
943
+ " <td>One</td>\n",
944
+ " <td>2</td>\n",
945
+ " <td>negative</td>\n",
946
+ " </tr>\n",
947
+ " <tr>\n",
948
+ " <th>3</th>\n",
949
+ " <td>Sharp Objects</td>\n",
950
+ " <td>47.82</td>\n",
951
+ " <td>Four</td>\n",
952
+ " <td>4</td>\n",
953
+ " <td>positive</td>\n",
954
+ " </tr>\n",
955
+ " <tr>\n",
956
+ " <th>4</th>\n",
957
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
958
+ " <td>54.23</td>\n",
959
+ " <td>Five</td>\n",
960
+ " <td>3</td>\n",
961
+ " <td>neutral</td>\n",
962
+ " </tr>\n",
963
+ " </tbody>\n",
964
+ "</table>\n",
965
+ "</div>\n",
966
+ " <div class=\"colab-df-buttons\">\n",
967
+ "\n",
968
+ " <div class=\"colab-df-container\">\n",
969
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-11331686-9ca2-426d-bdd8-8f640090a42b')\"\n",
970
+ " title=\"Convert this dataframe to an interactive table.\"\n",
971
+ " style=\"display:none;\">\n",
972
+ "\n",
973
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
974
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
975
+ " </svg>\n",
976
+ " </button>\n",
977
+ "\n",
978
+ " <style>\n",
979
+ " .colab-df-container {\n",
980
+ " display:flex;\n",
981
+ " gap: 12px;\n",
982
+ " }\n",
983
+ "\n",
984
+ " .colab-df-convert {\n",
985
+ " background-color: #E8F0FE;\n",
986
+ " border: none;\n",
987
+ " border-radius: 50%;\n",
988
+ " cursor: pointer;\n",
989
+ " display: none;\n",
990
+ " fill: #1967D2;\n",
991
+ " height: 32px;\n",
992
+ " padding: 0 0 0 0;\n",
993
+ " width: 32px;\n",
994
+ " }\n",
995
+ "\n",
996
+ " .colab-df-convert:hover {\n",
997
+ " background-color: #E2EBFA;\n",
998
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
999
+ " fill: #174EA6;\n",
1000
+ " }\n",
1001
+ "\n",
1002
+ " .colab-df-buttons div {\n",
1003
+ " margin-bottom: 4px;\n",
1004
+ " }\n",
1005
+ "\n",
1006
+ " [theme=dark] .colab-df-convert {\n",
1007
+ " background-color: #3B4455;\n",
1008
+ " fill: #D2E3FC;\n",
1009
+ " }\n",
1010
+ "\n",
1011
+ " [theme=dark] .colab-df-convert:hover {\n",
1012
+ " background-color: #434B5C;\n",
1013
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1014
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1015
+ " fill: #FFFFFF;\n",
1016
+ " }\n",
1017
+ " </style>\n",
1018
+ "\n",
1019
+ " <script>\n",
1020
+ " const buttonEl =\n",
1021
+ " document.querySelector('#df-11331686-9ca2-426d-bdd8-8f640090a42b button.colab-df-convert');\n",
1022
+ " buttonEl.style.display =\n",
1023
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1024
+ "\n",
1025
+ " async function convertToInteractive(key) {\n",
1026
+ " const element = document.querySelector('#df-11331686-9ca2-426d-bdd8-8f640090a42b');\n",
1027
+ " const dataTable =\n",
1028
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1029
+ " [key], {});\n",
1030
+ " if (!dataTable) return;\n",
1031
+ "\n",
1032
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1033
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1034
+ " + ' to learn more about interactive tables.';\n",
1035
+ " element.innerHTML = '';\n",
1036
+ " dataTable['output_type'] = 'display_data';\n",
1037
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1038
+ " const docLink = document.createElement('div');\n",
1039
+ " docLink.innerHTML = docLinkHtml;\n",
1040
+ " element.appendChild(docLink);\n",
1041
+ " }\n",
1042
+ " </script>\n",
1043
+ " </div>\n",
1044
+ "\n",
1045
+ "\n",
1046
+ " </div>\n",
1047
+ " </div>\n"
1048
+ ],
1049
+ "application/vnd.google.colaboratory.intrinsic+json": {
1050
+ "type": "dataframe",
1051
+ "variable_name": "df_books",
1052
+ "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 \"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 \"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}"
1053
+ }
1054
+ },
1055
+ "metadata": {},
1056
+ "execution_count": 49
1057
+ }
1058
+ ],
1059
+ "source": [
1060
+ "# Define the sentiment function\n",
1061
+ "def get_sentiment(popularity_score):\n",
1062
+ " if popularity_score <= 2:\n",
1063
+ " return \"negative\"\n",
1064
+ " elif popularity_score == 3:\n",
1065
+ " return \"neutral\"\n",
1066
+ " else:\n",
1067
+ " return \"positive\"\n",
1068
+ "\n",
1069
+ "# Create sentiment_label column\n",
1070
+ "df_books[\"sentiment_label\"] = df_books[\"popularity_score\"].apply(get_sentiment)\n",
1071
+ "\n",
1072
+ "# View first rows\n",
1073
+ "df_books.head()"
1074
+ ]
1075
+ },
1076
+ {
1077
+ "cell_type": "markdown",
1078
+ "metadata": {
1079
+ "id": "T8AdKkmASq9a"
1080
+ },
1081
+ "source": [
1082
+ "## **4.** 📈 Generate synthetic book sales data of 18 months"
1083
+ ]
1084
+ },
1085
+ {
1086
+ "cell_type": "markdown",
1087
+ "metadata": {
1088
+ "id": "OhXbdGD5fH0c"
1089
+ },
1090
+ "source": [
1091
+ "### *a. Create a generate_sales_profit function that would generate sales patterns based on sentiment_label (with some randomness)*"
1092
+ ]
1093
+ },
1094
+ {
1095
+ "cell_type": "code",
1096
+ "execution_count": 50,
1097
+ "metadata": {
1098
+ "id": "qkVhYPXGbgEn"
1099
+ },
1100
+ "outputs": [],
1101
+ "source": [
1102
+ "def generate_sales_profile(sentiment):\n",
1103
+ " months = pd.date_range(end=datetime.today(), periods=18, freq=\"M\")\n",
1104
+ "\n",
1105
+ " if sentiment == \"positive\":\n",
1106
+ " base = random.randint(200, 300)\n",
1107
+ " trend = np.linspace(base, base + random.randint(20, 60), len(months))\n",
1108
+ " elif sentiment == \"negative\":\n",
1109
+ " base = random.randint(20, 80)\n",
1110
+ " trend = np.linspace(base, base - random.randint(10, 30), len(months))\n",
1111
+ " else: # neutral\n",
1112
+ " base = random.randint(80, 160)\n",
1113
+ " trend = np.full(len(months), base + random.randint(-10, 10))\n",
1114
+ "\n",
1115
+ " seasonality = 10 * np.sin(np.linspace(0, 3 * np.pi, len(months)))\n",
1116
+ " noise = np.random.normal(0, 5, len(months))\n",
1117
+ " monthly_sales = np.clip(trend + seasonality + noise, a_min=0, a_max=None).astype(int)\n",
1118
+ "\n",
1119
+ " return list(zip(months.strftime(\"%Y-%m\"), monthly_sales))"
1120
+ ]
1121
+ },
1122
+ {
1123
+ "cell_type": "markdown",
1124
+ "metadata": {
1125
+ "id": "L2ak1HlcgoTe"
1126
+ },
1127
+ "source": [
1128
+ "### *b. Run the function as part of building sales_data*"
1129
+ ]
1130
+ },
1131
+ {
1132
+ "cell_type": "code",
1133
+ "execution_count": 51,
1134
+ "metadata": {
1135
+ "id": "SlJ24AUafoDB"
1136
+ },
1137
+ "outputs": [],
1138
+ "source": [
1139
+ "sales_data = []\n",
1140
+ "for _, row in df_books.iterrows():\n",
1141
+ " records = generate_sales_profile(row[\"sentiment_label\"])\n",
1142
+ " for month, units in records:\n",
1143
+ " sales_data.append({\n",
1144
+ " \"title\": row[\"title\"],\n",
1145
+ " \"month\": month,\n",
1146
+ " \"units_sold\": units,\n",
1147
+ " \"sentiment_label\": row[\"sentiment_label\"]\n",
1148
+ " })"
1149
+ ]
1150
+ },
1151
+ {
1152
+ "cell_type": "markdown",
1153
+ "metadata": {
1154
+ "id": "4IXZKcCSgxnq"
1155
+ },
1156
+ "source": [
1157
+ "### *c. ✋🏻🛑⛔️ Create a df_sales DataFrame from sales_data*"
1158
+ ]
1159
+ },
1160
+ {
1161
+ "cell_type": "code",
1162
+ "execution_count": 52,
1163
+ "metadata": {
1164
+ "id": "wcN6gtiZg-ws"
1165
+ },
1166
+ "outputs": [],
1167
+ "source": [
1168
+ "import pandas as pd\n",
1169
+ "\n",
1170
+ "df_sales = pd.DataFrame(sales_data)"
1171
+ ]
1172
+ },
1173
+ {
1174
+ "cell_type": "markdown",
1175
+ "metadata": {
1176
+ "id": "EhIjz9WohAmZ"
1177
+ },
1178
+ "source": [
1179
+ "### *d. Save df_sales as synthetic_sales_data.csv & view first few lines*"
1180
+ ]
1181
+ },
1182
+ {
1183
+ "cell_type": "code",
1184
+ "execution_count": 53,
1185
+ "metadata": {
1186
+ "colab": {
1187
+ "base_uri": "https://localhost:8080/",
1188
+ "height": 206
1189
+ },
1190
+ "id": "MzbZvLcAhGaH",
1191
+ "outputId": "0a931b3d-73f6-4a0a-ec81-78fe688d3ce6"
1192
+ },
1193
+ "outputs": [
1194
+ {
1195
+ "output_type": "execute_result",
1196
+ "data": {
1197
+ "text/plain": [
1198
+ " title month units_sold sentiment_label\n",
1199
+ "0 A Light in the Attic 2024-09 100 neutral\n",
1200
+ "1 A Light in the Attic 2024-10 109 neutral\n",
1201
+ "2 A Light in the Attic 2024-11 102 neutral\n",
1202
+ "3 A Light in the Attic 2024-12 107 neutral\n",
1203
+ "4 A Light in the Attic 2025-01 108 neutral"
1204
+ ],
1205
+ "text/html": [
1206
+ "\n",
1207
+ " <div id=\"df-b9a07971-f462-4e0f-b9e4-9cc28b2e37e2\" class=\"colab-df-container\">\n",
1208
+ " <div>\n",
1209
+ "<style scoped>\n",
1210
+ " .dataframe tbody tr th:only-of-type {\n",
1211
+ " vertical-align: middle;\n",
1212
+ " }\n",
1213
+ "\n",
1214
+ " .dataframe tbody tr th {\n",
1215
+ " vertical-align: top;\n",
1216
+ " }\n",
1217
+ "\n",
1218
+ " .dataframe thead th {\n",
1219
+ " text-align: right;\n",
1220
+ " }\n",
1221
+ "</style>\n",
1222
+ "<table border=\"1\" class=\"dataframe\">\n",
1223
+ " <thead>\n",
1224
+ " <tr style=\"text-align: right;\">\n",
1225
+ " <th></th>\n",
1226
+ " <th>title</th>\n",
1227
+ " <th>month</th>\n",
1228
+ " <th>units_sold</th>\n",
1229
+ " <th>sentiment_label</th>\n",
1230
+ " </tr>\n",
1231
+ " </thead>\n",
1232
+ " <tbody>\n",
1233
+ " <tr>\n",
1234
+ " <th>0</th>\n",
1235
+ " <td>A Light in the Attic</td>\n",
1236
+ " <td>2024-09</td>\n",
1237
+ " <td>100</td>\n",
1238
+ " <td>neutral</td>\n",
1239
+ " </tr>\n",
1240
+ " <tr>\n",
1241
+ " <th>1</th>\n",
1242
+ " <td>A Light in the Attic</td>\n",
1243
+ " <td>2024-10</td>\n",
1244
+ " <td>109</td>\n",
1245
+ " <td>neutral</td>\n",
1246
+ " </tr>\n",
1247
+ " <tr>\n",
1248
+ " <th>2</th>\n",
1249
+ " <td>A Light in the Attic</td>\n",
1250
+ " <td>2024-11</td>\n",
1251
+ " <td>102</td>\n",
1252
+ " <td>neutral</td>\n",
1253
+ " </tr>\n",
1254
+ " <tr>\n",
1255
+ " <th>3</th>\n",
1256
+ " <td>A Light in the Attic</td>\n",
1257
+ " <td>2024-12</td>\n",
1258
+ " <td>107</td>\n",
1259
+ " <td>neutral</td>\n",
1260
+ " </tr>\n",
1261
+ " <tr>\n",
1262
+ " <th>4</th>\n",
1263
+ " <td>A Light in the Attic</td>\n",
1264
+ " <td>2025-01</td>\n",
1265
+ " <td>108</td>\n",
1266
+ " <td>neutral</td>\n",
1267
+ " </tr>\n",
1268
+ " </tbody>\n",
1269
+ "</table>\n",
1270
+ "</div>\n",
1271
+ " <div class=\"colab-df-buttons\">\n",
1272
+ "\n",
1273
+ " <div class=\"colab-df-container\">\n",
1274
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b9a07971-f462-4e0f-b9e4-9cc28b2e37e2')\"\n",
1275
+ " title=\"Convert this dataframe to an interactive table.\"\n",
1276
+ " style=\"display:none;\">\n",
1277
+ "\n",
1278
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
1279
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
1280
+ " </svg>\n",
1281
+ " </button>\n",
1282
+ "\n",
1283
+ " <style>\n",
1284
+ " .colab-df-container {\n",
1285
+ " display:flex;\n",
1286
+ " gap: 12px;\n",
1287
+ " }\n",
1288
+ "\n",
1289
+ " .colab-df-convert {\n",
1290
+ " background-color: #E8F0FE;\n",
1291
+ " border: none;\n",
1292
+ " border-radius: 50%;\n",
1293
+ " cursor: pointer;\n",
1294
+ " display: none;\n",
1295
+ " fill: #1967D2;\n",
1296
+ " height: 32px;\n",
1297
+ " padding: 0 0 0 0;\n",
1298
+ " width: 32px;\n",
1299
+ " }\n",
1300
+ "\n",
1301
+ " .colab-df-convert:hover {\n",
1302
+ " background-color: #E2EBFA;\n",
1303
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1304
+ " fill: #174EA6;\n",
1305
+ " }\n",
1306
+ "\n",
1307
+ " .colab-df-buttons div {\n",
1308
+ " margin-bottom: 4px;\n",
1309
+ " }\n",
1310
+ "\n",
1311
+ " [theme=dark] .colab-df-convert {\n",
1312
+ " background-color: #3B4455;\n",
1313
+ " fill: #D2E3FC;\n",
1314
+ " }\n",
1315
+ "\n",
1316
+ " [theme=dark] .colab-df-convert:hover {\n",
1317
+ " background-color: #434B5C;\n",
1318
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1319
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1320
+ " fill: #FFFFFF;\n",
1321
+ " }\n",
1322
+ " </style>\n",
1323
+ "\n",
1324
+ " <script>\n",
1325
+ " const buttonEl =\n",
1326
+ " document.querySelector('#df-b9a07971-f462-4e0f-b9e4-9cc28b2e37e2 button.colab-df-convert');\n",
1327
+ " buttonEl.style.display =\n",
1328
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1329
+ "\n",
1330
+ " async function convertToInteractive(key) {\n",
1331
+ " const element = document.querySelector('#df-b9a07971-f462-4e0f-b9e4-9cc28b2e37e2');\n",
1332
+ " const dataTable =\n",
1333
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1334
+ " [key], {});\n",
1335
+ " if (!dataTable) return;\n",
1336
+ "\n",
1337
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1338
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1339
+ " + ' to learn more about interactive tables.';\n",
1340
+ " element.innerHTML = '';\n",
1341
+ " dataTable['output_type'] = 'display_data';\n",
1342
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1343
+ " const docLink = document.createElement('div');\n",
1344
+ " docLink.innerHTML = docLinkHtml;\n",
1345
+ " element.appendChild(docLink);\n",
1346
+ " }\n",
1347
+ " </script>\n",
1348
+ " </div>\n",
1349
+ "\n",
1350
+ "\n",
1351
+ " </div>\n",
1352
+ " </div>\n"
1353
+ ],
1354
+ "application/vnd.google.colaboratory.intrinsic+json": {
1355
+ "type": "dataframe",
1356
+ "variable_name": "df_sales",
1357
+ "summary": "{\n \"name\": \"df_sales\",\n \"rows\": 18000,\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\": \"month\",\n \"properties\": {\n \"dtype\": \"object\",\n \"num_unique_values\": 18,\n \"samples\": [\n \"2024-09\",\n \"2024-10\",\n \"2025-05\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"units_sold\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 98,\n \"min\": 0,\n \"max\": 362,\n \"num_unique_values\": 354,\n \"samples\": [\n 214,\n 289,\n 205\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}"
1358
+ }
1359
+ },
1360
+ "metadata": {},
1361
+ "execution_count": 53
1362
+ }
1363
+ ],
1364
+ "source": [
1365
+ "# Save df_sales as CSV\n",
1366
+ "df_sales.to_csv(\"synthetic_sales_data.csv\", index=False)\n",
1367
+ "\n",
1368
+ "# View first few lines\n",
1369
+ "df_sales.head()"
1370
+ ]
1371
+ },
1372
+ {
1373
+ "cell_type": "markdown",
1374
+ "metadata": {
1375
+ "id": "7g9gqBgQMtJn"
1376
+ },
1377
+ "source": [
1378
+ "## **5.** 🎯 Generate synthetic customer reviews"
1379
+ ]
1380
+ },
1381
+ {
1382
+ "cell_type": "markdown",
1383
+ "metadata": {
1384
+ "id": "Gi4y9M9KuDWx"
1385
+ },
1386
+ "source": [
1387
+ "### *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*"
1388
+ ]
1389
+ },
1390
+ {
1391
+ "cell_type": "code",
1392
+ "execution_count": 54,
1393
+ "metadata": {
1394
+ "id": "b3cd2a50"
1395
+ },
1396
+ "outputs": [],
1397
+ "source": [
1398
+ "synthetic_reviews_by_sentiment = {\n",
1399
+ " \"positive\": [\n",
1400
+ " \"An absolutely wonderful read from start to finish.\",\n",
1401
+ " \"I couldn’t put this book down—truly captivating.\",\n",
1402
+ " \"The storytelling was immersive and beautifully crafted.\",\n",
1403
+ " \"A heartfelt and inspiring story.\",\n",
1404
+ " \"The characters were vivid and unforgettable.\",\n",
1405
+ " \"An engaging plot with excellent pacing.\",\n",
1406
+ " \"A beautifully written and emotionally rich novel.\",\n",
1407
+ " \"One of the best books I’ve read this year.\",\n",
1408
+ " \"A powerful and uplifting experience.\",\n",
1409
+ " \"The author’s writing style was elegant and compelling.\",\n",
1410
+ " \"An unforgettable journey from beginning to end.\",\n",
1411
+ " \"The themes were handled with depth and care.\",\n",
1412
+ " \"A gripping and satisfying read.\",\n",
1413
+ " \"An imaginative and creative masterpiece.\",\n",
1414
+ " \"The emotional impact was profound.\",\n",
1415
+ " \"A delightful and charming story.\",\n",
1416
+ " \"I was hooked from the first page.\",\n",
1417
+ " \"A wonderfully constructed narrative.\",\n",
1418
+ " \"An inspiring and thought-provoking novel.\",\n",
1419
+ " \"The character development was superb.\",\n",
1420
+ " \"A brilliant blend of emotion and storytelling.\",\n",
1421
+ " \"An exciting and immersive experience.\",\n",
1422
+ " \"The plot twists were perfectly executed.\",\n",
1423
+ " \"A refreshing and engaging perspective.\",\n",
1424
+ " \"A deeply moving and memorable book.\",\n",
1425
+ " \"An outstanding achievement in storytelling.\",\n",
1426
+ " \"The prose was smooth and captivating.\",\n",
1427
+ " \"A compelling and beautifully told tale.\",\n",
1428
+ " \"It exceeded all my expectations.\",\n",
1429
+ " \"A fantastic and rewarding read.\",\n",
1430
+ " \"The dialogue felt natural and authentic.\",\n",
1431
+ " \"An emotionally satisfying conclusion.\",\n",
1432
+ " \"A truly exceptional novel.\",\n",
1433
+ " \"The story flowed effortlessly.\",\n",
1434
+ " \"An inspiring and meaningful narrative.\",\n",
1435
+ " \"A captivating and heartfelt read.\",\n",
1436
+ " \"The pacing kept me engaged throughout.\",\n",
1437
+ " \"A masterfully written story.\",\n",
1438
+ " \"An entertaining and powerful book.\",\n",
1439
+ " \"A rich and immersive reading experience.\",\n",
1440
+ " \"An impressive and unforgettable novel.\",\n",
1441
+ " \"The storytelling was both creative and compelling.\",\n",
1442
+ " \"A stunning and beautifully crafted book.\",\n",
1443
+ " \"A five-star reading experience.\",\n",
1444
+ " \"A deeply engaging and satisfying story.\",\n",
1445
+ " \"An uplifting and memorable journey.\",\n",
1446
+ " \"The narrative was gripping and emotional.\",\n",
1447
+ " \"A beautifully structured novel.\",\n",
1448
+ " \"An inspiring and powerful read.\",\n",
1449
+ " \"A truly remarkable book.\"\n",
1450
+ " ],\n",
1451
+ " \"neutral\": [\n",
1452
+ " \"It was an okay read overall.\",\n",
1453
+ " \"The book had its moments but wasn’t remarkable.\",\n",
1454
+ " \"A fairly average story.\",\n",
1455
+ " \"Some parts were engaging, others less so.\",\n",
1456
+ " \"It met my expectations but didn’t exceed them.\",\n",
1457
+ " \"A decent way to pass the time.\",\n",
1458
+ " \"The writing was fine, nothing extraordinary.\",\n",
1459
+ " \"An average book with a straightforward plot.\",\n",
1460
+ " \"I found it moderately interesting.\",\n",
1461
+ " \"Neither particularly good nor bad.\",\n",
1462
+ " \"The characters were adequate but not memorable.\",\n",
1463
+ " \"It was entertaining enough.\",\n",
1464
+ " \"A standard story for the genre.\",\n",
1465
+ " \"Some chapters were stronger than others.\",\n",
1466
+ " \"The pacing was inconsistent at times.\",\n",
1467
+ " \"It held my attention in parts.\",\n",
1468
+ " \"An acceptable but unremarkable novel.\",\n",
1469
+ " \"The plot was predictable but serviceable.\",\n",
1470
+ " \"It was fine for a casual read.\",\n",
1471
+ " \"A reasonably written book.\",\n",
1472
+ " \"I didn’t love it, but I didn’t dislike it either.\",\n",
1473
+ " \"The themes were presented clearly but simply.\",\n",
1474
+ " \"A mild and easy read.\",\n",
1475
+ " \"It delivered what it promised.\",\n",
1476
+ " \"The story was straightforward and simple.\",\n",
1477
+ " \"An average reading experience.\",\n",
1478
+ " \"There were both strengths and weaknesses.\",\n",
1479
+ " \"It was somewhat engaging.\",\n",
1480
+ " \"A typical example of its genre.\",\n",
1481
+ " \"The ending was satisfactory.\",\n",
1482
+ " \"It had a few interesting ideas.\",\n",
1483
+ " \"The execution was decent overall.\",\n",
1484
+ " \"A passable and light read.\",\n",
1485
+ " \"It kept me mildly interested.\",\n",
1486
+ " \"The writing style was standard.\",\n",
1487
+ " \"Some characters stood out more than others.\",\n",
1488
+ " \"It was readable but not memorable.\",\n",
1489
+ " \"An ordinary but competent book.\",\n",
1490
+ " \"The story moved at a steady pace.\",\n",
1491
+ " \"It was fine for what it was.\",\n",
1492
+ " \"A serviceable narrative.\",\n",
1493
+ " \"The plot developed in a predictable way.\",\n",
1494
+ " \"An adequate storytelling effort.\",\n",
1495
+ " \"It had its ups and downs.\",\n",
1496
+ " \"A modest and simple read.\",\n",
1497
+ " \"The overall experience was balanced.\",\n",
1498
+ " \"It didn’t leave a strong impression.\",\n",
1499
+ " \"A fairly routine novel.\",\n",
1500
+ " \"It was acceptable but not standout.\",\n",
1501
+ " \"An unexceptional yet readable book.\"\n",
1502
+ " ],\n",
1503
+ " \"negative\": [\n",
1504
+ " \"I struggled to finish this book.\",\n",
1505
+ " \"The story failed to hold my interest.\",\n",
1506
+ " \"The plot felt confusing and disjointed.\",\n",
1507
+ " \"I was disappointed by the overall execution.\",\n",
1508
+ " \"The characters lacked depth and development.\",\n",
1509
+ " \"It didn’t live up to the hype.\",\n",
1510
+ " \"The pacing was painfully slow.\",\n",
1511
+ " \"I found the writing style hard to enjoy.\",\n",
1512
+ " \"The narrative felt flat and uninspired.\",\n",
1513
+ " \"A frustrating reading experience.\",\n",
1514
+ " \"The dialogue felt unnatural and forced.\",\n",
1515
+ " \"It was difficult to stay engaged.\",\n",
1516
+ " \"The story lacked originality.\",\n",
1517
+ " \"I expected much more from this book.\",\n",
1518
+ " \"The ending was unsatisfying.\",\n",
1519
+ " \"The plot holes were distracting.\",\n",
1520
+ " \"It felt overly long and drawn out.\",\n",
1521
+ " \"The characters were forgettable.\",\n",
1522
+ " \"The writing seemed rushed.\",\n",
1523
+ " \"I couldn’t connect with the story.\",\n",
1524
+ " \"The themes were poorly developed.\",\n",
1525
+ " \"It failed to capture my attention.\",\n",
1526
+ " \"A disappointing and underwhelming novel.\",\n",
1527
+ " \"The structure felt messy.\",\n",
1528
+ " \"The book lacked emotional impact.\",\n",
1529
+ " \"I found it boring overall.\",\n",
1530
+ " \"The storytelling was inconsistent.\",\n",
1531
+ " \"The conflict felt forced.\",\n",
1532
+ " \"It didn’t offer anything new.\",\n",
1533
+ " \"The pacing dragged throughout.\",\n",
1534
+ " \"I had trouble staying focused while reading.\",\n",
1535
+ " \"The plot twists were predictable.\",\n",
1536
+ " \"It felt repetitive and dull.\",\n",
1537
+ " \"The characters’ motivations were unclear.\",\n",
1538
+ " \"The writing lacked polish.\",\n",
1539
+ " \"A forgettable and bland read.\",\n",
1540
+ " \"The story never fully came together.\",\n",
1541
+ " \"It was not worth the time investment.\",\n",
1542
+ " \"The narrative felt shallow.\",\n",
1543
+ " \"I was left feeling unsatisfied.\",\n",
1544
+ " \"The book lacked cohesion.\",\n",
1545
+ " \"The execution fell short of expectations.\",\n",
1546
+ " \"It didn’t resonate with me at all.\",\n",
1547
+ " \"The story felt underdeveloped.\",\n",
1548
+ " \"A tedious reading experience.\",\n",
1549
+ " \"The author’s style didn’t work for me.\",\n",
1550
+ " \"The plot felt overly complicated.\",\n",
1551
+ " \"The book failed to engage me emotionally.\",\n",
1552
+ " \"It was a disappointing effort overall.\",\n",
1553
+ " \"I wouldn’t recommend this one.\"\n",
1554
+ " ]\n",
1555
+ "}"
1556
+ ]
1557
+ },
1558
+ {
1559
+ "cell_type": "markdown",
1560
+ "metadata": {
1561
+ "id": "fQhfVaDmuULT"
1562
+ },
1563
+ "source": [
1564
+ "### *b. Generate 10 reviews per book using random sampling from the corresponding 50*"
1565
+ ]
1566
+ },
1567
+ {
1568
+ "cell_type": "code",
1569
+ "execution_count": 55,
1570
+ "metadata": {
1571
+ "id": "l2SRc3PjuTGM"
1572
+ },
1573
+ "outputs": [],
1574
+ "source": [
1575
+ "review_rows = []\n",
1576
+ "for _, row in df_books.iterrows():\n",
1577
+ " title = row['title']\n",
1578
+ " sentiment_label = row['sentiment_label']\n",
1579
+ " review_pool = synthetic_reviews_by_sentiment[sentiment_label]\n",
1580
+ " sampled_reviews = random.sample(review_pool, 10)\n",
1581
+ " for review_text in sampled_reviews:\n",
1582
+ " review_rows.append({\n",
1583
+ " \"title\": title,\n",
1584
+ " \"sentiment_label\": sentiment_label,\n",
1585
+ " \"review_text\": review_text,\n",
1586
+ " \"rating\": row['rating'],\n",
1587
+ " \"popularity_score\": row['popularity_score']\n",
1588
+ " })"
1589
+ ]
1590
+ },
1591
+ {
1592
+ "cell_type": "markdown",
1593
+ "metadata": {
1594
+ "id": "bmJMXF-Bukdm"
1595
+ },
1596
+ "source": [
1597
+ "### *c. Create the final dataframe df_reviews & save it as synthetic_book_reviews.csv*"
1598
+ ]
1599
+ },
1600
+ {
1601
+ "cell_type": "code",
1602
+ "execution_count": 56,
1603
+ "metadata": {
1604
+ "id": "ZUKUqZsuumsp"
1605
+ },
1606
+ "outputs": [],
1607
+ "source": [
1608
+ "df_reviews = pd.DataFrame(review_rows)\n",
1609
+ "df_reviews.to_csv(\"synthetic_book_reviews.csv\", index=False)"
1610
+ ]
1611
+ },
1612
+ {
1613
+ "cell_type": "markdown",
1614
+ "source": [
1615
+ "### *c. inputs for R*"
1616
+ ],
1617
+ "metadata": {
1618
+ "id": "_602pYUS3gY5"
1619
+ }
1620
+ },
1621
+ {
1622
+ "cell_type": "code",
1623
+ "execution_count": 57,
1624
+ "metadata": {
1625
+ "colab": {
1626
+ "base_uri": "https://localhost:8080/"
1627
+ },
1628
+ "id": "3946e521",
1629
+ "outputId": "cac6769e-1856-4a5a-acb0-b4fe1ae98a56"
1630
+ },
1631
+ "outputs": [
1632
+ {
1633
+ "output_type": "stream",
1634
+ "name": "stdout",
1635
+ "text": [
1636
+ "✅ Wrote synthetic_title_level_features.csv\n",
1637
+ "✅ Wrote synthetic_monthly_revenue_series.csv\n"
1638
+ ]
1639
+ }
1640
+ ],
1641
+ "source": [
1642
+ "import numpy as np\n",
1643
+ "\n",
1644
+ "def _safe_num(s):\n",
1645
+ " return pd.to_numeric(\n",
1646
+ " pd.Series(s).astype(str).str.replace(r\"[^0-9.]\", \"\", regex=True),\n",
1647
+ " errors=\"coerce\"\n",
1648
+ " )\n",
1649
+ "\n",
1650
+ "# --- Clean book metadata (price/rating) ---\n",
1651
+ "df_books_r = df_books.copy()\n",
1652
+ "if \"price\" in df_books_r.columns:\n",
1653
+ " df_books_r[\"price\"] = _safe_num(df_books_r[\"price\"])\n",
1654
+ "if \"rating\" in df_books_r.columns:\n",
1655
+ " df_books_r[\"rating\"] = _safe_num(df_books_r[\"rating\"])\n",
1656
+ "\n",
1657
+ "df_books_r[\"title\"] = df_books_r[\"title\"].astype(str).str.strip()\n",
1658
+ "\n",
1659
+ "# --- Clean sales ---\n",
1660
+ "df_sales_r = df_sales.copy()\n",
1661
+ "df_sales_r[\"title\"] = df_sales_r[\"title\"].astype(str).str.strip()\n",
1662
+ "df_sales_r[\"month\"] = pd.to_datetime(df_sales_r[\"month\"], errors=\"coerce\")\n",
1663
+ "df_sales_r[\"units_sold\"] = _safe_num(df_sales_r[\"units_sold\"])\n",
1664
+ "\n",
1665
+ "# --- Clean reviews ---\n",
1666
+ "df_reviews_r = df_reviews.copy()\n",
1667
+ "df_reviews_r[\"title\"] = df_reviews_r[\"title\"].astype(str).str.strip()\n",
1668
+ "df_reviews_r[\"sentiment_label\"] = df_reviews_r[\"sentiment_label\"].astype(str).str.lower().str.strip()\n",
1669
+ "if \"rating\" in df_reviews_r.columns:\n",
1670
+ " df_reviews_r[\"rating\"] = _safe_num(df_reviews_r[\"rating\"])\n",
1671
+ "if \"popularity_score\" in df_reviews_r.columns:\n",
1672
+ " df_reviews_r[\"popularity_score\"] = _safe_num(df_reviews_r[\"popularity_score\"])\n",
1673
+ "\n",
1674
+ "# --- Sentiment shares per title (from reviews) ---\n",
1675
+ "sent_counts = (\n",
1676
+ " df_reviews_r.groupby([\"title\", \"sentiment_label\"])\n",
1677
+ " .size()\n",
1678
+ " .unstack(fill_value=0)\n",
1679
+ ")\n",
1680
+ "for lab in [\"positive\", \"neutral\", \"negative\"]:\n",
1681
+ " if lab not in sent_counts.columns:\n",
1682
+ " sent_counts[lab] = 0\n",
1683
+ "\n",
1684
+ "sent_counts[\"total_reviews\"] = sent_counts[[\"positive\", \"neutral\", \"negative\"]].sum(axis=1)\n",
1685
+ "den = sent_counts[\"total_reviews\"].replace(0, np.nan)\n",
1686
+ "sent_counts[\"share_positive\"] = sent_counts[\"positive\"] / den\n",
1687
+ "sent_counts[\"share_neutral\"] = sent_counts[\"neutral\"] / den\n",
1688
+ "sent_counts[\"share_negative\"] = sent_counts[\"negative\"] / den\n",
1689
+ "sent_counts = sent_counts.reset_index()\n",
1690
+ "\n",
1691
+ "# --- Sales aggregation per title ---\n",
1692
+ "sales_by_title = (\n",
1693
+ " df_sales_r.dropna(subset=[\"title\"])\n",
1694
+ " .groupby(\"title\", as_index=False)\n",
1695
+ " .agg(\n",
1696
+ " months_observed=(\"month\", \"nunique\"),\n",
1697
+ " avg_units_sold=(\"units_sold\", \"mean\"),\n",
1698
+ " total_units_sold=(\"units_sold\", \"sum\"),\n",
1699
+ " )\n",
1700
+ ")\n",
1701
+ "\n",
1702
+ "# --- Title-level features (join sales + books + sentiment) ---\n",
1703
+ "df_title = (\n",
1704
+ " sales_by_title\n",
1705
+ " .merge(df_books_r[[\"title\", \"price\", \"rating\"]], on=\"title\", how=\"left\")\n",
1706
+ " .merge(sent_counts[[\"title\", \"share_positive\", \"share_neutral\", \"share_negative\", \"total_reviews\"]],\n",
1707
+ " on=\"title\", how=\"left\")\n",
1708
+ ")\n",
1709
+ "\n",
1710
+ "df_title[\"avg_revenue\"] = df_title[\"avg_units_sold\"] * df_title[\"price\"]\n",
1711
+ "df_title[\"total_revenue\"] = df_title[\"total_units_sold\"] * df_title[\"price\"]\n",
1712
+ "\n",
1713
+ "df_title.to_csv(\"synthetic_title_level_features.csv\", index=False)\n",
1714
+ "print(\"✅ Wrote synthetic_title_level_features.csv\")\n",
1715
+ "\n",
1716
+ "# --- Monthly revenue series (proxy: units_sold * price) ---\n",
1717
+ "monthly_rev = (\n",
1718
+ " df_sales_r.merge(df_books_r[[\"title\", \"price\"]], on=\"title\", how=\"left\")\n",
1719
+ ")\n",
1720
+ "monthly_rev[\"revenue\"] = monthly_rev[\"units_sold\"] * monthly_rev[\"price\"]\n",
1721
+ "\n",
1722
+ "df_monthly = (\n",
1723
+ " monthly_rev.dropna(subset=[\"month\"])\n",
1724
+ " .groupby(\"month\", as_index=False)[\"revenue\"]\n",
1725
+ " .sum()\n",
1726
+ " .rename(columns={\"revenue\": \"total_revenue\"})\n",
1727
+ " .sort_values(\"month\")\n",
1728
+ ")\n",
1729
+ "# if revenue is all NA (e.g., missing price), fallback to units_sold as a teaching proxy\n",
1730
+ "if df_monthly[\"total_revenue\"].notna().sum() == 0:\n",
1731
+ " df_monthly = (\n",
1732
+ " df_sales_r.dropna(subset=[\"month\"])\n",
1733
+ " .groupby(\"month\", as_index=False)[\"units_sold\"]\n",
1734
+ " .sum()\n",
1735
+ " .rename(columns={\"units_sold\": \"total_revenue\"})\n",
1736
+ " .sort_values(\"month\")\n",
1737
+ " )\n",
1738
+ "\n",
1739
+ "df_monthly[\"month\"] = pd.to_datetime(df_monthly[\"month\"], errors=\"coerce\").dt.strftime(\"%Y-%m-%d\")\n",
1740
+ "df_monthly.to_csv(\"synthetic_monthly_revenue_series.csv\", index=False)\n",
1741
+ "print(\"✅ Wrote synthetic_monthly_revenue_series.csv\")\n"
1742
+ ]
1743
+ },
1744
+ {
1745
+ "cell_type": "markdown",
1746
+ "metadata": {
1747
+ "id": "RYvGyVfXuo54"
1748
+ },
1749
+ "source": [
1750
+ "### *d. ✋🏻🛑⛔️ View the first few lines*"
1751
+ ]
1752
+ },
1753
+ {
1754
+ "cell_type": "code",
1755
+ "execution_count": 58,
1756
+ "metadata": {
1757
+ "colab": {
1758
+ "base_uri": "https://localhost:8080/",
1759
+ "height": 206
1760
+ },
1761
+ "id": "xfE8NMqOurKo",
1762
+ "outputId": "39d8355e-626e-47cb-aa7c-524649dbbcd4"
1763
+ },
1764
+ "outputs": [
1765
+ {
1766
+ "output_type": "execute_result",
1767
+ "data": {
1768
+ "text/plain": [
1769
+ " title sentiment_label review_text \\\n",
1770
+ "0 A Light in the Attic neutral A passable and light read. \n",
1771
+ "1 A Light in the Attic neutral An average reading experience. \n",
1772
+ "2 A Light in the Attic neutral It was somewhat engaging. \n",
1773
+ "3 A Light in the Attic neutral It was acceptable but not standout. \n",
1774
+ "4 A Light in the Attic neutral A serviceable narrative. \n",
1775
+ "\n",
1776
+ " rating popularity_score \n",
1777
+ "0 Three 3 \n",
1778
+ "1 Three 3 \n",
1779
+ "2 Three 3 \n",
1780
+ "3 Three 3 \n",
1781
+ "4 Three 3 "
1782
+ ],
1783
+ "text/html": [
1784
+ "\n",
1785
+ " <div id=\"df-22cea301-509b-428f-8bfd-dab5a7395c01\" class=\"colab-df-container\">\n",
1786
+ " <div>\n",
1787
+ "<style scoped>\n",
1788
+ " .dataframe tbody tr th:only-of-type {\n",
1789
+ " vertical-align: middle;\n",
1790
+ " }\n",
1791
+ "\n",
1792
+ " .dataframe tbody tr th {\n",
1793
+ " vertical-align: top;\n",
1794
+ " }\n",
1795
+ "\n",
1796
+ " .dataframe thead th {\n",
1797
+ " text-align: right;\n",
1798
+ " }\n",
1799
+ "</style>\n",
1800
+ "<table border=\"1\" class=\"dataframe\">\n",
1801
+ " <thead>\n",
1802
+ " <tr style=\"text-align: right;\">\n",
1803
+ " <th></th>\n",
1804
+ " <th>title</th>\n",
1805
+ " <th>sentiment_label</th>\n",
1806
+ " <th>review_text</th>\n",
1807
+ " <th>rating</th>\n",
1808
+ " <th>popularity_score</th>\n",
1809
+ " </tr>\n",
1810
+ " </thead>\n",
1811
+ " <tbody>\n",
1812
+ " <tr>\n",
1813
+ " <th>0</th>\n",
1814
+ " <td>A Light in the Attic</td>\n",
1815
+ " <td>neutral</td>\n",
1816
+ " <td>A passable and light read.</td>\n",
1817
+ " <td>Three</td>\n",
1818
+ " <td>3</td>\n",
1819
+ " </tr>\n",
1820
+ " <tr>\n",
1821
+ " <th>1</th>\n",
1822
+ " <td>A Light in the Attic</td>\n",
1823
+ " <td>neutral</td>\n",
1824
+ " <td>An average reading experience.</td>\n",
1825
+ " <td>Three</td>\n",
1826
+ " <td>3</td>\n",
1827
+ " </tr>\n",
1828
+ " <tr>\n",
1829
+ " <th>2</th>\n",
1830
+ " <td>A Light in the Attic</td>\n",
1831
+ " <td>neutral</td>\n",
1832
+ " <td>It was somewhat engaging.</td>\n",
1833
+ " <td>Three</td>\n",
1834
+ " <td>3</td>\n",
1835
+ " </tr>\n",
1836
+ " <tr>\n",
1837
+ " <th>3</th>\n",
1838
+ " <td>A Light in the Attic</td>\n",
1839
+ " <td>neutral</td>\n",
1840
+ " <td>It was acceptable but not standout.</td>\n",
1841
+ " <td>Three</td>\n",
1842
+ " <td>3</td>\n",
1843
+ " </tr>\n",
1844
+ " <tr>\n",
1845
+ " <th>4</th>\n",
1846
+ " <td>A Light in the Attic</td>\n",
1847
+ " <td>neutral</td>\n",
1848
+ " <td>A serviceable narrative.</td>\n",
1849
+ " <td>Three</td>\n",
1850
+ " <td>3</td>\n",
1851
+ " </tr>\n",
1852
+ " </tbody>\n",
1853
+ "</table>\n",
1854
+ "</div>\n",
1855
+ " <div class=\"colab-df-buttons\">\n",
1856
+ "\n",
1857
+ " <div class=\"colab-df-container\">\n",
1858
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-22cea301-509b-428f-8bfd-dab5a7395c01')\"\n",
1859
+ " title=\"Convert this dataframe to an interactive table.\"\n",
1860
+ " style=\"display:none;\">\n",
1861
+ "\n",
1862
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
1863
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
1864
+ " </svg>\n",
1865
+ " </button>\n",
1866
+ "\n",
1867
+ " <style>\n",
1868
+ " .colab-df-container {\n",
1869
+ " display:flex;\n",
1870
+ " gap: 12px;\n",
1871
+ " }\n",
1872
+ "\n",
1873
+ " .colab-df-convert {\n",
1874
+ " background-color: #E8F0FE;\n",
1875
+ " border: none;\n",
1876
+ " border-radius: 50%;\n",
1877
+ " cursor: pointer;\n",
1878
+ " display: none;\n",
1879
+ " fill: #1967D2;\n",
1880
+ " height: 32px;\n",
1881
+ " padding: 0 0 0 0;\n",
1882
+ " width: 32px;\n",
1883
+ " }\n",
1884
+ "\n",
1885
+ " .colab-df-convert:hover {\n",
1886
+ " background-color: #E2EBFA;\n",
1887
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1888
+ " fill: #174EA6;\n",
1889
+ " }\n",
1890
+ "\n",
1891
+ " .colab-df-buttons div {\n",
1892
+ " margin-bottom: 4px;\n",
1893
+ " }\n",
1894
+ "\n",
1895
+ " [theme=dark] .colab-df-convert {\n",
1896
+ " background-color: #3B4455;\n",
1897
+ " fill: #D2E3FC;\n",
1898
+ " }\n",
1899
+ "\n",
1900
+ " [theme=dark] .colab-df-convert:hover {\n",
1901
+ " background-color: #434B5C;\n",
1902
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1903
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1904
+ " fill: #FFFFFF;\n",
1905
+ " }\n",
1906
+ " </style>\n",
1907
+ "\n",
1908
+ " <script>\n",
1909
+ " const buttonEl =\n",
1910
+ " document.querySelector('#df-22cea301-509b-428f-8bfd-dab5a7395c01 button.colab-df-convert');\n",
1911
+ " buttonEl.style.display =\n",
1912
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1913
+ "\n",
1914
+ " async function convertToInteractive(key) {\n",
1915
+ " const element = document.querySelector('#df-22cea301-509b-428f-8bfd-dab5a7395c01');\n",
1916
+ " const dataTable =\n",
1917
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1918
+ " [key], {});\n",
1919
+ " if (!dataTable) return;\n",
1920
+ "\n",
1921
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1922
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1923
+ " + ' to learn more about interactive tables.';\n",
1924
+ " element.innerHTML = '';\n",
1925
+ " dataTable['output_type'] = 'display_data';\n",
1926
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1927
+ " const docLink = document.createElement('div');\n",
1928
+ " docLink.innerHTML = docLinkHtml;\n",
1929
+ " element.appendChild(docLink);\n",
1930
+ " }\n",
1931
+ " </script>\n",
1932
+ " </div>\n",
1933
+ "\n",
1934
+ "\n",
1935
+ " </div>\n",
1936
+ " </div>\n"
1937
+ ],
1938
+ "application/vnd.google.colaboratory.intrinsic+json": {
1939
+ "type": "dataframe",
1940
+ "variable_name": "df_reviews",
1941
+ "summary": "{\n \"name\": \"df_reviews\",\n \"rows\": 10000,\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\": 150,\n \"samples\": [\n \"An entertaining and powerful book.\",\n \"It felt repetitive and dull.\",\n \"One of the best books I\\u2019ve read this year.\"\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}"
1942
+ }
1943
+ },
1944
+ "metadata": {},
1945
+ "execution_count": 58
1946
+ }
1947
+ ],
1948
+ "source": [
1949
+ "import random\n",
1950
+ "import pandas as pd\n",
1951
+ "\n",
1952
+ "# Recreate review_rows\n",
1953
+ "review_rows = []\n",
1954
+ "\n",
1955
+ "for _, row in df_books.iterrows():\n",
1956
+ " title = row[\"title\"]\n",
1957
+ " sentiment_label = row[\"sentiment_label\"]\n",
1958
+ " review_pool = synthetic_reviews_by_sentiment[sentiment_label]\n",
1959
+ "\n",
1960
+ " sampled_reviews = random.sample(review_pool, 10)\n",
1961
+ "\n",
1962
+ " for review_text in sampled_reviews:\n",
1963
+ " review_rows.append({\n",
1964
+ " \"title\": title,\n",
1965
+ " \"sentiment_label\": sentiment_label,\n",
1966
+ " \"review_text\": review_text,\n",
1967
+ " \"rating\": row[\"rating\"],\n",
1968
+ " \"popularity_score\": row[\"popularity_score\"]\n",
1969
+ " })\n",
1970
+ "\n",
1971
+ "# Create dataframe\n",
1972
+ "df_reviews = pd.DataFrame(review_rows)\n",
1973
+ "\n",
1974
+ "# View first rows\n",
1975
+ "df_reviews.head()"
1976
+ ]
1977
+ }
1978
+ ],
1979
+ "metadata": {
1980
+ "colab": {
1981
+ "collapsed_sections": [
1982
+ "jpASMyIQMaAq",
1983
+ "lquNYCbfL9IM",
1984
+ "0IWuNpxxYDJF",
1985
+ "oCdTsin2Yfp3",
1986
+ "T0TOeRC4Yrnn",
1987
+ "duI5dv3CZYvF",
1988
+ "qMjRKMBQZlJi",
1989
+ "p-1Pr2szaqLk",
1990
+ "SIaJUGIpaH4V",
1991
+ "pY4yCoIuaQqp",
1992
+ "n4-TaNTFgPak",
1993
+ "HnngRNTgacYt",
1994
+ "HF9F9HIzgT7Z",
1995
+ "T8AdKkmASq9a",
1996
+ "OhXbdGD5fH0c",
1997
+ "L2ak1HlcgoTe",
1998
+ "4IXZKcCSgxnq",
1999
+ "EhIjz9WohAmZ",
2000
+ "Gi4y9M9KuDWx",
2001
+ "fQhfVaDmuULT",
2002
+ "bmJMXF-Bukdm",
2003
+ "RYvGyVfXuo54"
2004
+ ],
2005
+ "provenance": []
2006
+ },
2007
+ "kernelspec": {
2008
+ "display_name": "Python 3",
2009
+ "name": "python3"
2010
+ },
2011
+ "language_info": {
2012
+ "name": "python"
2013
+ }
2014
+ },
2015
+ "nbformat": 4,
2016
+ "nbformat_minor": 0
2017
+ }
2a_Python_Analysis (1).ipynb ADDED
The diff for this file is too large to render. See raw diff