This project performs unsupervised clustering of Amazon products using textual metadata, image features, and co-purchase/view graphs. It is tailored for the "Cell Phones and Accessories" category and supports three major clustering strategies:
- Identity Clustering (Text-Based) — groups based on product descriptions/titles.
- Similarity Clustering (Multimodal) — combines both text and image features.
- Complementary Product Clustering — detects related products based on graph links from
also_buyandalso_view.
Visualizations of clustered products (image grids) are also generated for fast inspection.
You can download Amazon Data (2018) for cell phones and accessories from: https://mcauleylab.ucsd.edu/public_datasets/data/amazon_v2/metaFiles2/meta_Cell_Phones_and_Accessories.json.gz
🔹 clustered_products.csv # Final dataset with cluster labels
🔹 product_clustering.py # Preprocessing + Embedding Generator
🔹 amazon_clustering_pipeline.py # Clustering logic (Text, Image, Graph)
🔹 amazon_clustering_pipeline.py # Clustering logic (Text, Image, Graph)
🔹 validate_clusters.py # validating results
🔹 plot_clusters.py # plot generator for each cluster
🔹 meta_Cell_Phones_and_Accessories.json # Raw metadata (from Amazon)
🔹 cellphones_subset.csv # Cleaned dataset with key fields
🔹 images/ # Downloaded product images
🔹 cluster_grids/ # Image grids for cluster preview
🔹 text_embeddings.npy # SentenceTransformer embeddings
🔹 image_embeddings.npy # CLIP image embeddings
🔹 README.md # Project overview and usage
Make sure you have:
- Python 3.8+
- A GPU (optional but recommended for CLIP embeddings)
Install dependencies:
pip install -r requirements.txt📄 requirements.txt (example)
pandas
numpy
scikit-learn
umap-learn
hdbscan
networkx
matplotlib
sentence-transformers
torch
tqdm
Pillow
clip @ git+https://github.com/openai/CLIP.git
python product_clustering.pyThis will:
-
Parse and clean product metadata (
meta_*.json→cellphones_subset.csv) -
Download images to
images/ -
Generate and save:
text_embeddings.npyusing Sentence-BERT (all-MiniLM-L6-v2)image_embeddings.npyusing CLIP (ViT-B/32)
python amazon_clustering_pipeline.pyThis script:
-
Loads and normalizes embeddings
-
Applies:
- KMeans (Text Only — Identity Clustering)
- UMAP + HDBSCAN (Text + Image — Similarity Clustering)
- Graph Clustering via NetworkX (Complementary Product Clustering)
-
Outputs
clustered_products.csvwith:identity_clustersimilarity_clustercomplementary_cluster
python validate_clusters.pypython visualize_clusters_grids.pyThis generates image grids for each cluster into cluster_grids/:
cluster_grids/
🔹 identity_cluster_0.png
🔹 similarity_cluster_2.png
🔹 complementary_cluster_5.png
└── ...
Each image contains up to 5 representative products in that cluster:
| asin | title | image_url | identity_cluster | similarity_cluster | complementary_cluster |
|---|---|---|---|---|---|
| B00012345 | Apple iPhone XR - 64GB | ... | 0 | 2 | 5 |
| B00067890 | OtterBox Case for iPhone XR | ... | 3 | 2 | 5 |
- You can tune
n_clusters(KMeans) ormin_cluster_size(HDBSCAN) for different clustering granularity. - Image quality and missing downloads may impact visual clustering—these are skipped if unavailable.
- You may also explore embedding visualization with t-SNE or UMAP in future work.
- Fine-tune models on e-commerce domain-specific data.
- Add interactive HTML visualizations for clusters.
- Implement multilingual support for product text.
- Use category hierarchy more deeply in clustering.
Developed by razi-tm
Inspired by OpenAI CLIP, Amazon Metadata, and SBERT
MIT License. Use freely with attribution.
