irspack.utils.df_to_sparse#

irspack.utils.df_to_sparse(df, user_column, item_column, user_ids=None, item_ids=None, rating_column=None)[source]#

Convert pandas dataframe into sparse matrix.

Parameters:
  • df (DataFrame) – The dataframe to be converted into a sparse matrix.

  • user_column (str) – The column name for users.

  • item_column (str) – The column name for items.

  • user_ids (List[Any] | ndarray | None) – If not None, the resulting matrix’s rows correspond exactly to this list. In this case, rows where df[user_column] is not in user_ids will be dropped.

  • item_ids (List[Any] | ndarray | None) – If not None, the resulting matrix’s columns correspond exactly to this list. In this case, rows where df[item_column] is not in item_ids will be dropped.

  • rating_column (str | None) – If not None, the non-zero elements of the resulting matrix will correspond to the values of this column.

Raises:
  • RuntimeError – If user_ids is not None and df[user_column] contains values not in user_ids.

  • RuntimeError – If item_ids is not None and df[item_column] contains values not in item_ids.

Returns:

  • The resulting sparse matrix.

  • user ids corresponding to the rows in the matrix.

  • item ids corresponding to the columns in the matrix.

Return type:

Tuple[csr_matrix, ndarray, ndarray]