Recommender Systems Collaborative Filtering

# Recommender Systems & Collaborative Filtering

## Introduction & Motivation

Recommender Systems: predict user preferences. Collaborative filtering and content-based methods. Applications: e-commerce, streaming platforms, social networks.

Motivation: Personalized recommendations; improve user engagement.

Applications: Product recommendations, content discovery.

---

## Core Concepts & Theory

### User-Item Matrix

Represent preferences and interactions.

### Collaborative Filtering

Find similar users or items.

### Matrix Factorization

Decompose into latent factors.

### Cold-Start Problem

Handle new users/items with limited data.

---

## Mathematical Formulation

User-Item Interaction:
$$R \approx U imes V^T$$

SVD Factorization:
$$\min_{U,V} \|R - UV^T\|_F^2 + \lambda(\|U\|_F^2 + \|V\|_F^2)$$

Cosine Similarity:
$$ ext{sim}(u_i, u_j) = \frac{u_i^T u_j}{\|u_i\| \cdot \|u_j\|}$$

---

## Advanced Theory & Extensions

### Alternating Least Squares (ALS)

Efficient matrix factorization.

### Implicit Feedback

Handle unary feedback (clicks, views).

### Neural Collaborative Filtering

Deep learning for recommendations.

---

## Computational Considerations

Matrix factorization: O(N·M·K·iterations).

Similarity computation: O(N²) or O(M²).

Prediction: O(K).

---

## Practical Implementation Strategies

### Data Sparsity

Handling sparse user-item matrices.

### Top-K Recommendation

Efficient retrieval of best items.

### Diversity Promotion

Avoid repetitive recommendations.

---

## Benchmark Datasets & Evaluation

MovieLens: 25M ratings, 62K movies.

Netflix: 100M+ ratings.

Amazon Reviews: Product recommendation benchmark.

---

## Key Challenges & Limitations

### Cold Start

New users or items.

### Data Sparsity

Most entries unknown.

### Popularity Bias

Recommend popular items over niche.

---

## Hyperparameter Tuning

Latent factors: 20-100.

Regularization: 0.01-0.1.

Learning rate: 0.001-0.01.

---

## Real-World Applications & Case Studies

Netflix: Personalized content discovery.

Amazon: Cross-selling and upselling.

Spotify: Playlist and song recommendations.

---

## Integration with Other Methods

Recommender systems + NLP for content-based filtering; + knowledge graphs for semantic recommendations.

---

## Summary & Key Takeaways

Recommender Systems via matrix factorization enable personalized user recommendations.

Principles:
1. Collaborative filtering: User/item similarity.
2. Matrix factorization: Latent factors.
3. Implicit feedback: Binary interactions.
4. Cold start: Handle new entities.
5. Diversity: Varied recommendations.

---

---

## Appendix: Practical Labs

### Lab 1: User-Item Matrix

import numpy as np

def create_user_item_matrix(interactions, num_users, num_items):
 """Create sparse user-item matrix"""
 matrix = np.zeros((num_users, num_items))
 
 for user_id, item_id, rating in interactions:
 matrix[user_id, item_id] = rating
 
 return matrix

# Test
interactions = [(0, 0, 5), (0, 2, 4), (1, 1, 3), (2, 2, 5)]
matrix = create_user_item_matrix(interactions, 3, 3)

assert matrix.shape == (3, 3), "Matrix shape"
assert matrix[0, 0] == 5, "Correct rating"
print("✓ User-item matrix working")

if __name__ == "__main__":
 print("Lab 1: UserItemMatrix - PASSED")

### Lab 2: SVD Factorization

import numpy as np

def svd_factorization(matrix, k=2):
 """SVD matrix factorization"""
 U, s, Vt = np.linalg.svd(matrix, full_matrices=False)
 
 # Keep top-k factors
 U_k = U[:, :k]
 s_k = s[:k]
 Vt_k = Vt[:k, :]
 
 # Reconstruct
 reconstructed = U_k @ np.diag(s_k) @ Vt_k
 
 return U_k, s_k, Vt_k, reconstructed

# Test
np.random.seed(42)
matrix = np.random.randn(5, 3)

U, s, Vt, recon = svd_factorization(matrix, k=2)

assert U.shape[1] == 2, "Correct latent dimension"
assert recon.shape == matrix.shape, "Reconstructed shape"
print("✓ SVD factorization working")

if __name__ == "__main__":
 print("Lab 2: SVDFactorization - PASSED")

### Lab 3: Cosine Similarity

import numpy as np

def compute_cosine_similarity(user1_vec, user2_vec):
 """Compute cosine similarity between users"""
 dot_product = np.dot(user1_vec, user2_vec)
 norm1 = np.linalg.norm(user1_vec)
 norm2 = np.linalg.norm(user2_vec)
 
 similarity = dot_product / (norm1 * norm2 + 1e-8)
 
 return similarity

# Test
np.random.seed(42)
user1 = np.array([1, 0, 1, 0])
user2 = np.array([1, 0, 1, 1])

sim = compute_cosine_similarity(user1, user2)

assert -1 <= sim <= 1, "Similarity in range"
print("✓ Cosine similarity working")

if __name__ == "__main__":
 print("Lab 3: CosineSimilarity - PASSED")

### Lab 4: Top-K Recommendation

import numpy as np

def recommend_top_k(user_id, user_item_matrix, k=5):
 """Generate top-k recommendations"""
 user_ratings = user_item_matrix[user_id]
 
 # Items user hasn't rated
 unrated_mask = user_ratings == 0
 
 # Predicted scores (simplified: item popularity)
 item_popularity = user_item_matrix.sum(axis=0)
 predicted_scores = item_popularity.copy()
 predicted_scores[~unrated_mask] = -np.inf
 
 # Top-k
 top_k_idx = np.argsort(predicted_scores)[-k:]
 
 return top_k_idx[::-1]

# Test
np.random.seed(42)
matrix = np.random.rand(5, 10)
recs = recommend_top_k(0, matrix, k=3)

assert len(recs) == 3, "Correct recommendation count"
print("✓ Top-K recommendation working")

if __name__ == "__main__":
 print("Lab 4: TopKRecommendation - PASSED")

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account