2022-03-11 02:08:53 +00:00
{
"cells": [
{
2023-01-10 18:37:25 +00:00
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"## Zero-shot classification with embeddings\n",
"\n",
"In this notebook we will classify the sentiment of reviews using embeddings and zero labeled data! The dataset is created in the [Obtain_dataset Notebook](Obtain_dataset.ipynb).\n",
"\n",
2023-01-10 18:37:25 +00:00
"We'll define positive sentiment to be 4- and 5-star reviews, and negative sentiment to be 1- and 2-star reviews. 3-star reviews are considered neutral and we won't use them for this example.\n",
2022-03-11 02:08:53 +00:00
"\n",
"We will perform zero-shot classification by embedding descriptions of each class and then comparing new samples to those class embeddings."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
2023-01-10 18:37:25 +00:00
"# imports\n",
2022-03-11 02:08:53 +00:00
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"from sklearn.metrics import classification_report\n",
"\n",
2023-01-10 18:37:25 +00:00
"# parameters\n",
"EMBEDDING_MODEL = \"text-embedding-ada-002\"\n",
"\n",
"# load data\n",
"datafile_path = \"data/fine_food_reviews_with_embeddings_1k.csv\"\n",
2022-12-13 23:28:39 +00:00
"\n",
2022-07-12 00:18:08 +00:00
"df = pd.read_csv(datafile_path)\n",
2023-01-10 18:37:25 +00:00
"df[\"embedding\"] = df.embedding.apply(eval).apply(np.array)\n",
2022-03-11 02:08:53 +00:00
"\n",
2023-01-10 18:37:25 +00:00
"# convert 5-star rating to binary sentiment\n",
"df = df[df.Score != 3]\n",
"df[\"sentiment\"] = df.Score.replace({1: \"negative\", 2: \"negative\", 4: \"positive\", 5: \"positive\"})\n"
2022-03-11 02:08:53 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Zero-Shot Classification\n",
"To perform zero shot classification, we want to predict labels for our samples without any training. To do this, we can simply embed short descriptions of each label, such as positive and negative, and then compare the cosine distance between embeddings of samples and label descriptions. \n",
"\n",
"The highest similarity label to the sample input is the predicted label. We can also define a prediction score to be the difference between the cosine distance to the positive and to the negative label. This score can be used for plotting a precision-recall curve, which can be used to select a different tradeoff between precision and recall, by selecting a different threshold."
]
},
{
"cell_type": "code",
2022-07-12 00:18:08 +00:00
"execution_count": 2,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
2023-01-10 18:37:25 +00:00
" negative 0.61 0.88 0.72 136\n",
2022-12-13 23:28:39 +00:00
" positive 0.98 0.90 0.94 789\n",
2022-03-11 02:08:53 +00:00
"\n",
2022-12-13 23:28:39 +00:00
" accuracy 0.90 925\n",
2023-01-10 18:37:25 +00:00
" macro avg 0.79 0.89 0.83 925\n",
2022-12-13 23:28:39 +00:00
"weighted avg 0.92 0.90 0.91 925\n",
2022-03-11 02:08:53 +00:00
"\n"
]
},
{
"data": {
2023-01-10 18:37:25 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEWCAYAAAB8LwAVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAA5S0lEQVR4nO3dd3xW5f3/8dc7A8IGAREZgogioKBGXFWQKg7EXUddWBy0dfy0ddW60FZarf1qtSoWKo6CSq1SRREVXLWVIRsExAgBlT0DIePz++OcxJt4JzkhuXNnfJ6PRx65z3XW57oD9+c+13XOdcnMcM4550pKSXYAzjnnaiZPEM455+LyBOGccy4uTxDOOefi8gThnHMuLk8Qzjnn4vIE4aqUpKGSPk52HFVJ0iWS3omw3VOS7qqOmKqDpCxJJ4Wv75X0QrJjctXLE4RDUkNJoyV9LWmrpNmSTkt2XFGEH2I7JG2T9J2kZyU1rcpzmNmLZjYownbDzez+qjx3EUkmaXtYz1WSHpGUmohzOVfEE4QDSANWAv2BFsBvgZcldUlmUBUwxMyaAocDmQT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Oudqh5xd+UzP2oiFA1Vt2L6LLTvyyCswvlq/nVSJL77bSqP0VLbl5nNyz3YM798tyVE7TxDOuQrbsauAtVtz2Zizi6VrtpFXUMiKDTl8t3knkli2dhvrtuaSniqy1udEOmaHlo3YkVcQjExbUOgJogaIlCDCkVX3BXYAWWZWmNConHNJkVcQ/Nd+ffYqlq3ZxvJ12yksNFZv2sG23Hy+Wredwgij1XZo2YimDdNomJ7CuYd3oGFaKp32asTR+7cGoLDQaNk4nZaNG9CyUTppqd+
2022-03-11 02:08:53 +00:00
"text/plain": [
2023-01-10 18:37:25 +00:00
"<Figure size 432x288 with 1 Axes>"
2022-03-11 02:08:53 +00:00
]
},
2023-01-10 18:37:25 +00:00
"metadata": {
"needs_background": "light"
},
2022-03-11 02:08:53 +00:00
"output_type": "display_data"
}
],
"source": [
"from openai.embeddings_utils import cosine_similarity, get_embedding\n",
"from sklearn.metrics import PrecisionRecallDisplay\n",
"\n",
2022-07-12 00:18:08 +00:00
"def evaluate_embeddings_approach(\n",
2022-03-11 02:08:53 +00:00
" labels = ['negative', 'positive'], \n",
2023-01-10 18:37:25 +00:00
" model = EMBEDDING_MODEL,\n",
2022-03-11 02:08:53 +00:00
"):\n",
2023-01-10 18:37:25 +00:00
" label_embeddings = [get_embedding(label, engine=model) for label in labels]\n",
2022-03-11 02:08:53 +00:00
"\n",
" def label_score(review_embedding, label_embeddings):\n",
" return cosine_similarity(review_embedding, label_embeddings[1]) - cosine_similarity(review_embedding, label_embeddings[0])\n",
"\n",
2023-01-10 18:37:25 +00:00
" probas = df[\"embedding\"].apply(lambda x: label_score(x, label_embeddings))\n",
2022-03-11 02:08:53 +00:00
" preds = probas.apply(lambda x: 'positive' if x>0 else 'negative')\n",
"\n",
" report = classification_report(df.sentiment, preds)\n",
" print(report)\n",
"\n",
" display = PrecisionRecallDisplay.from_predictions(df.sentiment, probas, pos_label='positive')\n",
" _ = display.ax_.set_title(\"2-class Precision-Recall curve\")\n",
"\n",
2023-01-10 18:37:25 +00:00
"evaluate_embeddings_approach(labels=['negative', 'positive'], model=EMBEDDING_MODEL)"
2022-03-11 02:08:53 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that this classifier already performs extremely well. We used similarity embeddings, and the simplest possible label name. Let's try to improve on this by using more descriptive label names, and search embeddings."
]
},
{
"cell_type": "code",
2022-07-12 00:18:08 +00:00
"execution_count": 3,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
2022-12-13 23:28:39 +00:00
" negative 0.98 0.73 0.84 136\n",
" positive 0.96 1.00 0.98 789\n",
2022-03-11 02:08:53 +00:00
"\n",
2022-12-13 23:28:39 +00:00
" accuracy 0.96 925\n",
" macro avg 0.97 0.86 0.91 925\n",
"weighted avg 0.96 0.96 0.96 925\n",
2022-03-11 02:08:53 +00:00
"\n"
]
},
{
"data": {
2023-01-10 18:37:25 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEWCAYAAAB8LwAVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAzlUlEQVR4nO3deZyWVf3/8debRVEENUBEEEHUFFxIEbdMcldc03Lfcq3UfvqtvvqtXCjT0upRaW5J4hKGZIaKEiq4pcUiqxtoqIOoKJuKIMx8fn9cZ/BmvGfmGmbumWF4Px+Pecx9nXMtn3MP3Oc+51zXOYoIzMzMqmrV1AGYmVnz5ArCzMyKcgVhZmZFuYIwM7OiXEGYmVlRriDMzKwoVxDWoCSdKenZpo6jIUk6RdI/c+x3i6SfNkZMjUHSHEkHptdXSbqnqWOyxuUKwpC0vqQ7JL0p6SNJUyQd1tRx5ZE+xD6V9LGk9yTdKWmjhrxGRNwbEQfn2O+CiPhZQ167kqSQ9Ekq51xJv5HUuhTXMqv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4RHNSva6M/P+EKwsysmRn70nsAq90We+Wyvpy1T+9GjcMVhJlZM/PwxV9l7Mz3+PLmHQA44g/PsqK8otHjcAVhZtbMdGzXluN26wHAJ8tXNlkcHqQ2M7OiXEGYmVlRubqY0syqWwCfAnMiovE7w8zMrFFVW0FI2phsMr2TgPWA+UA7oKukF4A/RsS4RonSzMwaXU0tiJHAXcC+EbGoMEPSbsBpkraOiDtKGJ+ZmTWRaiuIiDiohrxJwKSSRGRmZs1CrYPUypwq6Yq03VPSwNKHZmZmlRYuXdHo18xzF9Mfgb3IxiIAPgJuKllEZma2SuV
2022-03-11 02:08:53 +00:00
"text/plain": [
2023-01-10 18:37:25 +00:00
"<Figure size 432x288 with 1 Axes>"
2022-03-11 02:08:53 +00:00
]
},
2023-01-10 18:37:25 +00:00
"metadata": {
"needs_background": "light"
},
2022-03-11 02:08:53 +00:00
"output_type": "display_data"
}
],
"source": [
2023-01-10 18:37:25 +00:00
"evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'])"
2022-03-11 02:08:53 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the search embeddings and descriptive names leads to an additional improvement in performance."
]
},
{
"cell_type": "code",
2022-07-12 00:18:08 +00:00
"execution_count": 4,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
2022-12-13 23:28:39 +00:00
" negative 0.98 0.73 0.84 136\n",
" positive 0.96 1.00 0.98 789\n",
2022-03-11 02:08:53 +00:00
"\n",
2022-12-13 23:28:39 +00:00
" accuracy 0.96 925\n",
" macro avg 0.97 0.86 0.91 925\n",
"weighted avg 0.96 0.96 0.96 925\n",
2022-03-11 02:08:53 +00:00
"\n"
]
},
{
"data": {
2023-01-10 18:37:25 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEWCAYAAAB8LwAVAAAAOXRFWHRTb2Z0d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WL5sX6qGqkYAWEpCEkNYE2kkpInkxqDhAR95JMTH4Yyfy5i0nmtCUi5kn6DTAuPdWA8g5rM7N8mjdtQtc2GxTk3M9P/5SmTZKC49///ZKOm6xPE0GzJk1o0kQ0ayKaNBEbr9+cOQuX0GmT9WnaRMmPkm1NJT7/aikTP1rA7PlLaNV+LS8gIuLkarYHcFEl2wYCAwsRl5lZFrNuqtu+yeemzeHCRybW6TkLbY3upDYzW1NE+hjNmHc/Z+5Xy1heWsby0mB5aRlzFi5h/XWasaK0jBVlwfLSoGubDThk++I+leUCwsysHsz6cjEAv3/u3Uz7r9usCe/+9tBChlQtFxBmZvXggh9vSfOmYrv2rSgtCzZarznNmzZhnWZJp/mG6zajWdMmNGsq7hw9k4Gv/rfYIbuAMDOrD02aiPP22TLTvs2bqvqd6oFHczUzs7xcgzAza2CWLk86q5+bNoely8soLQsO7NGu3t+fcAFhZtbAjHrrM4BVHou9dml3zt6ra73G4QLCzKyBefrSvRk1/TN+uFlLAA7/06ssLy2r9zhcQJiZNTCtWjTnuF06AvDNshVFi8Od1GZmlpcLCDMzyytTE1M6surmwBJgVkTUf2OYmZnVq0oLCEkbkQymdzKwDjAXaAG0k/QGcHdEjKmXKM3MrN5VVYMYBgwG9omIBbkbJO0CnC5py4h4sIDxmZlZkVRaQETEgVVsmwBMKEhEZmbWIFTbSa3EaZKuSdc7S+pd+NDMzKyYsjzFdDewB0lfBMBXwF0Fi8jMzBqELAXEbhFxEbA
2022-03-11 02:08:53 +00:00
"text/plain": [
2023-01-10 18:37:25 +00:00
"<Figure size 432x288 with 1 Axes>"
2022-03-11 02:08:53 +00:00
]
},
2023-01-10 18:37:25 +00:00
"metadata": {
"needs_background": "light"
},
2022-03-11 02:08:53 +00:00
"output_type": "display_data"
}
],
"source": [
2023-01-10 18:37:25 +00:00
"evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'])"
2022-03-11 02:08:53 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As shown above, zero-shot classification with embeddings can lead to great results, especially when the labels are more descriptive than just simple words."
]
}
],
"metadata": {
"kernelspec": {
2023-01-10 18:37:25 +00:00
"display_name": "openai",
2022-07-12 00:18:08 +00:00
"language": "python",
2023-01-10 18:37:25 +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",
2023-01-10 18:37:25 +00:00
"version": "3.9.9"
2022-03-11 02:08:53 +00:00
},
2022-07-12 00:18:08 +00:00
"orig_nbformat": 4,
"vscode": {
"interpreter": {
2023-01-10 18:37:25 +00:00
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
2022-07-12 00:18:08 +00:00
}
}
2022-03-11 02:08:53 +00:00
},
"nbformat": 4,
"nbformat_minor": 2
}