Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sasrec tests #193

Open
wants to merge 7 commits into
base: experimental/sasrec
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions rectools/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def recommend(
"""
self._check_is_fitted()
self._check_k(k)

item_type = dataset.item_id_map.external_dtype
dataset = self._custom_transform_dataset_u2i(dataset, users, on_unsupported_targets)

sorted_item_ids_to_recommend = self._get_sorted_item_ids_to_recommend(items_to_recommend, dataset)
Expand Down Expand Up @@ -194,6 +194,10 @@ def recommend(
reco_warm_final = self._reco_to_external(reco_warm, dataset.user_id_map, dataset.item_id_map)
reco_cold_final = self._reco_items_to_external(reco_cold, dataset.item_id_map)

reco_hot_final = self._adjust_reco_types(reco_hot_final, dataset.user_id_map.external_dtype, item_type)
reco_warm_final = self._adjust_reco_types(reco_warm_final, dataset.user_id_map.external_dtype, item_type)
reco_cold_final = self._adjust_reco_types(reco_cold_final, dataset.user_id_map.external_dtype, item_type)

del reco_hot, reco_warm, reco_cold

reco_all = self._concat_reco((reco_hot_final, reco_warm_final, reco_cold_final))
Expand Down Expand Up @@ -267,7 +271,7 @@ def recommend_to_items( # pylint: disable=too-many-branches
"""
self._check_is_fitted()
self._check_k(k)

item_type = dataset.item_id_map.external_dtype
dataset = self._custom_transform_dataset_i2i(dataset, target_items, on_unsupported_targets)

sorted_item_ids_to_recommend = self._get_sorted_item_ids_to_recommend(items_to_recommend, dataset)
Expand Down Expand Up @@ -318,6 +322,10 @@ def recommend_to_items( # pylint: disable=too-many-branches
reco_cold_final = self._reco_items_to_external(reco_cold, dataset.item_id_map)
del reco_hot, reco_warm, reco_cold

reco_hot_final = self._adjust_reco_types(reco_hot_final, item_type, item_type)
reco_warm_final = self._adjust_reco_types(reco_warm_final, item_type, item_type)
reco_cold_final = self._adjust_reco_types(reco_cold_final, item_type, item_type)

reco_all = self._concat_reco((reco_hot_final, reco_warm_final, reco_cold_final))
del reco_hot_final, reco_warm_final, reco_cold_final
reco_df = self._make_reco_table(reco_all, Columns.TargetItem, add_rank_col)
Expand Down Expand Up @@ -410,10 +418,12 @@ def _check_targets_are_valid(
return hot_targets, warm_targets, cold_targets

@classmethod
def _adjust_reco_types(cls, reco: RecoTriplet_T, target_type: tp.Type = np.int64) -> RecoTriplet_T:
def _adjust_reco_types(
cls, reco: RecoTriplet_T, target_type: tp.Type = np.int64, item_type: tp.Type = np.int64
) -> RecoTriplet_T:
target_ids, item_ids, scores = reco
target_ids = np.asarray(target_ids, dtype=target_type)
item_ids = np.asarray(item_ids, dtype=np.int64)
item_ids = np.asarray(item_ids, dtype=item_type)
scores = np.asarray(scores, dtype=np.float32)
return target_ids, item_ids, scores

Expand Down
Loading
Loading