<jupyter_start><jupyter_text>Related Anime Graph* This is a simple signal based off of related series* Two series are said to be related if one is a sequel/prequel/etc. of the other* The predicted score of a series is the average of all related series<jupyter_code># CHANGE THIS PARAMETER
recommendee = "taapaye"
import os
import pickle

import numpy as np
import pandas as pd
from tqdm import tqdm
# TODO read these hyperparameters from a file
# Change this to get recommendations for a different user
signal_name = f"related"
outdir = f"../../data/recommendations/{recommendee}"
os.chdir(outdir)
user_df = pickle.load(open("user_anime_list.pkl", "rb"))
#related_df = pickle.load(open("../../processed_data/related_anime_graph.pkl", "rb"))
def get_oos_prediction(user_df, related_df, anime_id):
    user_oos = user_df.loc[lambda x: x["anime_id"] != anime_id]
    series_df = (
        user_oos.merge(related_df, on="anime_id")
        .groupby("series_id")[["score", "score_var"]]
        .mean()
    )
    pred_scores = related_df.merge(series_df, on="series_id", how="left").fillna(0)
    pred_scores = pred_scores.rename(
        {"score": "delta", "score_var": "delta_var"}, axis=1
    ).drop("series_id", axis=1)
    return pred_scores.loc[lambda x: x["anime_id"] == anime_id]
def store_signals(signal_name):
    related_df = pickle.load(open(f"../../processed_data/{signal_name}_anime_graph.pkl", "rb"))
    signal_name=signal_name.replace('_', '')
    
    loocv_dfs = []
    for anime_id in tqdm(user_df["anime_id"]):
        loocv_dfs.append(get_oos_prediction(user_df, related_df, anime_id))
    loocv_df = pd.concat(loocv_dfs, ignore_index=True).set_index("anime_id")
    loocv_df.to_pickle(f"{signal_name}_loocv.pkl")
    
    pred_scores = (
        user_df.merge(related_df, on="anime_id").groupby("series_id")["score"].mean()
    )
    pred_vars = (
        user_df.merge(related_df, on="anime_id")
        .groupby("series_id")["score_var"]
        .apply(lambda x: x.sum() / x.size ** 2)
    )
    signal = pd.DataFrame()
    signal["delta"] = pred_scores
    signal["delta_var"] = pred_vars
    signal = (
        signal.merge(related_df, on="series_id")
        .drop("series_id", axis=1)
        .set_index("anime_id")
    )
    signal.to_pickle(f"{signal_name}.pkl")
store_signals('strict_relations')
store_signals('weak_relations')
store_signals('all_relations')<jupyter_output>100%|████████████████████████████████████████████████████████████████████████████████| 425/425 [00:04<00:00, 89.50it/s]
100%|████████████████████████████████████████████████████████████████████████████████| 425/425 [00:04<00:00, 92.09it/s]
100%|████████████████████████████████████████████████████████████████████████████████| 425/425 [00:04<00:00, 96.20it/s]
{'fraction_non_alphanumeric': 0.22391304347826088, 'fraction_numerical': 0.02463768115942029, 'mean_word_length': 4.716356107660456, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '231687', 'license': 'MIT', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1059, 'n_words': 179, 'path': 'notebooks/Signals/RelatedAnimeGraph.ipynb', 'repo_name': 'Fro116/RecommenderSystem', 'size': '2760'}