<jupyter_start><jupyter_text>Table of Contents- [Step C.1](Step-C.1)- [Step C.2](Step-C.2)  Step C.1<jupyter_code># disclaimer: may not work as is in Windows OS
# download the metadata for this dataset
# dataset source: https://nijianmo.github.io/amazon/index.html

!wget --backups=1 http://deepyeti.ucsd.edu/jianmo/amazon/metaFiles2/meta_Digital_Music.json.gz -P data/
def inspect_df(df: pd.DataFrame, n: int = 5) -> pd.DataFrame:
    """Helper method to easily inspect DataFrames."""

    print(f"shape: {df.shape}")

    return df.head(n)
def parse(filepath: str) -> Iterator[dict]:
    file_obj = gzip.open(filepath, "rb")
    for line in file_obj:
        yield json.loads(line)
def file_to_dataframe(filepath: str) -> pd.DataFrame:
    i = 0
    df = {}
    for d in parse(filepath):
        df[i] = d
        i += 1
    return pd.DataFrame.from_dict(df, orient="index")
metadata = file_to_dataframe("data/meta_Digital_Music.json.gz")

inspect_df(metadata)
naive_df = pickle.load(open("data/naive-RS.pkl", "rb"))
content_based_df = pickle.load(open("data/content-based-RS.pkl", "rb"))
collab_filtering_df = pickle.load(open("data/collab-filtering-RS.pkl", "rb"))
naive_df.rename(
    columns={"reviewerID": "user", "asin": "top_recommendations"}, inplace=True
)
content_based_df.rename(
    columns={"reviewerID": "user", "asin": "top_recommendations"}, inplace=True
)
inspect_df(content_based_df)
inspect_df(collab_filtering_df)
inspect_df(naive_df)
all_ratings = pd.DataFrame()
all_ratings["user"] = naive_df["user"]
all_ratings["top_5_content"] = all_ratings["user"].map(
    lambda x: content_based_df[content_based_df["user"] == x][
        "top_recommendations"
    ].values[0][0:5]
)
all_ratings["top_5_collab"] = all_ratings["user"].map(
    lambda x: collab_filtering_df[collab_filtering_df["user"] == x][
        "top_recommendations"
    ].values[0][0:5]
)
all_ratings["all_content"] = all_ratings["user"].map(
    lambda x: content_based_df[content_based_df["user"] == x][
        "top_recommendations"
    ].values[0]
)
all_ratings["all_collab"] = all_ratings["user"].map(
    lambda x: collab_filtering_df[collab_filtering_df["user"] == x][
        "top_recommendations"
    ].values[0]
)
all_ratings["naive"] = all_ratings["user"].map(
    lambda x: naive_df[naive_df["user"] == x]["top_recommendations"].values[0]
)
inspect_df(all_ratings)
all_ratings_titles = pd.DataFrame()
all_ratings_titles["user"] = naive_df["user"]
all_ratings_titles["top_5_content_title"] = all_ratings["top_5_content"].map(
    lambda x: list(
        itertools.chain(*[metadata[metadata["asin"] == i]["title"] for i in x])
    )
)
all_ratings_titles["top_5_collab_title"] = all_ratings["top_5_collab"].map(
    lambda x: list(
        itertools.chain(*[metadata[metadata["asin"] == i]["title"] for i in x])
    )
)
all_ratings_titles["naive_title"] = all_ratings["naive"].map(
    lambda x: list(
        itertools.chain(*[metadata[metadata["asin"] == i]["title"] for i in x])
    )
)
all_ratings_titles
def get_ranking_positions_for_content_based(id_: str):
    top_5 = all_ratings[all_ratings["user"] == id_]["top_5_content"].values[0]
    all_rec = all_ratings[all_ratings["user"] == id_]["all_collab"].values[0]
    reccomendations_positions = {}
    positions = []

    for i in range(len(top_5)):
        top_5_index = i
        try:
            all_rec_index = all_rec.index(top_5[i])
        except ValueError:
            all_rec_index = -1
        if all_rec_index > 0:
            pos = round(abs(top_5_index - all_rec_index) / 2)
        else:
            pos = all_rec_index
        positions.append(pos)

    reccomendations_positions = dict(zip(top_5, positions))
    return reccomendations_positions
def get_ranking_positions_for_collab(id_: str):
    top_5 = all_ratings[all_ratings["user"] == id_]["top_5_collab"].values[0]
    all_rec = all_ratings[all_ratings["user"] == id_]["all_content"].values[0]
    reccomendations_positions = {}
    positions = []

    for i in range(len(top_5)):
        top_5_index = i
        try:
            all_rec_index = all_rec.index(top_5[i])
        except ValueError:
            all_rec_index = -1
        if all_rec_index > 0:
            pos = round(abs(top_5_index - all_rec_index) / 2)
        else:
            pos = all_rec_index
        positions.append(pos)

    reccomendations_positions = dict(zip(top_5, positions))
    return reccomendations_positions
