openai-cookbook/examples/User_and_product_embeddings.ipynb

175 lines
15 KiB
Plaintext
Raw Normal View History

2022-03-11 02:08:53 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## User and product embeddings\n",
"\n",
"We calculate user and product embeddings based on the training set, and evaluate the results on the unseen test set. We will evaluate the results by plotting the user and product similarity versus the review score. The dataset is created in the [Obtain_dataset Notebook](Obtain_dataset.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Calculate user and product embeddings\n",
"\n",
"We calculate these embeddings simply by averaging all the reviews about the same product or written by the same user within the training set."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(24502, 19035)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"df = pd.read_csv('output/embedded_babbage_similarity_50k.csv', index_col=0) # note that you will need to generate this file to run the code below\n",
2022-03-11 02:08:53 +00:00
"df['babbage_similarity'] = df.babbage_similarity.apply(eval).apply(np.array)\n",
"X_train, X_test, y_train, y_test = train_test_split(df, df.Score, test_size = 0.2, random_state=42)\n",
"\n",
"user_embeddings = X_train.groupby('UserId').babbage_similarity.apply(np.mean)\n",
"prod_embeddings = X_train.groupby('ProductId').babbage_similarity.apply(np.mean)\n",
"len(user_embeddings), len(prod_embeddings)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that most of the users and products appear within the 50k examples only once."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Evaluate the embeddings\n",
"\n",
"To evaluate the recommendations, we look at the similarity of the user and product embeddings amongst the reviews in the unseen test set. We calculate the cosine distance between the user and product embeddings, which gives us a similarity score between 0 and 1. We then normalize the scores to be evenly split between 0 and 1, by calculating the percentile of the similarity score amongst all predicted scores."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from openai.embeddings_utils import cosine_similarity\n",
"\n",
"# evaluate embeddings as recommendations on X_test\n",
"def evaluate_single_match(row):\n",
" user_id = row.UserId\n",
" product_id = row.ProductId\n",
" try:\n",
" user_embedding = user_embeddings[user_id]\n",
" product_embedding = prod_embeddings[product_id]\n",
" similarity = cosine_similarity(user_embedding, product_embedding)\n",
" return similarity\n",
" except Exception as e:\n",
" return np.nan\n",
"\n",
"X_test['cosine_similarity'] = X_test.apply(evaluate_single_match, axis=1)\n",
"X_test['percentile_cosine_similarity'] = X_test.cosine_similarity.rank(pct=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.1 Visualize cosine similarity by review score\n",
"\n",
"We group the cosine similarity scores by the review score, and plot the distribution of cosine similarity scores for each review score."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Correlation between user&vector similarity percentile metric and review number of stars (score): 22.11%\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEcCAYAAADA5t+tAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAaO0lEQVR4nO3de5Qc5X3m8e+j0QUMWEhgTwAJDY5lBa2IAQlYDt5YWhEsbEciCUkgNjGLYMxiJfJhFySIFzA+2YPjS7LGJFnFloEQJBx8OQqIiw3TdnzBIK42wmR1QLJGsCtuQgwgpBG//aNKuJnumWnoalXPvM/nnDp0Vb1d9etXwzPvvF1drYjAzMzSMabsAszMbO9y8JuZJcbBb2aWGAe/mVliHPxmZolx8JuZJcbBb3uVpJD03rLrKJOkuZJ6h9iffB9Zazn4EyVpo6RXJfVJekHSrZKmll3XHpLOlvSjsusYySSNl/QlSb35v/NGSX9bdl1WPgd/2n4vIvYHDgH+H3B1yfW0jKSx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3MrKWSDf7+/n6OPvpo5s+fT0QgiXnz5nH33XeXXZqZWUslG/wdHR1UKhW++MUvMnPmTNavX89FF11ER0dH2aWZmbVUsnP8g92Xx/frMbPRLtkR/+uvv053dzeXXnopr732GhMmTODcc89lxYoVZZdmZtZSyY74J0yYwIwZM9ixYwc9PT3s2LGDGTNmMGHChLJLMzNrqWRH/Oeddx7Lli0DYObMmXz5y19m2bJlnH/++SVXZmbWWskG/9VXXw3wpqme888//43tZmajVbJTPZCFf/VUj0PfzFIw6kf8kgo5jq/2MbPRYtSP+CN
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import statsmodels.api as sm\n",
"\n",
"\n",
"correlation = X_test[['percentile_cosine_similarity', 'Score']].corr().values[0,1]\n",
"print('Correlation between user & vector similarity percentile metric and review number of stars (score): %.2f%%' % (100*correlation))\n",
"\n",
"# boxplot of cosine similarity for each score\n",
"X_test.boxplot(column='percentile_cosine_similarity', by='Score')\n",
"plt.title('')\n",
"plt.show()\n",
"plt.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can observe a weak trend, showing that the higher the similarity score between the user and the product embedding, the higher the review score. Therefore, the user and product embeddings can weakly predict the review score - even before the user receives the product!\n",
"\n",
"Because this signal works in a different way than the more commonly used collaborative filtering, it can act as an additional feature to slightly improve the performance on existing problems."
]
}
],
"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
}