Skip to content

Commit a1893be

Browse files
tohtskytohtsky
and
tohtsky
authored
modify iALS docs (#91)
Co-authored-by: tohtsky <you@example.com>
1 parent 5be4848 commit a1893be

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

irspack/recommenders/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ class BaseRecommender(object, metaclass=RecommenderMeta):
6060
each row correspods to a user's interaction with items.
6161
"""
6262

63-
X_train_all: sps.csr_matrix
6463
config_class: Type[RecommenderConfig]
6564

6665
def __init__(self, X_train_all: InteractionMatrix, **kwargs: Any) -> None:
67-
self.X_train_all = sps.csr_matrix(X_train_all).astype(np.float64)
66+
self.X_train_all: sps.csr_matrix = sps.csr_matrix(X_train_all).astype(
67+
np.float64
68+
)
69+
"""The matrix to feed into recommender."""
70+
6871
self.n_users: int = self.X_train_all.shape[0]
6972
self.n_items: int = self.X_train_all.shape[1]
7073
self.X_train_all.sort_indices()

irspack/recommenders/ials.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,25 @@ class IALSRecommender(
156156
BaseRecommenderWithUserEmbedding,
157157
BaseRecommenderWithItemEmbedding,
158158
):
159-
r"""Implementation of Implicit Alternating Least Squares(IALS) or Weighted Matrix Factorization(WMF).
159+
r"""Implementation of implicit Alternating Least Squares (iALS) or Weighted Matrix Factorization (WMF).
160160
161161
By default, it tries to minimize the following loss:
162162
163163
.. math ::
164164
165-
\frac{1}{2} \sum _{u, i \in S} X_{ui} (\mathbf{u}_u \cdot \mathbf{v}_i - 1) ^ 2
166-
+ \frac{\alpha_0}{2} \sum_{u, i} X_{ui} (\mathbf{u}_u \cdot \mathbf{v}_i) ^ 2 +
165+
\frac{1}{2} \sum _{u, i \in S} c_{ui} (\mathbf{u}_u \cdot \mathbf{v}_i - 1) ^ 2
166+
+ \frac{\alpha_0}{2} \sum_{u, i} (\mathbf{u}_u \cdot \mathbf{v}_i) ^ 2 +
167167
\frac{\text{reg}}{2} \left( \sum_u (\alpha_0 I + N_u) ^ \nu || \mathbf{u}_u || ^2 + \sum_i (\alpha_0 U + N_i) ^ \nu || \mathbf{v}_i || ^2 \right)
168168
169+
where :math:`S` denotes the set of all pairs wher :math:`X_{ui}` is non-zero.
170+
169171
See the seminal paper:
170172
171173
- `Collaborative filtering for implicit feedback datasets
172174
<http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.167.5120&rep=rep1&type=pdf>`_
173175
174176
175-
To speed up the learning procedure, we have also implemented the conjugate gradient descent version following:
177+
By default it uses a conjugate gradient descent version:
176178
177179
- `Applications of the conjugate gradient method for implicit feedback collaborative filtering
178180
<https://dl.acm.org/doi/abs/10.1145/2043932.2043987>`_
@@ -188,20 +190,26 @@ class IALSRecommender(
188190
n_components (int, optional):
189191
The dimension for latent factor. Defaults to 20.
190192
alpha0 (float, optional):
191-
The "unovserved" weight
193+
The "unobserved" weight.
192194
reg (float, optional) :
193195
Regularization coefficient for both user & item factors. Defaults to 1e-3.
194196
nu (float, optional) :
195197
Controlles frequency regularization introduced in the paper,
196198
"Revisiting the Performance of iALS on Item Recommendation Benchmarks".
197199
confidence_scaling (str, optional) :
198200
Specifies how to scale confidence scaling :math:`c_{ui}`. Must be either "none" or "log".
199-
If "none", the non-zero "rating" :math:`r_{ui}` yields
201+
If "none", the non-zero (not-necessarily 1) :math:`X_{ui}` yields
202+
200203
.. math ::
201-
c_{ui} = 1 + \alpha r_{ui}
204+
c_{ui} = A + X_{ui}
205+
202206
If "log",
207+
203208
.. math ::
204-
c_{ui} = 1 + \alpha \log (1 + r_{ui} / \epsilon )
209+
c_{ui} = A + \log (1 + X_{ui} / \epsilon )
210+
211+
The constant :math:`A` above will be 0 if ``loss_type`` is ``"IALSPP"``, :math:`\alpha_0` if ``loss_type`` is ``"ORIGINAL"``.
212+
205213
Defaults to "none".
206214
epsilon (float, optional):
207215
The :math:`\epsilon` parameter for log-scaling described above.
@@ -240,6 +248,18 @@ class IALSRecommender(
240248
Maximal number of conjute gradient descent steps during the prediction time,
241249
i.e., the case when a user unseen at the training time is given as a history matrix.
242250
Defaults to 5.
251+
252+
Examples:
253+
254+
>>> from irspack import IALSRecommender, rowwise_train_test_split, Evaluator
255+
>>> from irspack.utils.sample_data import mf_example_data
256+
>>> X = mf_example_data(100, 30, random_state=1)
257+
>>> X_train, X_test = rowwise_train_test_split(X, random_state=0)
258+
>>> rec = IALSRecommender(X_train)
259+
>>> rec.learn()
260+
>>> evaluator=Evaluator(X_test)
261+
>>> print(evaluator.get_scores(rec, [20]))
262+
OrderedDict([('hit@20', 1.0), ('recall@20', 0.9003412698412698), ('ndcg@20', 0.6175493479217139), ('map@20', 0.3848785870622406), ('precision@20', 0.3385), ('gini_index@20', 0.0814), ('entropy@20', 3.382497875272383), ('appeared_item@20', 30.0)])
243263
"""
244264

245265
config_class = IALSConfig

irspack/utils/id_mapping.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def get_recommendation_for_known_user_id(
134134
forbidden_item_ids: Optional[List[ItemIdType]] = None,
135135
) -> List[Tuple[ItemIdType, float]]:
136136
"""Retrieve recommendation result for a known user.
137+
137138
Args:
138139
user_id:
139140
The target user ID.
@@ -344,6 +345,7 @@ def score_to_recommended_items_batch(
344345
n_threads: Optional[int] = None,
345346
) -> List[List[Tuple[ItemIdType, float]]]:
346347
r"""Retrieve recommendation from score array.
348+
347349
Args:
348350
score:
349351
1d numpy ndarray for score.

0 commit comments

Comments
 (0)