DEV Community

Thesius Code
Thesius Code

Posted on • Originally published at datanest-stores.pages.dev

Product Recommendation Engine

Product Recommendation Engine

Collaborative filtering, content-based recommendations, trending product detection, and A/B testing integration. Increase average order value with personalized product suggestions.

Key Features

  • Collaborative Filtering — "Customers who bought X also bought Y" recommendations
  • Content-Based Filtering — Recommend similar products by attributes and descriptions
  • Trending Detection — Surface products with rising purchase velocity
  • Hybrid Scoring — Blend collaborative and content-based signals with configurable weights
  • A/B Testing — Compare recommendation strategies and measure lift
  • Cold Start Handling — Fallback strategies for new users and new products

Quick Start

# 1. Extract and configure
unzip product-recommendation-engine.zip
cd product-recommendation-engine
cp config.example.yaml config.yaml

# 2. Train the recommendation model
python -m recommendation_engine.core --train --config config.yaml

# 3. Get recommendations for a customer
python -m recommendation_engine.core --recommend --customer-id C-1001 --limit 5
Enter fullscreen mode Exit fullscreen mode

Architecture

src/recommendation_engine/
├── core.py              # Orchestrator: training pipeline and recommendation API
├── collaborative.py     # User-item matrix, co-occurrence, similarity scoring
├── content_based.py     # Product attribute matching, TF-IDF text similarity
├── trending.py          # Velocity detection, time-decay scoring
├── hybrid.py            # Score blending and re-ranking
└── utils.py             # Matrix operations, normalization, evaluation metrics
Enter fullscreen mode Exit fullscreen mode

Pipeline: Purchase History → Build User-Item Matrix → Compute Similarities → Rank → Filter → Serve

Usage Examples

Get Personalized Recommendations

from recommendation_engine.core import RecommendationEngine

engine = RecommendationEngine(config_path="config.yaml")
engine.train(orders_data=orders, products_data=products)

recs = engine.recommend(customer_id="C-1001", limit=5)
for rec in recs:
    print(f"  {rec['product_name']} (score: {rec['score']:.3f}, "
          f"method: {rec['source']})")
# Classic Blue Widget (score: 0.892, method: collaborative)
# Premium Red Gadget (score: 0.834, method: hybrid)
# Deluxe Green Tool  (score: 0.756, method: content_based)
Enter fullscreen mode Exit fullscreen mode

Find Similar Products

from recommendation_engine.content_based import ContentRecommender

content_rec = ContentRecommender(config_path="config.yaml")
similar = content_rec.find_similar(
    sku="WIDGET-BLU-LG",
    limit=5,
    min_score=0.5
)

for item in similar:
    print(f"  {item['sku']}: similarity={item['score']:.3f}")
Enter fullscreen mode Exit fullscreen mode

Trending Products

from recommendation_engine.trending import TrendingDetector

detector = TrendingDetector(
    window_days=7,
    min_orders=10,
    velocity_threshold=1.5  # 1.5x normal sales rate
)

trending = detector.detect(orders)
for product in trending[:5]:
    print(f"  {product['name']}: {product['velocity']:.1f}x normal rate")
Enter fullscreen mode Exit fullscreen mode

Co-Purchase Analysis Query

-- Products frequently bought together
SELECT
    a.product_id AS product_a,
    b.product_id AS product_b,
    pa.product_name AS name_a,
    pb.product_name AS name_b,
    COUNT(DISTINCT a.order_id) AS co_purchase_count,
    COUNT(DISTINCT a.order_id) * 1.0 /
        (SELECT COUNT(DISTINCT order_id) FROM order_items
         WHERE product_id = a.product_id) AS confidence
FROM order_items a
JOIN order_items b ON a.order_id = b.order_id AND a.product_id < b.product_id
JOIN products pa ON a.product_id = pa.id
JOIN products pb ON b.product_id = pb.id
GROUP BY a.product_id, b.product_id, pa.product_name, pb.product_name
HAVING COUNT(DISTINCT a.order_id) >= 10
ORDER BY co_purchase_count DESC
LIMIT 20;
Enter fullscreen mode Exit fullscreen mode

Configuration

model:
  type: "hybrid"                   # collaborative | content_based | hybrid
  collaborative_weight: 0.6       # Weight for collaborative signal (hybrid mode)
  content_weight: 0.4             # Weight for content signal (hybrid mode)

collaborative:
  algorithm: "item_knn"           # item_knn | user_knn | co_occurrence
  k_neighbors: 20                 # Number of similar items to consider
  min_co_purchases: 3             # Minimum co-purchase count to consider

content_based:
  features: ["category", "brand", "price_range", "tags"]
  text_fields: ["name", "description"]
  similarity_metric: "cosine"     # cosine | jaccard

trending:
  window_days: 7
  min_orders: 10
  velocity_threshold: 1.5

cold_start:
  new_user_strategy: "popular"    # popular | trending | category_top
  new_product_strategy: "content" # content | manual_boost
  min_interactions: 5             # User needs N orders before collaborative kicks in

output:
  max_recommendations: 10
  exclude_purchased: true         # Don't recommend already-purchased items
  diversity_factor: 0.3           # 0=pure relevance, 1=max diversity
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Start with collaborative filtering — It outperforms content-based for most stores
  2. Handle cold start explicitly — New users and products need fallback strategies
  3. Exclude recently purchased items — Don't recommend what they just bought
  4. Add diversity — Pure relevance creates filter bubbles; blend in variety
  5. Retrain regularly — Weekly retraining captures shifting purchase patterns

Troubleshooting

Issue Cause Fix
Same recommendations for everyone Not enough purchase history Lower min_interactions or use trending fallback
Irrelevant recommendations Content features too broad Add more specific features (tags, sub-categories)
Training too slow Large user-item matrix Reduce k_neighbors or sample older data
No trending products detected Threshold too high Lower velocity_threshold to 1.2

This is 1 of 11 resources in the Retail Automation Pro toolkit. Get the complete [Product Recommendation Engine] with all files, templates, and documentation for $49.

Get the Full Kit →

Or grab the entire Retail Automation Pro bundle (11 products) for $139 — save 30%.

Get the Complete Bundle →


Related Articles

Top comments (0)