def recommend_k_fastai(model, test, train): with Timer() as t: total_users, total_items = model.data.train_ds.x.classes.values() total_items = total_items[1:] total_users = total_users[1:] test_users = test[DEFAULT_USER_COL].unique() test_users = np.intersect1d(test_users, total_users) users_items = cartesian_product(test_users, total_items) users_items = pd.DataFrame( users_items, columns=[DEFAULT_USER_COL, DEFAULT_ITEM_COL]) training_removed = pd.merge( users_items, train.astype(str), on=[DEFAULT_USER_COL, DEFAULT_ITEM_COL], how="left", ) training_removed = training_removed[ training_removed[DEFAULT_RATING_COL].isna()][[ DEFAULT_USER_COL, DEFAULT_ITEM_COL ]] topk_scores = score( model, test_df=training_removed, user_col=DEFAULT_USER_COL, item_col=DEFAULT_ITEM_COL, prediction_col=DEFAULT_PREDICTION_COL, top_k=DEFAULT_K, ) return topk_scores, t
def predict_k(self, dataset: RecommendationDataset, k: int) -> pd.DataFrame: total_users = dataset.data[dataset.user_col].unique() total_items = dataset.data[dataset.item_col].unique() users_items = cartesian_product(total_users, total_items) users_items = pd.DataFrame(users_items, columns=[dataset.user_col, dataset.item_col]) training_removed = pd.merge(users_items, dataset.data, on=[dataset.user_col, dataset.item_col], how='left') training_removed = training_removed[training_removed[dataset.score_col].isna()][[dataset.user_col, dataset.item_col]] return self.predict_scores(dataset.wrap_data(training_removed))
def recommend_k_fastai(model, test, train): with Timer() as t: total_users, total_items = model.data.train_ds.x.classes.values() total_items = total_items[1:] total_users = total_users[1:] test_users = test[DEFAULT_USER_COL].unique() test_users = np.intersect1d(test_users, total_users) users_items = cartesian_product(test_users, total_items) users_items = pd.DataFrame(users_items, columns=[DEFAULT_USER_COL, DEFAULT_ITEM_COL]) training_removed = pd.merge(users_items, train.astype(str), on=[DEFAULT_USER_COL, DEFAULT_ITEM_COL], how='left') training_removed = training_removed[training_removed[DEFAULT_RATING_COL].isna()][[DEFAULT_USER_COL, DEFAULT_ITEM_COL]] topk_scores = score(model, test_df=training_removed, user_col=DEFAULT_USER_COL, item_col=DEFAULT_ITEM_COL, prediction_col=DEFAULT_PREDICTION_COL, top_k=DEFAULT_K) return topk_scores, t
learn.export('export.pkl') learner = load_learner(path="/Users/rohittikle/Documents/GitHub/recommenders/") #st.write(learner) total_users, total_items = learner.data.train_ds.x.classes.values() total_items = total_items[1:] total_users = total_users[1:] #st.write("Items:",total_items,"Users:",total_users) #st.write("Users:",total_users) test_users = data_test[USER].unique() test_users = np.intersect1d(test_users, total_users) #st.write("Test Users:",test_users) users_items = cartesian_product(np.array(test_users),np.array(total_items)) users_items = pd.DataFrame(users_items, columns=[USER,ITEM]) # st.write("Users:",users_items) training_removed = pd.merge(users_items, data_train.astype(str), on=[USER, ITEM], how='left') training_removed = training_removed[training_removed['Rating'].isna()][[USER,ITEM]] # st.write("Training:",training_removed) start_time = time.time() # st.write("Start Time:",start_time) top_k_scores = score(learner, test_df=training_removed, user_col=USER, item_col=ITEM, prediction_col=PREDICTION) #st.write("TOP K SCORES:",top_k_scores) test_time = time.time() - start_time