Search code examples
matrixsparse-matrixcsr

How to get sparse.csr_matrix when user id and product id are non numeric?


I am trying to create a recommender system for that I need to create a sparse matrix. I am trying with sparse.csr_matrix but the problem is my user id and product id are non-numeric, for example, 34f7653-2de.

I am trying with the following code after converting datatype to categorical. I have also tried with sparse.coo_matrix

SUI = sparse.csr_matrix((train['item_count'].astype(float), (train['user_id'], train['item_id'])))

showing the following error:

TypeError: Categorical is not ordered for operation max you can use .as_ordered() to change the Categorical to an ordered one

Is there any way to do that non-number user id and product id?


Solution

  • We need to convert those non_numeric ids to numeric id. We can do that with following code.

    user_id = user_id.astype('category').cat.codes