(
self,
userid,
user_items,
N=10,
filter_already_liked_items=True,
filter_items=None,
recalculate_user=False,
items=None,
)
| 146 | return ids, 1 - (scores**2) / 2 |
| 147 | |
| 148 | def recommend( |
| 149 | self, |
| 150 | userid, |
| 151 | user_items, |
| 152 | N=10, |
| 153 | filter_already_liked_items=True, |
| 154 | filter_items=None, |
| 155 | recalculate_user=False, |
| 156 | items=None, |
| 157 | ): |
| 158 | if (filter_already_liked_items or recalculate_user) and not isinstance( |
| 159 | user_items, csr_matrix |
| 160 | ): |
| 161 | raise ValueError("user_items needs to be a CSR sparse matrix") |
| 162 | |
| 163 | if items is not None and self.approximate_recommend: |
| 164 | raise NotImplementedError("using a 'items' list with ANN search isn't supported") |
| 165 | |
| 166 | if not self.approximate_recommend: |
| 167 | return self.model.recommend( |
| 168 | userid, |
| 169 | user_items, |
| 170 | N=N, |
| 171 | filter_already_liked_items=filter_already_liked_items, |
| 172 | filter_items=filter_items, |
| 173 | recalculate_user=recalculate_user, |
| 174 | items=items, |
| 175 | ) |
| 176 | |
| 177 | # batch computation isn't supported by annoy, fallback to looping over items |
| 178 | if not np.isscalar(userid): |
| 179 | return _batch_call( |
| 180 | self.recommend, |
| 181 | userid, |
| 182 | user_items=user_items, |
| 183 | N=N, |
| 184 | filter_already_liked_items=filter_already_liked_items, |
| 185 | filter_items=filter_items, |
| 186 | recalculate_user=recalculate_user, |
| 187 | items=items, |
| 188 | ) |
| 189 | |
| 190 | # support recalculate_user if possible (TODO: come back to this since its a bit of a hack) |
| 191 | if hasattr(self.model, "+_user_factor"): |
| 192 | user = self.model._user_factor(userid, user_items, recalculate_user) # pylint: disable=protected-access |
| 193 | elif recalculate_user: |
| 194 | raise NotImplementedError(f"recalculate_user isn't supported with {self.model}") |
| 195 | else: |
| 196 | user = self.model.user_factors[userid] |
| 197 | if implicit.gpu.HAS_CUDA and isinstance(user, implicit.gpu.Matrix): |
| 198 | user = user.to_numpy() |
| 199 | |
| 200 | # calculate the top N items, removing the users own liked items from |
| 201 | # the results |
| 202 | count = N |
| 203 | if filter_items: |
| 204 | count += len(filter_items) |
| 205 | filter_items = np.array(filter_items) |
nothing calls this directly
no test coverage detected