IALSRecommender

class rsdiv.recommenders.IALSRecommender(df_interaction: DataFrame, items: DataFrame, test_size: Union[float, int], random_split: bool = False, factors: int = 300, regularization: float = 0.03, alpha: float = 0.6, iterations: int = 10, random_state: Optional[int] = 42, toppop_mask: Optional[ndarray] = None)[source]

Bases: BaseRecommender

iALS recommender based on implicit.

Parameters
  • interaction (pd.DataFrame) – user-item interaction matrix.

  • items (Optional[pd.DataFrame]) – item side information.

  • factors (int) – the dimensions of user/item embeddings.

  • regularization (float) – regularization coefficient.

  • alpha (float) – the unobserved weight.

bm25(X: coo_matrix, K1: int = 100, B: float = 0.8) csr_matrix[source]

Weighs each col of a sparse matrix X by BM25 weighting. Taken from nearest_neighbours.py of implicit

recommend_single(user_string: str, top_k: int = 100) List[source]

Recommend for single user with top_k items.

Parameters
  • user_string (str) – the original token string for user.

  • top_k (int, optional) – top_k items to be recommended. Defaults to 100.

Returns

a list of recommended item ids.

Return type

List

get_score_single_user(user_string: str, keep_indices: ndarray) Optional[ndarray][source]

Get the single user’s predictions scores for the filtered items. Return None for new users.

Parameters
  • user_string (str) – Original user token string.

  • keep_indices (np.ndarray) – Items to be kept based on filters.

Returns

Predictions for the given items. Return None for new users.

Return type

Optional[np.ndarray]

get_topk_single_user(user_string: str, keep_indices: ndarray, top_k: int) Tuple[ndarray, ndarray][source]

Get the recommended item ids for a given user id.

Parameters
  • user_string (str) – User id string.

  • keep_indices (np.ndarray) – Indices for items to be kept.

  • top_k (int) – Top-k items to be recommended.

Returns

Recommended items ids and the corresponding scores. Return toppop for new users.

Return type

Tuple[np.ndarray, np.ndarray]

get_interaction(df_interaction: DataFrame) Tuple[DataFrame, ndarray, ndarray]

The converter for input dataframe

Parameters

df_interaction (pd.DataFrame) – user/item interaction matrix. columns should be [“userId”, “itemId”]

get_item_id(item_string: str) Optional[int]

Get the item_id for a given item_string.

Parameters

item_string (str) – Original item string.

Returns

The index of item_id in item_list. Return None it not in training set.

Return type

Optional[int]

get_topk_indices(scores: ndarray, top_k: int) ndarray

Get the indices correspond to the topk items.

Parameters
  • scores (np.ndarray) – Scores given by the models.

  • top_k (int) – Numbers of top items to be kept.

Returns

Indices for top_k items.

Return type

np.ndarray

get_user_id(user_string: str) Optional[int]

Get the user_id for a given user_string.

Parameters

user_string (str) – Original user string.

Returns

The index of user_id in user_list. Return None it not in training set.

Return type

Optional[int]