2022-07-11 22:39:17 +00:00
{
"cells": [
{
2023-01-10 18:37:25 +00:00
"attachments": {},
2022-07-11 22:39:17 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
2023-01-10 18:37:25 +00:00
"## Classification using embeddings\n",
2022-07-11 22:39:17 +00:00
"\n",
2023-01-10 18:37:25 +00:00
"There are many ways to classify text. This notebook shares an example of text classification using embeddings. For many text classification tasks, we've seen fine-tuned models do better than embeddings. See an example of fine-tuned models for classification in [Fine-tuned_classification.ipynb](Fine-tuned_classification.ipynb). We also recommend having more examples than embedding dimensions, which we don't quite achieve here.\n",
2022-07-11 22:39:17 +00:00
"\n",
2023-09-20 21:50:59 +00:00
"In this text classification task, we predict the score of a food review (1 to 5) based on the embedding of the review's text. We split the dataset into a training and a testing set for all the following tasks, so we can realistically evaluate performance on unseen data. The dataset is created in the [Get_embeddings_from_dataset Notebook](Get_embeddings_from_dataset.ipynb).\n"
2022-07-11 22:39:17 +00:00
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
2023-01-10 18:37:25 +00:00
" 1 0.88 0.35 0.50 20\n",
2022-12-13 23:28:39 +00:00
" 2 1.00 0.38 0.55 8\n",
" 3 1.00 0.18 0.31 11\n",
" 4 1.00 0.26 0.41 27\n",
2023-01-10 18:37:25 +00:00
" 5 0.74 1.00 0.85 134\n",
2022-07-11 22:39:17 +00:00
"\n",
2023-01-10 18:37:25 +00:00
" accuracy 0.77 200\n",
" macro avg 0.92 0.43 0.52 200\n",
"weighted avg 0.82 0.77 0.72 200\n",
2022-07-11 22:39:17 +00:00
"\n"
]
}
],
"source": [
2023-01-10 18:37:25 +00:00
"# imports\n",
2022-07-11 22:39:17 +00:00
"import pandas as pd\n",
"import numpy as np\n",
2023-07-17 23:40:54 +00:00
"from ast import literal_eval\n",
2022-07-11 22:39:17 +00:00
"\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import classification_report, accuracy_score\n",
"\n",
2023-01-10 18:37:25 +00:00
"# 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-11 22:39:17 +00:00
"df = pd.read_csv(datafile_path)\n",
2023-07-17 23:40:54 +00:00
"df[\"embedding\"] = df.embedding.apply(literal_eval).apply(np.array) # convert string to array\n",
2022-07-11 22:39:17 +00:00
"\n",
2023-01-10 18:37:25 +00:00
"# split data into train and test\n",
2022-07-11 22:39:17 +00:00
"X_train, X_test, y_train, y_test = train_test_split(\n",
2023-01-10 18:37:25 +00:00
" list(df.embedding.values), df.Score, test_size=0.2, random_state=42\n",
2022-07-11 22:39:17 +00:00
")\n",
"\n",
2023-01-10 18:37:25 +00:00
"# train random forest classifier\n",
2022-07-11 22:39:17 +00:00
"clf = RandomForestClassifier(n_estimators=100)\n",
"clf.fit(X_train, y_train)\n",
"preds = clf.predict(X_test)\n",
"probas = clf.predict_proba(X_test)\n",
"\n",
"report = classification_report(y_test, preds)\n",
"print(report)\n"
]
},
{
2023-07-17 23:40:54 +00:00
"attachments": {},
2022-07-11 22:39:17 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the model has learnt to distinguish between the categories decently. 5-star reviews show the best performance overall, and this is not too surprising, since they are the most common in the dataset."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-12-13 23:28:39 +00:00
"RandomForestClassifier() - Average precision score over all classes: 0.87\n"
2022-07-11 22:39:17 +00:00
]
},
{
"data": {
2023-01-10 18:37:25 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjEAAAIDCAYAAAD13U9SAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXM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LssWQmQ22qCU/5NSnoB+7Engz3Ws1uJjU0p5ujwSXP58HctrnGOt+F2/QvdGTI8eLzNj5jZm41HsAwbXs6wa3bsao2cd6xx9XCwEzo9eO6aje1Bj29l11O9Ml1Ke0UQ7a/xPOOLpTXywOdqWROo9xptwnWtoHzWHpraTzGuBAiViOjv/AE4WQoxH71+W6PEPCCGuJhp42RhSyj3AWuAuIYRZCHECcFbCKi8C84QQc4UQJvSLsh9os1wjUsp90fb+JISwRgP5rgHqzFUj9aG9HwB/FUJkCD2YcLAQYhaAEOICcSR4shx930Si84fR++tjPIt+0XpZCDEi2laO0POR1HXhTkePJ3ELIUagxxcR3e5UIcT06H6qRh+OHInu10uFEE4pZTD6/UgdbTfGf4Dro9sQQgiHEGLeUeLvaN4BZh312Sz07gN/C2xojOeAs4QQpwohDNH/52whRN/o/+1ddFGZJYQwCSFmHt2A0HPlnCj0wGMfujevrv3V3sfmPzhyjrX0d6VH7a8QQmQDd7TQlueBk4QQFwo9eDpHCDEhumwD8L2o12cI+rnTIFLK9UAJ8F/gfSllRXTRasAl9MBqW/S3jhFCTG2inQuBXwh9sEAaunfiBdn00UsNHeONXef+C9wshJgc/e6QqPBpLk1tJ5nXAgVKxHRqou78Z4DbpZSbgL+iB+geBsY
2022-07-11 22:39:17 +00:00
"text/plain": [
2023-01-10 18:37:25 +00:00
"<Figure size 648x720 with 1 Axes>"
2022-07-11 22:39:17 +00:00
]
},
2023-01-10 18:37:25 +00:00
"metadata": {
"needs_background": "light"
},
2022-07-11 22:39:17 +00:00
"output_type": "display_data"
}
],
"source": [
"from openai.embeddings_utils import plot_multiclass_precision_recall\n",
"\n",
"plot_multiclass_precision_recall(probas, y_test, [1, 2, 3, 4, 5], clf)\n"
]
},
{
2023-07-17 23:40:54 +00:00
"attachments": {},
2022-07-11 22:39:17 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"Unsurprisingly 5-star and 1-star reviews seem to be easier to predict. Perhaps with more data, the nuances between 2-4 stars could be better predicted, but there's also probably more subjectivity in how people use the inbetween scores."
]
}
],
"metadata": {
"kernelspec": {
2023-01-10 18:37:25 +00:00
"display_name": "openai",
2022-07-11 22:39:17 +00:00
"language": "python",
2023-01-10 18:37:25 +00:00
"name": "python3"
2022-07-11 22:39:17 +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-07-11 22:39:17 +00:00
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
2023-01-10 18:37:25 +00:00
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
2022-07-11 22:39:17 +00:00
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}