mirror of
https://github.com/openai/openai-cookbook
synced 2024-11-08 01:10:29 +00:00
143 lines
97 KiB
Plaintext
143 lines
97 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Visualizing the embeddings in 2D\n",
|
||
|
"\n",
|
||
|
"We will use t-SNE to reduce the dimensionality of the embeddings from 2048 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 [Obtain_dataset Notebook](Obtain_dataset.ipynb)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 1. Reduce dimensionality\n",
|
||
|
"\n",
|
||
|
"We reduce the dimensionality to 2 dimensions using t-SNE decomposition."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 19,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"(1000, 2)"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 19,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import pandas as pd\n",
|
||
|
"from sklearn.manifold import TSNE\n",
|
||
|
"\n",
|
||
|
"# Load the embeddings\n",
|
||
|
"df = pd.read_csv('output/embedded_1k_reviews.csv')\n",
|
||
|
"\n",
|
||
|
"# Convert to a list of lists of floats\n",
|
||
|
"matrix = df.babbage_similarity.apply(eval).to_list()\n",
|
||
|
"\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"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 2. Plotting the embeddings\n",
|
||
|
"\n",
|
||
|
"We colour each review by its star rating, ranging from red to green."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"We can observe a decent data separation even in the reduced 2 dimensions. There seems to be a cluster of mostly negative reviews."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"Text(0.5, 1.0, 'Amazon ratings visualized in language using t-SNE')"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 20,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAEICAYAAAC6fYRZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOy9Z5gc13Wg/d4KncOE7skZYRBJgABBMFMUKVKkSGXKEiVRlrSSbK/X9rdeede7Xq/DrrRrr5LD2pZkW5YVrCyKSsxiJnIehAFmMDlP51BdVff7cRuDGWAAIjGB/T5PPzPdVXXrVjp17rknCCklFSpUqFDh8kR7tTtQoUKFChVePipCvkKFChUuYypCvkKFChUuYypCvkKFChUuYypCvkKFChUuYypCvkKFChUuYypC/jJGCNEmhMgIIfRXuy/wyvRHCHGLEGJo3vf9QohbLvE+/lkI8ednWHa/EOLhC2z3fwgh/vXienf5czHn+I3IZSfkhRBPCiFmhRDeV7svrzRCiH4hxG0nvkspB6SUISml82r26wSvRn+klKullE++gvv7hpTyLa/U/t6IvJzn+NRn6AzrrBZCPCyEmBFCJIQQ24UQd5WX3S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08Yz84y/1xnnwZNaewB9iJGpXYKKVwMV7qGbJQo6pHUROr+1DH/pHFGpNqru7/oCai53OtON1P/urzObBzRSw0NVaooBBChFAa+zIpZd+r3J0KF4EQQkPZ5O+XUj5xidteiRJ03vIk72ua8sjk76SU7S+58mXC5aLJV7gECCHuEUIEykPkvwT2cnLCr8LrCCHEHUKIqrKN/Q9Rprd
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"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",
|
||
|
"plt.title(\"Amazon ratings visualized in language using t-SNE\")"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"interpreter": {
|
||
|
"hash": "be4b5d5b73a21c599de40d6deb1129796d12dc1cc33a738f7bac13269cfcafe8"
|
||
|
},
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3.7.3 64-bit ('base': conda)",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.7.3"
|
||
|
},
|
||
|
"orig_nbformat": 4
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|