benfred / implicit

Fast Python Collaborative Filtering for Implicit Feedback Datasets
https://benfred.github.io/implicit/
MIT License
3.57k stars 612 forks source link

When I pass an item_user matrix I get the wrong shape for item and user factors? #608

Closed josh-ashkinaze closed 2 years ago

josh-ashkinaze commented 2 years ago

This may be just be my misunderstanding. So as I understand, these recommendation objects take in an item_user matrix. But when I do that, I get the wrong shape for item_factors and user_factors. And conversely, when I pass in a user_item matrix, I get the right shape. So what am I doing wrong?


import random
import numpy as np
from scipy.sparse import csr_matrix
from implicit.als import AlternatingLeastSquares

random.seed(50)

# 100 users, 50 items 
U = 100
I = 50
user_item_matrix = np.zeros((U, I))
for user in range(U):
    for item in range(I):
        user_item_matrix[user][item] = random.choice([0,1,2])

# Pass in the user_item matrix, expecting the WRONG result 
# since ALS supposed to take item_user matrix --> got RIGHT dimensions
als = AlternatingLeastSquares(factors=5, regularization=1, iterations=5)
als.fit(csr_matrix(user_item_matrix))
print(als.user_factors.shape, als.item_factors.shape)
# (100, 5) (50, 5)
# CORRECTT

# Pass in the item_user matrix, expecting the RIGHT result 
# since ALS supposed to take item_user matrix --> got WRONG dimensions
als2 = AlternatingLeastSquares(factors=5, regularization=1, iterations=5)
als2.fit(csr_matrix(user_item_matrix.T))
print(als2.user_factors.shape, als2.item_factors.shape)
# (50, 5) (100, 5)
# NOT CORRECT
benfred commented 2 years ago

model.fit takes a user-items matrix - https://benfred.github.io/implicit/api/models/recommender_base.html#implicit.recommender_base.RecommenderBase.fit

There were some breaking API changes in v0.5.0 that might be tripping you up =( https://github.com/benfred/implicit/releases/tag/v0.5.0

josh-ashkinaze commented 2 years ago

Ah got it. Yep didn't realize the change in v0.5. Thanks!