DEV Community

Amelia
Amelia

Posted on

Building a Denim Recommendation Engine with Python

Ever stared at a wall of women's jeans online, overwhelmed by choices? As a developer, I thought: why not build a simple recommendation system to match jeans to body types and preferences? Let's walk through a basic content-based filtering approach.

First, we need data. For this demo, I created a small dataset of jeans with features like rise, stretch, cut, and wash. Each feature gets a numeric score.

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

jeans_data = {
    'name': ['High-Rise Skinny', 'Mom Jeans', 'Bootcut Stretch', 'Wide Leg'],
    'rise': [9, 7, 8, 6],
    'stretch': [8, 4, 7, 3],
    'cut': [3, 6, 5, 8],
    'wash': ['dark', 'light', 'medium', 'black']
}
df = pd.DataFrame(jeans_data)
Enter fullscreen mode Exit fullscreen mode

The tricky part is combining numerical features with categorical ones. I used TF-IDF for the wash column and concatenated everything into a feature matrix.

# Convert wash to TF-IDF
tfidf = TfidfVectorizer()
wash_matrix = tfidf.fit_transform(df['wash']).toarray()

# Combine with numerical features
features = df[['rise', 'stretch', 'cut']].values
import numpy as np
feature_matrix = np.hstack([features, wash_matrix])
Enter fullscreen mode Exit fullscreen mode

Now, the recommendation function. We compute cosine similarity between a user's preference vector and all jeans. For example, a user who wants high-rise, stretchy, dark-wash jeans:

def recommend(user_prefs, feature_matrix, df, top_n=2):
    user_vector = np.array([user_prefs['rise'], user_prefs['stretch'], 
                            user_prefs['cut'], 0, 0])  # simplified
    similarities = cosine_similarity([user_vector], feature_matrix)[0]
    indices = similarities.argsort()[-top_n:][::-1]
    return df.iloc[indices]['name'].tolist()

user = {'rise': 9, 'stretch': 8, 'cut': 3}
print(recommend(user, feature_matrix, df))
# Output: ['High-Rise Skinny', 'Bootcut Stretch']
Enter fullscreen mode Exit fullscreen mode

This is obviously simplified. In production, you'd normalize features, handle missing data, and add user feedback loops. But it shows how you can turn a shopping dilemma into a fun coding challenge.

For real-world deployment, you'd scrape product data from somewhere like Frishay's women jeans collection, but that's a whole other tutorial. The key takeaway: recommendation systems don't have to be black boxes. Start small, iterate, and you'll build something that actually helps people find their perfect fit.

Top comments (1)

Collapse
 
davitparkltd profile image
Davit Park

Great post! It's amazing how much we take for granted until we stop and think about it. What sparked your interest in this topic?