2022-03-11 02:08:53 +00:00
{
"cells": [
{
2023-01-10 23:58:38 +00:00
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualizing the embeddings in 2D\n",
"\n",
2023-09-20 21:50:59 +00:00
"We will use t-SNE to reduce the dimensionality of the embeddings from 1536 to 2. Once the embeddings are reduced to two dimensions, we can plot them in a 2D scatter plot. The dataset is created in the [Get_embeddings_from_dataset Notebook](Get_embeddings_from_dataset.ipynb)."
2022-03-11 02:08:53 +00:00
]
},
{
2023-07-17 23:40:54 +00:00
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Reduce dimensionality\n",
"\n",
"We reduce the dimensionality to 2 dimensions using t-SNE decomposition."
]
},
{
"cell_type": "code",
2022-07-12 00:12:44 +00:00
"execution_count": 1,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1000, 2)"
]
},
2022-07-12 00:12:44 +00:00
"execution_count": 1,
2022-03-11 02:08:53 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"from sklearn.manifold import TSNE\n",
2022-12-13 23:28:39 +00:00
"import numpy as np\n",
2023-07-17 23:40:54 +00:00
"from ast import literal_eval\n",
2022-03-11 02:08:53 +00:00
"\n",
"# Load the embeddings\n",
2023-01-10 23:58:38 +00:00
"datafile_path = \"data/fine_food_reviews_with_embeddings_1k.csv\"\n",
2022-07-12 00:12:44 +00:00
"df = pd.read_csv(datafile_path)\n",
2022-03-11 02:08:53 +00:00
"\n",
"# Convert to a list of lists of floats\n",
2023-07-17 23:40:54 +00:00
"matrix = np.array(df.embedding.apply(literal_eval).to_list())\n",
2022-03-11 02:08:53 +00:00
"\n",
"# Create a t-SNE model and transform the data\n",
"tsne = TSNE(n_components=2, perplexity=15, random_state=42, init='random', learning_rate=200)\n",
"vis_dims = tsne.fit_transform(matrix)\n",
"vis_dims.shape"
]
},
{
2023-07-17 23:40:54 +00:00
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Plotting the embeddings\n",
"\n",
"We colour each review by its star rating, ranging from red to green."
]
},
{
2023-01-10 23:58:38 +00:00
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
2023-01-10 23:58:38 +00:00
"We can observe a decent data separation even in the reduced 2 dimensions."
2022-03-11 02:08:53 +00:00
]
},
{
"cell_type": "code",
2022-07-12 00:12:44 +00:00
"execution_count": 2,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'Amazon ratings visualized in language using t-SNE')"
]
},
2022-07-12 00:12:44 +00:00
"execution_count": 2,
2022-03-11 02:08:53 +00:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2024-01-25 17:59:05 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAioAAAGzCAYAAAABsTylAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOy9d5hd1XWw/552e5+50/uod1QQsgDRMcUYDAb82Q4QO3ZCYkPiktifPxvsJKTYjhN3xw5u+IdjjBs2VSBRBep9Rhpper+9l1N+f1zpotGMpBFISILz6rnPozlln7X3aeusvYpgGIaBiYmJiYmJiclZiHimBTAxMTExMTExORamomJiYmJiYmJy1mIqKiYmJiYmJiZnLaaiYmJiYmJiYnLWYioqJiYmJiYmJmctpqJiYmJiYmJictZiKiomJiYmJiYmZy2momJiYmJiYmJy1mIqKiYmJiYmJiZnLaaiYvK25b777kMQhDMtxptGEATuu+++My3GJDl+/OMfIwgCPT09b6kcLS0t3HnnnSfc7lSP25133klLS8spa8/k1HC23B8mpw9TUXmL+M53voMgCKxcufJMi/K2Ip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3g2Yk79nGYeeughqqury2Z+QRC47bbbePjhhydMyRzm/e9//wTHp8NRQh/60Icm+FusXLmSQqHA4OBgeZndbi//P5lMEgqFuOiii8hkMnR0dEwp3913383OnTv59a9/Xf6
2022-03-11 02:08:53 +00:00
"text/plain": [
2024-01-25 17:59:05 +00:00
"<Figure size 640x480 with 1 Axes>"
2022-03-11 02:08:53 +00:00
]
},
2024-01-25 17:59:05 +00:00
"metadata": {},
2022-03-11 02:08:53 +00:00
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import matplotlib\n",
"import numpy as np\n",
"\n",
"colors = [\"red\", \"darkorange\", \"gold\", \"turquoise\", \"darkgreen\"]\n",
"x = [x for x,y in vis_dims]\n",
"y = [y for x,y in vis_dims]\n",
"color_indices = df.Score.values - 1\n",
"\n",
"colormap = matplotlib.colors.ListedColormap(colors)\n",
"plt.scatter(x, y, c=color_indices, cmap=colormap, alpha=0.3)\n",
"for score in [0,1,2,3,4]:\n",
" avg_x = np.array(x)[df.Score-1==score].mean()\n",
" avg_y = np.array(y)[df.Score-1==score].mean()\n",
" color = colors[score]\n",
" plt.scatter(avg_x, avg_y, marker='x', color=color, s=100)\n",
2022-12-13 23:28:39 +00:00
"\n",
2022-03-11 02:08:53 +00:00
"plt.title(\"Amazon ratings visualized in language using t-SNE\")"
]
}
],
"metadata": {
"kernelspec": {
2023-01-10 23:58:38 +00:00
"display_name": "openai",
2022-07-12 00:12:44 +00:00
"language": "python",
2023-01-10 23:58:38 +00:00
"name": "python3"
2022-03-11 02:08:53 +00:00
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2024-01-25 17:59:05 +00:00
"version": "3.11.5"
2022-03-11 02:08:53 +00:00
},
2022-07-12 00:12:44 +00:00
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
}
}
2022-03-11 02:08:53 +00:00
},
"nbformat": 4,
"nbformat_minor": 2
}