Skip to content

Welcome to the world of e-commerce, where customer feedback is a goldmine of insights! In this project, you'll dive into the Women's Clothing E-Commerce Reviews dataset, focusing on the 'Review Text' column filled with direct customer opinions.

Your mission is to use text embeddings and Python to analyze these reviews, uncover underlying themes, and understand customer sentiments. This analysis will help improve customer service and product offerings.

The Data

You will be working with a dataset specifically focusing on customer reviews. Below is the data dictionary for the relevant field:

womens_clothing_e-commerce_reviews.csv

ColumnDescription
'Review Text'Textual feedback provided by customers about their shopping experience and product quality.

Armed with access to powerful embedding API services, you will process the reviews, extract meaningful insights, and present your findings.

Let's get started!

Install useful libraries

# Run this cell to install ChromaDB if desired
try:
    assert version('chromadb') == '0.4.17'
except:
    !pip install chromadb==0.4.17
try:
    assert version('pysqlite3') == '0.5.2'
except:
    !pip install pysqlite3-binary==0.5.2
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
Hidden output

Load the dataset

Load data and perform basic data checks to ensure you are using relevant data for the analysis

# Load the dataset
import pandas as pd
pd.set_option('display.max_columns', 11)
reviews = pd.read_csv("womens_clothing_e-commerce_reviews.csv")

# Display the first few entries
reviews.head()
print(reviews.info())
reviews = reviews.dropna(subset=['Review Text'])
print('\n\n', reviews.index)
reviews = reviews.reset_index(drop=True)
print('\n\n', reviews.index, '\n\n')
print('\n\n', reviews.info())
print(reviews.head())
# Start coding here
# Use as many cells as you need.
from openai import OpenAI

client = OpenAI(api_key="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")

review_text = reviews['Review Text'].tolist()
print(len(review_text))
#print(review_text)

batch_size=100
batches = [review_text[i * batch_size : min(len(review_text), (i + 1) * batch_size)] for i in range(10)]
embeddings = []
for batch in batches:
    emb = client.embeddings.create(input=batch, model='text-embedding-3-small').model_dump()
    embeddings.extend([emb['data'][i]['embedding'] for i in range(len(batch))])
print(len(embeddings))
print(embeddings[:5])
from sklearn.manifold import TSNE
import numpy as np
import matplotlib.pyplot as plt

tsne = TSNE(n_components=2, random_state=16)
embeddings_2d = tsne.fit_transform(np.array(embeddings))
x = embeddings_2d[:, 0]
y = embeddings_2d[:, 1]
plt.scatter(x, y, c=reviews['Rating'])
plt.show()
from scipy.spatial import distance

query = 'comfort'
query_emb = client.embeddings.create(input=query, model='text-embedding-3-small').model_dump()['data'][0]['embedding']
for index, emb in enumerate(embeddings):
    cos = distance.cosine(query_emb, emb)
    if cos < 0.7:
        print(f"Review: {reviews.loc[index, 'Review Text']}, Score: {cos}")
input_review = reviews.loc[0, 'Review Text']
print(f"Input review: {input_review}")

def find_3_closest(input_review):
    distances = []
    input_emb = client.embeddings.create(input=input_review, model='text-embedding-3-small').model_dump()['data'][0]['embedding']
    #print(input_emb)
    for index, emb in enumerate(embeddings):
        cos = distance.cosine(input_emb, emb)
        distances.append({'distance' : cos, 'index' : index, 'embedding' : emb})
    distances = sorted(distances, key=lambda x:x['distance'])
    return distances[:3]

most_similar_reviews = []
for r in find_3_closest(input_review):
    most_similar_reviews.append(reviews.loc[r['index'], 'Review Text'])
    print(f"\n\nReview: {reviews.loc[r['index'], 'Review Text']}, Score: {r['distance']}, Index: {r['index']}")