def get_final_ranking_for_user(id_: str):
    collab_ranking = get_ranking_positions_for_collab(id_)
    content_ranking = get_ranking_positions_for_content_based(id_)
    all_rankins_dict = {**content_ranking, **collab_ranking}
    final_ranking_list = list(
        dict(sorted(all_rankins_dict.items(), key=lambda x: x[1], reverse=True)).keys()
    )

    return final_ranking_list
all_ratings["ranking_final"] = all_ratings["user"].map(
    lambda x: get_final_ranking_for_user(x)
)

inspect_df(all_ratings)
all_ratings_titles["ranking_final_title"] = all_ratings["ranking_final"].map(
    lambda x: list(
        itertools.chain(*[metadata[metadata["asin"] == i]["title"] for i in x])
    )
)

inspect_df(all_ratings_titles)<jupyter_output>shape: (167, 5)<jupyter_text>Step C.2<jupyter_code>def compare_recommendations_for_user(id_: str):

    hybrid_user = pd.DataFrame()
    hybrid_user["asin"] = all_ratings.loc[all_ratings["user"] == id_][
        "ranking_final"
    ].values[0]
    hybrid_user["title"] = pd.DataFrame(
        all_ratings_titles.loc[all_ratings_titles["user"] == id_][
            "ranking_final_title"
        ].values[0]
    )

    content_user = pd.DataFrame()
    content_user["asin"] = all_ratings.loc[all_ratings["user"] == id_][
        "top_5_content"
    ].values[0]
    content_user["title"] = pd.DataFrame(
        all_ratings_titles.loc[all_ratings_titles["user"] == id_][
            "top_5_content_title"
        ].values[0]
    )

    naive_user = pd.DataFrame()
    naive_user["asin"] = all_ratings.loc[all_ratings["user"] == id_]["naive"].values[0]
    naive_user["title"] = pd.DataFrame(
        all_ratings_titles.loc[all_ratings_titles["user"] == id_]["naive_title"].values[
            0
        ]
    )

    print(f"For user {id_} the recommendations are:")
    print("\n")
    print("HYBRID\n")
    print(hybrid_user)
    print("=" * 100)
    print("CONTENT-BASED\n")
    print(content_user)
    print("=" * 100)
    print("NAIVE\n")
    print(naive_user)
compare_recommendations_for_user("A15OXG4V7IK2D9")
compare_recommendations_for_user("A1352I3HWDQCZH")
compare_recommendations_for_user("AYPCUQS6ARWFH")
compare_recommendations_for_user("A17B6IPLJ964N0")
compare_recommendations_for_user("AWJ9J0JAHN6PQ")
compare_recommendations_for_user("AXOO7BPM22BDO")
compare_recommendations_for_user("A15GBKY0IPZJI3")
compare_recommendations_for_user("AWMORCDEUIBFO")
compare_recommendations_for_user("AWG2O9C42XW5G")
compare_recommendations_for_user("A15JTJXQXO22JJ")<jupyter_output>For user A15JTJXQXO22JJ the recommendations are:


HYBRID

         asin                                              title
0  B01AB9Q2RE            Say It To Me: EP/CD-single - UK Release
1  B00000I7LP                                         Slim Shady
2  B000X1HU68                                   This Is Thirteen
3  B0016DCUIA                    Invasion Of The Booty Snatchers
4  B00146B9LI                   Reaching For The Sky (LP RECORD)
5  B00JGEU434  <span class="a-size-medium a-color-secondary a...
6  9714721180                      METAL RESISTANCE    BABYMETAL
7  B001EE4PUM                                                NaN
8  B0059CQ3QY                                                NaN
9  B01AN3DNE8                                                NaN
====================================================================================================
CONTENT-BASED

         asin                                    title
0  B01AB9Q2RE  Say It To Me: EP/CD-single - UK Release
[...]
{'fraction_non_alphanumeric': 0.14109657947686116, 'fraction_numerical': 0.022635814889336015, 'mean_word_length': 3.410981697171381, 'pattern_counts': {'":': 4, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '187905', 'license': 'MIT', 'n_tokens_mistral': 2988, 'n_tokens_neox': 2840, 'n_words': 446, 'path': 'hybrid-RS.ipynb', 'repo_name': 'ilias-ant/recommender-systems', 'size': '7952'}