<jupyter_start><jupyter_text>Data Merge<jupyter_code>BASE_PATH = "../preprocessed_data/"
pc_sim_train = pd.read_csv(BASE_PATH+"train_product_context_sim.csv")
pc_sim_sub = pd.read_csv(BASE_PATH+"val_product_context_sim.csv")

train = train.merge(pc_sim_train, on=["query_id", "user_id", "session_id", "product_id"], how="left")
sub = sub.merge(pc_sim_sub, on=["query_id", "user_id", "session_id", "product_id"], how="left")

train = train[["query_id", "product_context_similarity", "is_click"]]
train = train.fillna(0)

train.shape, sub.shape
def get_pred(row, threshold):
    if row["product_context_similarity"]>=threshold:
        return 1
    return 0

dic = {}
def get_metrics(data):
    threshold_range = np.arange(0.7, 0.9, 0.01).tolist()[1:]
    for threshold in threshold_range:
        data["click_pred"] = data.swifter.apply(lambda x: get_pred(x, threshold), 1)
        y_true = data["is_click"].values
        y_pred = data["click_pred"].values
        precision = precision_score(y_true, y_pred, average='micro')
        recall = recall_score(y_true, y_pred, average='micro')
        f1 = f1_score(y_true, y_pred, average='micro')
        print(f"------------THRESHOLD = {threshold}----------------")
        print("PRECISION: ", precision)
        print("RECALL: ", recall)
        print("F1-Score: ", f1)
        dic[threshold] = {"precision": precision, "recall": recall, "F1": f1}
               
get_metrics(train)
json.dump(dic, open("../heuristic_dict_v2.json", "w"))
import matplotlib.pyplot as plt
bins = [0, 0.2, 0.4, 0.6, 0.8, 1.0]
temp = train[train["is_click"]==0]
temp['binned'] = pd.cut(temp['product_context_similarity'], bins)
temp.head()

temp["binned"].value_counts()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18218623481781376, 'fraction_numerical': 0.017929438982070563, 'mean_word_length': 4.242424242424242, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 6, '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': '292347', 'license': 'MIT', 'n_tokens_mistral': 650, 'n_tokens_neox': 625, 'n_words': 108, 'path': 'heuristic_XGB.ipynb', 'repo_name': 'diptamath/Context-aware-Retail-Product-Recommendation', 'size': '1729'}