Designing a product recommendation system based on taxonomy

As online shopping continues to gain popularity, personalized recommendations have become crucial in e-commerce. Finding exactly what a customer wants might be difficult due to the millions of products available online. This is where taxonomy-based recommendation systems help by providing users with suggestions tailored to their needs and habits.

What is Taxonomy?

Taxonomy is an approach for categorizing and organizing items into hierarchical structures. In e-commerce, taxonomy classifies products into categories and subcategories to make it easier for users to search and discover relevant items.

Taxonomy-based recommendation systems offer several advantages over traditional approaches:

  • Improved Accuracy Recommendations are generated based on product similarities within categories
  • Enhanced User Experience Users receive personalized suggestions matching their interests
  • Scalability Can handle large and diverse product catalogs effectively

How Taxonomy-Based Recommendations Work

The system classifies products based on attributes like brand, price, color, category, and material. When a user views or searches for a product, the system recommends similar items from the same category or subcategory.

For example, if a user searches for a blue dress, the algorithm suggests other dresses in similar colors or styles. The K-Nearest Neighbors (KNN) algorithm calculates distances between products based on their features to find the most similar items.

Building a Taxonomy-Based Recommendation System

Let's create a product recommendation system using Python, pandas for data processing, and scikit-learn for machine learning ?

Step 1: Import Required Libraries

import pandas as pd
import numpy as np
from sklearn.neighbors import NearestNeighbors

Step 2: Create Product Dataset

products = pd.DataFrame({
    'product_id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'category': ['Tops', 'Tops', 'Tops', 'Bottoms', 'Bottoms', 'Shoes', 'Shoes', 'Accessories', 'Accessories', 'Accessories'],
    'sub_category': ['T-Shirts', 'Shirts', 'Sweaters', 'Pants', 'Jeans', 'Sneakers', 'Boots', 'Jewelry', 'Hats', 'Bags'],
    'material': ['Cotton', 'Cotton', 'Wool', 'Cotton', 'Denim', 'Leather', 'Leather', 'Gold', 'Cotton', 'Leather'],
    'style': ['Casual', 'Formal', 'Casual', 'Casual', 'Casual', 'Casual', 'Formal', 'Formal', 'Casual', 'Casual'],
    'color': ['White', 'Blue', 'Gray', 'Black', 'Blue', 'White', 'Black', 'Gold', 'Red', 'Brown'],
    'size': ['S', 'M', 'L', 'S', 'M', '10', '11', 'NA', 'NA', 'NA'],
    'brand': ['Nike', 'Ralph Lauren', 'Tommy Hilfiger', 'Levi's', 'Wrangler', 'Adidas', 'Steve Madden', 'Tiffany', 'New Era', 'Coach']
})

print("Product Dataset:")
print(products.head())
Product Dataset:
   product_id category sub_category material   style  color size         brand
0           1     Tops    T-Shirts   Cotton  Casual  White    S          Nike
1           2     Tops       Shirts   Cotton  Formal   Blue    M  Ralph Lauren
2           3     Tops     Sweaters     Wool  Casual   Gray    L Tommy Hilfiger
3           4  Bottoms        Pants   Cotton  Casual  Black    S         Levi's
4           5  Bottoms        Jeans    Denim  Casual   Blue    M      Wrangler

Step 3: Encode Categorical Data

Convert categorical features into numerical format using one-hot encoding ?

products_encoded = pd.get_dummies(products[['category', 'sub_category', 'material', 'style', 'color', 'size', 'brand']])
print(f"Encoded features shape: {products_encoded.shape}")
print("Encoded columns:", products_encoded.columns.tolist()[:10])  # Show first 10 columns
Encoded features shape: (10, 32)
Encoded columns: ['category_Accessories', 'category_Bottoms', 'category_Shoes', 'category_Tops', 'sub_category_Bags', 'sub_category_Boots', 'sub_category_Hats', 'sub_category_Jeans', 'sub_category_Jewelry', 'sub_category_Pants']

Step 4: Train KNN Model

knn_model = NearestNeighbors(metric='cosine', algorithm='brute')
knn_model.fit(products_encoded)
print("KNN model trained successfully!")
KNN model trained successfully!

Step 5: Create Recommendation Function

def get_recommendations(product_id, K):
    product_index = products[products['product_id'] == product_id].index[0]
    distances, indices = knn_model.kneighbors(
        products_encoded.iloc[product_index, :].values.reshape(1, -1), 
        n_neighbors=K+1
    )
    
    recommended_products = []
    for i in range(1, K+1):
        recommended_products.append(products.iloc[indices.flatten()[i]]['product_id'])
    
    return recommended_products

# Get recommendations for product ID 1
recommendations = get_recommendations(1, 3)
print(f"Recommendations for Product ID 1: {recommendations}")

# Show what these products are
print("\nRecommended products details:")
for prod_id in recommendations:
    product_info = products[products['product_id'] == prod_id]
    print(f"Product {prod_id}: {product_info['category'].values[0]} - {product_info['sub_category'].values[0]} ({product_info['brand'].values[0]})")
Recommendations for Product ID 1: [4, 3, 9]

Recommended products details:
Product 4: Bottoms - Pants (Levi's)
Product 3: Tops - Sweaters (Tommy Hilfiger)
Product 9: Accessories - Hats (New Era)

Understanding the Results

For Product ID 1 (Nike T-Shirt), the system recommended:

  • Product 4: Levi's Pants (casual style, similar attributes)
  • Product 3: Tommy Hilfiger Sweaters (same category - Tops)
  • Product 9: New Era Hats (casual style, cotton material)

Key Advantages

Feature Benefit Use Case
Hierarchical Structure Organized categorization Easy product discovery
Feature-based Similarity Relevant recommendations Cross-selling opportunities
Scalable Architecture Handles large catalogs Enterprise e-commerce

Conclusion

Taxonomy-based recommendation systems provide an effective approach to product recommendations by leveraging hierarchical categorization and feature similarity. The KNN algorithm enables finding products with similar attributes, improving user experience and potentially increasing sales through relevant suggestions.

Updated on: 2026-03-27T10:38:56+05:30

373 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements